using MECF.Framework.UI.Client.RecipeEditorLib.DGExtension.CustomColumn;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

namespace MECF.Framework.UI.Client.RecipeEditorLib.RecipeModel.Params
{
    public class FlowModeParam : ComboxParam
    {
        #region Variables

        public enum FlowModeEnum
        {
            Vent,
            Purge,
            Run
        }

        #endregion

        #region Constructors

        public FlowModeParam(IEnumerable<ComboxColumn.Option> options, string controlName, string initValue, string defaultValue) : 
            base(options, controlName, initValue, defaultValue)
        {
        }

        #endregion

        #region Properties

        public override ComboxColumn.Option Value
        {
            get => _value;
            set
            {
                var isSwitchToVent = false;
                if (value.Equals(FlowModeEnum.Purge))
                {
                    if (Previous is FlowModeParam pp)
                    {
                        if (pp.Value.Equals(FlowModeEnum.Run))
                        {
                            isSwitchToVent = true;
                        }
                    }

                    if (Next is FlowModeParam np)
                    {
                        if (np.Value.Equals(FlowModeEnum.Run))
                        {
                            isSwitchToVent = true;
                        }
                    }
                }
                else if (value.Equals(FlowModeEnum.Run))
                {
                    if (Previous is FlowModeParam pp)
                    {
                        if (pp.Value.Equals(FlowModeEnum.Purge))
                        {
                            isSwitchToVent = true;
                        }
                    }

                    if (Next is FlowModeParam np)
                    {
                        if (np.Value.Equals(FlowModeEnum.Purge))
                        {
                            isSwitchToVent = true;
                        }
                    }
                }

                if (isSwitchToVent)
                    MessageBox.Show("Flow mode cannot be switched directly between 'Purge' and 'Run'.", "Error",
                        MessageBoxButton.OK, MessageBoxImage.Error);

                _value = isSwitchToVent
                    ? Options.FirstOrDefault(x => x.Equals(FlowModeEnum.Vent)) ?? Options.First()
                    : value;
                
                ColumnPermChangedCallback?.Invoke(this);
                _isSaved = _value.Equals(_valueSnapshot);
                
                OnPropertyChanged();
                OnPropertyChanged(nameof(IsSaved));
            }
        }

        #endregion

        #region Methods

        private void InnerValidate()
        {
            // 判断Flow Mode是否直接在Purge和Run之间切换。
            
            var currentStepOption = Value;
            var flowModeValid = true;

            if (Previous != null)
            {
                var previousStepOption = ((ComboxParam)Previous).Value;
                if ((previousStepOption.Equals(FlowModeEnum.Purge) && currentStepOption.Equals(FlowModeEnum.Run))
                    || (previousStepOption.Equals(FlowModeEnum.Run) && currentStepOption.Equals(FlowModeEnum.Purge)))
                {
                    flowModeValid = false;
                }
            }

            if (Next != null)
            {
                var nextStepOption = ((ComboxParam)Next).Value;
                if ((nextStepOption.Equals(FlowModeEnum.Purge) && currentStepOption.Equals(FlowModeEnum.Run))
                    || (nextStepOption.Equals(FlowModeEnum.Run) && currentStepOption.Equals(FlowModeEnum.Purge)))
                {
                    flowModeValid = false;
                }
            }

            IsValid = flowModeValid;
            ValidationError = !flowModeValid
                ? "Flow mode cannot be switched directly between 'Purge' and 'Run'"
                : null;
        }

        public override void Validate()
        {
            var linkedList = Flatten();

            foreach (var param in linkedList)
            {
                if(param is FlowModeParam p)
                    p.InnerValidate();
            }

            //TODO 如果当前参数是TMA或TCS，当设备未安装，流量模式必须为Purge
            //Issue TCS没有对应的系统参数；TMA要区分PM腔，但Recipe不分PM腔，此处不好判断
            if (ControlName == RecipeControlNames.TMA_FLOW_MODE)
            {

            }

            if (ControlName == RecipeControlNames.TCS_FLOW_MODE)
            {

            }
        }

        #endregion
    }
}
