using MECF.Framework.Common.Aitex.Core.RT.EMS;
using MECF.Framework.UI.Client.ClientBase;
using OpenSEMI.ClientBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MECF.Framework.UI.Client.CenterViews.Maintain.ViewModels
{
    public class MaintainItemSettingViewModel : DialogViewModel<ResultYesNo>
    {
        #region Properties

        private MaintainerItemConfigInfo _iteminfo =new();

        public MaintainerItemConfigInfo ItemInfo
        {
            get { return _iteminfo; }
            set { _iteminfo = value; NotifyOfPropertyChange(nameof(ItemInfo)); }
        }

        private bool _addingmode = true;

        public bool AddingMode
        {
            get { return _addingmode; }
            set { _addingmode = value; NotifyOfPropertyChange(nameof(AddingMode)); }
        }

        private List<string> _parentnames = new();

        public List<string> ParentNames
        {
            get { return _parentnames; }
            set { _parentnames = value; NotifyOfPropertyChange(nameof(ParentNames)); }
        }

        MaintainerItemConfigInfo origininfo;
        #endregion

        #region Constructors
        public MaintainItemSettingViewModel()
        {
            this.DisplayName = "Setting";
            this.DialogResult = ResultYesNo.NO;
        }

        /// <summary>
        /// 修改配置时用的构造函数
        /// </summary>
        /// <param name="info"></param>
        /// <param name="infos"></param>
        public MaintainItemSettingViewModel(MaintainerItemConfigEnableInfo info, List<MaintainerItemConfigInfo> infos)
        {
            ItemInfo = (MaintainerItemConfigInfo)info.Config.Clone();      //拷贝成新的对象
            var list = new List<string>();
            infos.ForEach(i =>
            {
                if (!list.Contains(i.ParentName))
                {
                    list.Add(i.ParentName);
                }
            });
            ParentNames = list;

            AddingMode = false;
            this.DisplayName = "Setting";
            this.DialogResult = ResultYesNo.NO;

            origininfo = info.Config;
        }

        /// <summary>
        /// 新增项目时用的构造函数
        /// </summary>
        /// <param name="info"></param>
        /// <param name="infos"></param>
        public MaintainItemSettingViewModel(List<MaintainerItemConfigInfo> infos)
        {
            var uidlist = new List<string>();
            var list = new List<string>();
            infos.ForEach(i => {
                if (!list.Contains(i.ParentName))
                {
                    list.Add(i.ParentName);
                }
                uidlist.Add(i.UID);
            });
            //新增UID
            for (int i = 1; i < 10000; i++)
            {
                if (!uidlist.Contains(i.ToString()))
                {
                    ItemInfo.UID = i.ToString();
                    break;
                }
            }

            //修改index
            for (int i = 1; i < 10000; i++)
            {
                if (infos.FindIndex(j => j.Index == i) == -1)
                {
                    ItemInfo.Index = i;
                    break;
                }
            }

            ParentNames = list;

            AddingMode = true;
            this.DisplayName = "Adding";
            this.DialogResult = ResultYesNo.NO;

        }
        #endregion

        #region Functions

        public void OKExit()
        {
            if (string.IsNullOrWhiteSpace(ItemInfo.Name) ||
                    string.IsNullOrWhiteSpace(ItemInfo.ParentName))
            {
                DialogBox.ShowError("Incomplete Configuration.");
                return;
            }

            if (!AddingMode)
            {
                if (origininfo.Name == ItemInfo.Name &&
                origininfo.ParentName == ItemInfo.ParentName &&
                origininfo.Description == ItemInfo.Description &&
                origininfo.DefaultRecord == ItemInfo.DefaultRecord &&
                origininfo.FilePath == ItemInfo.FilePath)
                {
                    this.DialogResult = ResultYesNo.NO;
                    this.TryClose();
                    return;
                }

                this.DialogResult = ResultYesNo.YES;
                this.TryClose();
                return;
            }
            else
            {
                this.DialogResult = ResultYesNo.YES;
                this.TryClose();
                return;
            }
        }

        public void NGExit()
        {
            this.DialogResult = ResultYesNo.NO;
            this.TryClose();
        }

        #endregion
    }
}
