using Aitex.Core.RT.SCCore;
using Caliburn.Micro;
using Caliburn.Micro.Core;
using MECF.Framework.Common.DataCenter;
using MECF.Framework.Common.RecipeCenter;
using MECF.Framework.UI.Client.CenterViews.Editors.Recipe;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using FurnaceUI.Models;
using FurnaceUI.Views.Parameter;
using FurnaceUI.Views.Recipes;
using MECF.Framework.UI.Client.CenterViews.Configs.SystemConfig;
using SciChart.Charting2D.Interop;
using RecipeEditorLib.RecipeModel.Params;
using OpenSEMI.ClientBase;
using MECF.Framework.UI.Client.ClientBase;
namespace FurnaceUI.Views.Editors
{
    public class RecipeTempModeViewModel : FurnaceUIViewModelBase
    {
        private bool _isProfileSelected = false;
        public bool IsProfileSelected
        {
            get => _isProfileSelected;
            set
            {
                _isProfileSelected = value;
                NotifyOfPropertyChange(nameof(IsProfileSelected));
            }
        }
        private string _selectProfileTable = "";
        public string SelectProfileTable
        {
            get => _selectProfileTable;
            set
            {
                _selectProfileTable = value;
                NotifyOfPropertyChange(nameof(SelectProfileTable));
            }
        }
        private bool _modelLeftVisibility;
        public bool ModelLeftVisibility
        {
            get => _modelLeftVisibility;
            set
            {
                _modelLeftVisibility = value;
                NotifyOfPropertyChange(nameof(ModelLeftVisibility));
            }
        }
        private RecipeDataBase _CurrentRecipe;
        public RecipeDataBase CurrentRecipe
        {
            get { return _CurrentRecipe; }
            set { _CurrentRecipe = value; this.NotifyOfPropertyChange(nameof(CurrentRecipe)); }
        }
        private bool _heaterControlIsChecked;
        public bool HeaterControlIsChecked
        {
            get { return _heaterControlIsChecked; }
            set { _heaterControlIsChecked = value; this.NotifyOfPropertyChange(nameof(HeaterControlIsChecked)); }
        }
        private bool _furnaceControlIsChecked;
        public bool FurnaceControlIsChecked
        {
            get { return _furnaceControlIsChecked; }
            set { _furnaceControlIsChecked = value; this.NotifyOfPropertyChange(nameof(FurnaceControlIsChecked)); }
        }
        private bool _furnaceDirectControlIsChecked;
        public bool FurnaceDirectControlIsChecked
        {
            get { return _furnaceDirectControlIsChecked; }
            set { _furnaceDirectControlIsChecked = value; this.NotifyOfPropertyChange(nameof(FurnaceDirectControlIsChecked)); }
        }
        private bool _profileControlIsChecked;
        public bool ProfileControlIsChecked
        {
            get { return _profileControlIsChecked; }
            set { _profileControlIsChecked = value; this.NotifyOfPropertyChange(nameof(ProfileControlIsChecked)); }
        }
        public bool IsSave { get; set; }
        public bool IsManualSet { get; set; }
        private string _selectBtnName;
        public string SelectBtnName
        {
            get { return _selectBtnName; }
            set { _selectBtnName = value; this.NotifyOfPropertyChange(nameof(SelectBtnName)); }
        }
        public RecipeTempModeViewModel()
        {
        }
        private bool _isEnable;
        public bool IsEnable
        {
            get { return _isEnable; }
            set { _isEnable = value; this.NotifyOfPropertyChange(nameof(IsEnable)); }
        }
        public string ResultString { get; set; }
        public string RecipeType { get; set; }
        protected override void OnViewLoaded(object view)
        {
            base.OnViewLoaded(view);
            LoadData();
            InitModelLeftVisibility();
            IsEnable = IsManualSet == true ? IsManualSet : CGlobal.RecipeProcessEditViewEnable;
        }
        private void LoadData()
        {
            if (!string.IsNullOrEmpty(SelectProfileTable))
            {
                SelectProfileTable = SelectProfileTable.TrimEnd(')');
            }
            else
            {
                SelectProfileTable = "Heater";
            }
            InitControlBtn();
        }
        private const string FURNACE = "Furnace";
        private const string DIRECT = "Furnace Direct";
        private const string HEATER = "Heater";
        private const string PROFILE = "Profile";
        private void InitControlBtn()
        {
            if (string.IsNullOrEmpty(SelectBtnName))
            {
                if (!string.IsNullOrEmpty(SelectProfileTable))
                {
                    if (SelectProfileTable.StartsWith(PROFILE))
                        SelectBtnName = PROFILE;
                    else if (SelectProfileTable.StartsWith(HEATER))
                        SelectBtnName = HEATER;
                    else if (SelectProfileTable.StartsWith(DIRECT))
                        SelectBtnName = DIRECT;
                    else
                        SelectBtnName = FURNACE;
                }
                else
                    SelectBtnName = HEATER;
            }
            if (PROFILE.Equals(SelectBtnName))
            {
                FurnaceDirectControlIsChecked = false;
                FurnaceControlIsChecked = false;
                HeaterControlIsChecked = false;
                ProfileControlIsChecked = true;
                IsProfileSelected = true;
            }
            if (FURNACE.Equals(SelectBtnName))
            {
                FurnaceDirectControlIsChecked = false;
                FurnaceControlIsChecked = true;
                HeaterControlIsChecked = false;
                ProfileControlIsChecked = false;
            }
            if (HEATER.Equals(SelectBtnName))
            {
                FurnaceDirectControlIsChecked = false;
                FurnaceControlIsChecked = false;
                HeaterControlIsChecked = true;
                ProfileControlIsChecked = false;
            }
            if (DIRECT.Equals(SelectBtnName))
            {
                FurnaceDirectControlIsChecked = true;
                FurnaceControlIsChecked = false;
                HeaterControlIsChecked = false;
                ProfileControlIsChecked = false;
            }
        }
        /// 
        /// Profile Control按钮 手动且Heater不展示  Recipe展示
        /// 
        private void InitModelLeftVisibility()
        {
            ModelLeftVisibility = true;
            //if (null == CurrentRecipe)
            //{
                if (!string.IsNullOrEmpty(SelectBtnName) && SelectBtnName.ToUpper().Equals(HEATER))
                {
                    ModelLeftVisibility = false;
                }
            //}
        }
        public void ProfileClick()
        {
            ShowProfileTable();
        }
        public void RdoChecked(string cmd)
        {
            SelectBtnName = cmd;
            switch (cmd)
            {
                case "Heater":
                    ResultString = "Heater";
                    IsProfileSelected = false;
                    ProfileControlIsChecked = false;
                    InitModelLeftVisibility();
                    break;
                case "Furnace":
                    ResultString = "Furnace";
                    IsProfileSelected = false;
                    ProfileControlIsChecked = false;
                    ModelLeftVisibility = true;
                    break;
                case "Direct":
                    SelectBtnName = "Furnace Direct";
                    ResultString = "Furnace Direct";
                    ProfileControlIsChecked = false;
                    IsProfileSelected = false;
                    ModelLeftVisibility = true;
                    break;
                case "Profile":
                    ResultString = "Profile";
                    IsProfileSelected = true;
                    ShowProfileTable();
                    break;
                default:
                    break;
            }
        }
        public void ShowProfileTable()
        {
            var windowManager = IoC.Get();//"Combination.ProfileCondition"
            StringParam profile = CurrentRecipe.ConfigItems.FirstOrDefault(x => x.Name == "Combination.ProfileCondition") as StringParam;
            if (profile == null)
            {
                DialogBox.ShowDialog(DialogButton.Cancel, DialogType.INFO, "Please set the combinatin file first");
                return;
            }
            TempProfileEditViewModel tempProfileEditViewModel;
            tempProfileEditViewModel = new TempProfileEditViewModel();
            tempProfileEditViewModel.CurrentRecipe = CurrentRecipe;
            tempProfileEditViewModel.IsEditEnabled = false;
            tempProfileEditViewModel.ProfileTable1.IsChecked = false;
            tempProfileEditViewModel.ProfileTable2.IsChecked = false;
            tempProfileEditViewModel.ProfileTable3.IsChecked = false;
            string test = SelectProfileTable;
            if (SelectProfileTable != null && SelectProfileTable != "Heater" && SelectProfileTable != "Furnace Direct" && SelectProfileTable != "Furnace" && SelectProfileTable != "")
            {
                if (CurrentRecipe == null && SelectProfileTable.Contains(":"))
                {
                    if (SelectProfileTable.Split(':')[0] == "1")
                    {
                        tempProfileEditViewModel.Table1CheckinRecipe = true;
                    }
                    if (SelectProfileTable.Split(':')[0] == "2")
                    {
                        tempProfileEditViewModel.Table2CheckinRecipe = true;
                    }
                    if (SelectProfileTable.Split(':')[0] == "3")
                    {
                        tempProfileEditViewModel.Table3CheckinRecipe = true;
                    }
                }
                else
                {
                    if (SelectProfileTable.Split(',')[1] == "1")
                    {
                        tempProfileEditViewModel.Table1CheckinRecipe = true;
                    }
                    if (SelectProfileTable.Split(',')[1] == "2")
                    {
                        tempProfileEditViewModel.Table2CheckinRecipe = true;
                    }
                    if (SelectProfileTable.Split(',')[1] == "3")
                    {
                        tempProfileEditViewModel.Table3CheckinRecipe = true;
                    }
                }
            }
            if ((bool)(windowManager as WindowManager)?.ShowDialogWithTitle(tempProfileEditViewModel, null, "TempProfileEdit"))
            {
                SelectProfileTable = tempProfileEditViewModel.ResultString;
                ResultString = $"Profile({tempProfileEditViewModel.ResultString})";
            }
            else
            {
                if (string.IsNullOrEmpty(tempProfileEditViewModel.ResultString))
                {
                    if (!string.IsNullOrEmpty(ResultString) && !ResultString.Contains(","))
                    {
                        ResultString = "";
                        SelectProfileTable = "";
                    }
                }
            }
        }
        public void TempSetSave()
        {
            if (string.IsNullOrEmpty(ResultString) && !string.IsNullOrEmpty(SelectProfileTable))
            {
                ResultString = SelectProfileTable.Contains("Profile") ? $"{SelectProfileTable})" : SelectProfileTable;
            }
            IsSave = true;
            ((Window)GetView()).DialogResult = true;
        }
        public void TempSetCancel()
        {
            IsSave = false;
            ((Window)GetView()).DialogResult = false;
        }
    }
}