using MainShell.Models;
using MainShell.ParaSetting.Model;
using MaxwellControl.Controls;
using MaxwellFramework.Core.Interfaces;
using MwFramework.ManagerService;
using Stylet;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace MainShell.ParaSetting.ViewModel
{
    public class ProduceControlViewModel : BaseScreen
    {
        private ObservableCollection<SubProcessItem> _subProcesses = new ObservableCollection<SubProcessItem>();

        public ObservableCollection<SubProcessItem> SubProcesses
        {
            get => _subProcesses;
            set
            {
                _subProcesses = value;
                OnPropertyChanged("SubProcesses");
            }
        }

        private RunSetting _runSetting;

        public RunSetting RunSetting
        {
            get => _runSetting;
            set
            {
                _runSetting = value;
                OnPropertyChanged("RunSetting");
            }
        }

        private IParamList _paramList;
        public ProduceControlViewModel(IParameterManager parameterManager)
        {
            _paramList = parameterManager as IParamList;
            if (_paramList == null)
            {
                throw new ArgumentNullException(nameof(parameterManager));
            }
        }

        protected override void OnViewLoaded()
        {
            base.OnViewLoaded();
            RunSetting = _paramList.GetParameter<RunSetting>();
            InitializeSubProcesses();
        }

        private void InitializeSubProcesses()
        {
            if (SubProcesses.Count > 0) return;
            SubProcesses.Add(new SubProcessItem
            {
                ProcessName = "产品上料",
                IsEnabled = RunSetting.FlowControlItem.SubStrateLoadEnable,
                ToolTipText = "控制产品上料子流程的启用/禁用"
            });
            SubProcesses.Add(new SubProcessItem
            {
                ProcessName = "产品下料",
                IsEnabled = RunSetting.FlowControlItem.SubStrateUnLoadEnable,
                ToolTipText = "控制产品下料子流程的启用/禁用"
            });
            SubProcesses.Add(new SubProcessItem
            {
                ProcessName = "产品定位",
                IsEnabled = RunSetting.FlowControlItem.SubstratePositionEnable,
                ToolTipText = "控制产品高度测量子流程的启用/禁用"
            });
            SubProcesses.Add(new SubProcessItem
            {
                ProcessName = "产品高度测量",
                IsEnabled = RunSetting.FlowControlItem.SubstrateHeightMeasureEnable,
                ToolTipText = "控制产品高度测量子流程的启用/禁用"
            });
            SubProcesses.Add(new SubProcessItem
            {
                ProcessName = "方片上料",
                IsEnabled = RunSetting.FlowControlItem.WaferLoadEnable,
                ToolTipText = "控制方片下料子流程的启用/禁用"
            });
            SubProcesses.Add(new SubProcessItem
            {
                ProcessName = "方片下料",
                IsEnabled = RunSetting.FlowControlItem.WaferUnLoadEnable,
                ToolTipText = "控制方片下料子流程的启用/禁用"
            });
            SubProcesses.Add(new SubProcessItem
            {
                ProcessName = "方片拉直",
                IsEnabled = RunSetting.FlowControlItem.WaferStraightenEnable,
                ToolTipText = "控制方片拉直子流程的启用/禁用"
            });
            SubProcesses.Add(new SubProcessItem
            {
                ProcessName = "晶粒飞拍",
                IsEnabled = RunSetting.FlowControlItem.WaferPositionEnable,
                ToolTipText = "控制晶粒飞拍子流程的启用/禁用"
            });
            SubProcesses.Add(new SubProcessItem
            {
                ProcessName = "转移作业",
                IsEnabled = RunSetting.FlowControlItem.BondingEnable,
                ToolTipText = "控制转移作业子流程的启用/禁用"
            });
            SubProcesses.Add(new SubProcessItem
            {
                ProcessName = "复检",
                IsEnabled = RunSetting.FlowControlItem.SubstrateRecheckEnable,
                ToolTipText = "控制复检子流程的启用/禁用"
            });
        }

        public void BtnSave()
        {
            RunSetting.FlowControlItem.SubStrateLoadEnable = SubProcesses.First(p => p.ProcessName == "产品上料").IsEnabled;
            RunSetting.FlowControlItem.SubStrateUnLoadEnable = SubProcesses.First(p => p.ProcessName == "产品下料").IsEnabled;
            RunSetting.FlowControlItem.WaferLoadEnable = SubProcesses.First(p => p.ProcessName == "方片上料").IsEnabled;
            RunSetting.FlowControlItem.WaferUnLoadEnable = SubProcesses.First(p => p.ProcessName == "方片下料").IsEnabled;
            RunSetting.FlowControlItem.SubstratePositionEnable = SubProcesses.First(p => p.ProcessName == "产品定位").IsEnabled;
            RunSetting.FlowControlItem.SubstrateHeightMeasureEnable = SubProcesses.First(p => p.ProcessName == "产品高度测量").IsEnabled;
            RunSetting.FlowControlItem.WaferStraightenEnable = SubProcesses.First(p => p.ProcessName == "方片拉直").IsEnabled;
            RunSetting.FlowControlItem.WaferPositionEnable = SubProcesses.First(p => p.ProcessName == "晶粒飞拍").IsEnabled;
            RunSetting.FlowControlItem.BondingEnable = SubProcesses.First(p => p.ProcessName == "转移作业").IsEnabled;
            RunSetting.FlowControlItem.SubstrateRecheckEnable = SubProcesses.First(p => p.ProcessName == "复检").IsEnabled;
            RunSetting.Write();
            MessageBox.Show("保存完成");
        }
    }


    public class SubProcessItem : PropertyChangedBase
    {
        private string _processName;
        public string ProcessName
        {
            get { return _processName; }
            set { SetAndNotify(ref _processName, value); }
        }

        private bool _isEnabled;
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set 
            {
                SetAndNotify(ref _isEnabled, value);
                if (value) StatusText = "已启用";
                else StatusText = "已禁用";
            }
        }

        private string _toolTipText;
        public string ToolTipText
        {
            get { return _toolTipText; }
            set { SetAndNotify(ref _toolTipText, value); }
        }

        private string _statusText;
        public string StatusText
        {
            get { return _statusText; }
            set { SetAndNotify(ref _statusText, value); }
        }
        //public string StatusText => IsEnabled ? "已启用" : "已禁用";
        //public Brush StatusColor => IsEnabled ? Brushes.Green : Brushes.Gray;
    }
}
