using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Aitex.Core.RT.Device.PmDevices;
using MECF.Framework.UI.Client.RecipeEditorLib.DGExtension.CustomColumn;

namespace MECF.Framework.UI.Client.RecipeEditorLib.RecipeModel.Params
{
    public class ComboxParam : ParamBaseWithGenericValue<ComboxColumn.Option>
    {
        #region Constructors
        
        public ComboxParam(IEnumerable<ComboxColumn.Option> options, string controlName, string initValue, string defaultValue)
        {
            // options 参数不能为null，或没有项目
            var enumerable = options as ComboxColumn.Option[] ?? options.ToArray();
            Debug.Assert(enumerable != null);
            Debug.Assert(enumerable.Any());

            ControlName = controlName;
            Options = enumerable;

            // 修复兼容性问题
            initValue = CorrectValue(initValue);

            // 检查Recipe中保存的值是否在Options中定义？
            // 如果没有定义，则取默认值。
            ComboxColumn.Option selected;
            if (!string.IsNullOrEmpty(initValue) &&
                Options.FirstOrDefault(x => x.Equals(initValue)) != null)
            {
                // 注意：Recipe中保存的是ControlName，编辑器中应显示DisplayName
                selected = Options.First(x => x.Equals(initValue));
            }
            else
            {
                if (!string.IsNullOrEmpty(defaultValue) &&
                    Options.Any(x => x.Equals(defaultValue)))
                    selected = Options.First(x => x.Equals(defaultValue));
                else
                    selected = Options.First();
            }

            _value = selected;
        }

        #endregion

        #region Properties

        public IEnumerable<ComboxColumn.Option> Options { get; }

        private bool _isEditable;
        public bool IsEditable
        {
            get => _isEditable;
            set => Set(ref _isEditable, value);
        }

        private string _foreground = "Black";
        public string Foreground
        {
            get => _foreground;
            set => Set(ref _foreground, value);
        }
        public string LoopBackground => "Transparent";

        public bool IsLoopItem => false;
        
        #endregion

        #region Methods

        /// <summary>
        /// 修正当前参数的值，解决兼容性问题。
        /// <para>
        /// 1、PSU和SCR控制策略为Pyro时，变更为PyroFollow。
        /// </para>
        /// </summary>
        /// <param name="initValue"></param>
        /// <returns></returns>
        private string CorrectValue(string initValue)
        {
            if (ControlName == "TC1.SetHeaterMode" || ControlName == "TC2.SetHeaterMode2")
            {
                // 处理TC兼容性问题
                if (initValue == "Pyro" || initValue == HeatStrategy.PyroFollow.ToString() || initValue == "1")
                    return "Follow";
                
                if (initValue == HeatStrategy.PyroAuto.ToString() || initValue == "2")
                    return "Auto";

                if (initValue == HeatStrategy.Power.ToString() || initValue == "0")
                    return "Power";
            }

            return initValue;
        }

        #endregion
    }
}
