using DocumentFormat.OpenXml.EMMA;
using DocumentFormat.OpenXml.Wordprocessing;
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.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml.Linq;

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

        MaintainerSettingView view;

        private bool _addingmode = true;

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

        private MaintainerInfo _planinfo = new();

        public MaintainerInfo MaInfo
        {
            get { return _planinfo; }
            set { _planinfo = value; NotifyOfPropertyChange(nameof(MaInfo)); }
        }

        private List<string> _typelist = new() {nameof(PresetMaintainer), nameof(ProcessMaintainer), nameof(CoatingMaintainer) };

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

        private List<string> _plannames = new();

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

        private DateTime originstartdate;

        private List<MaintainerInfo> _planinfos = new();

        public List<MaintainerInfo> PlanInfos
        {
            get { return _planinfos; }
            set { _planinfos = value; NotifyOfPropertyChange(nameof(PlanInfos)); }
        }

        MaintainerInfo origininfo;

        private Visibility vi = Visibility.Visible;

        public Visibility Visible
        {
            get { return vi; }
            set { vi = value; NotifyOfPropertyChange(nameof(Visible)); }
        }
 
        #endregion

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

        /// <summary>
        /// 修改配置时用的构造函数
        /// </summary>
        /// <param name="info"></param>
        /// <param name="infos"></param>
        public MaintainerSettingViewModel(MaintainerInfo info, List<MaintainerInfo> infos)
        {
            MaInfo = (MaintainerInfo)info.Clone();      //拷贝成新的对象

            if (info.Type == nameof(CoatingMaintainer))
            {
                Visible = Visibility.Hidden;
            }
            else
            {
                Visible = Visibility.Visible;
            }
            



            infos.ForEach(i => {
                if (i.Type == MaInfo.Type && i.Name == MaInfo.Name && i.Module != MaInfo.Module)
                {
                    PlanInfos.Add(i);
                }

                if (!PlanNames.Contains(i.Name))
                {
                    PlanNames.Add(i.Name);
                }
            });

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

            origininfo = info;
        }

        /// <summary>
        /// 新增项目时用的构造函数
        /// </summary>
        /// <param name="info"></param>
        /// <param name="infos"></param>
        public MaintainerSettingViewModel(Dictionary<string, ObservableCollection<MaintainerInfo>> infodic)
        {
            var list = new List<int>();
            foreach (var infos in infodic.Values)
            {
                foreach (var i in infos)
                {
                    list.Add(i.Index);
                    if (i.Name != MaInfo.Name || i.Module != MaInfo.Module)
                    {
                        PlanInfos.Add(i);
                    }
                    if (!PlanNames.Contains(i.Name))
                    {
                        PlanNames.Add(i.Name);
                    }
                }
            }
            

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

            for (int i = 0; i < 10000; i++)
            {
                if (!list.Contains(i))
                {
                    MaInfo.Index = i;
                    break;
                }
            }

            MaInfo.StartDate = new DateTime(2023,12,31);
            MaInfo.Type = nameof(PresetMaintainer);
            MaInfo.Threshold = 100;
            MaInfo.TimeDisplayUnit = TimeUnit.Hours;

        }
        #endregion

        #region Functions

        protected override void OnViewLoaded(object _view)
        {
            base.OnViewLoaded(_view);
            this.view = (MaintainerSettingView)_view;
            this.view.wfTimeStart.Value = MaInfo.StartDate;
            //this.view.wfTimeStart.MinDate = StartDate;  //过去时间不允许再选择
        }

        public void OKExit()
        {
            if (!AddingMode)
            {
                if (originstartdate == view.wfTimeStart.Value &&
                origininfo.Module == MaInfo.Module &&
                origininfo.Name == MaInfo.Name &&
                origininfo.Index == MaInfo.Index &&
                origininfo.Enable == MaInfo.Enable &&
                origininfo.Description == MaInfo.Description &&
                originstartdate == MaInfo.StartDate &&
                origininfo.Threshold == MaInfo.Threshold &&
                origininfo.TimeDisplayUnit == MaInfo.TimeDisplayUnit)
                {
                    this.DialogResult = ResultYesNo.NO;
                    this.TryClose();
                    return;
                }
                string name = MaInfo.Name;
                string module = MaInfo.Module;

                foreach (var item in PlanInfos)
                {
                    if (item.Module == module)
                    {
                        DialogBox.ShowError("Duplicate Module in Plan(" + name + ")");
                        return;
                    }
                }
                this.DialogResult = ResultYesNo.YES;

                MaInfo.StartDate = view.wfTimeStart.Value;

                this.TryClose();
            }
            else
            {
                if (string.IsNullOrWhiteSpace(MaInfo.Name))
                {
                    DialogBox.ShowError("Name can not be empty!");
                    return;
                }
                string name = MaInfo.Name;
                string module = MaInfo.Module;
                string type = MaInfo.Type;

                foreach (var item in PlanInfos)
                {
                    if (item.EqualsTypeNameModule(type,name,module))
                    {
                        DialogBox.ShowError("Duplicate Module in Plan(" + name + ")");
                        return;
                    }
                }

                this.DialogResult = ResultYesNo.YES;

                MaInfo.StartDate = view.wfTimeStart.Value;

                this.TryClose();
            }
            
        }

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

        public void SelectionChanged()
        {
            if (MaInfo.Type == nameof(CoatingMaintainer))
                Visible = Visibility.Visible;
            else
                Visible = Visibility.Hidden;
        }
        #endregion
    }
}
