using MECF.Framework.Common.DataCenter;
using MECF.Framework.Common.MECF.Framework.Common.SCCore;
using MECF.Framework.Common.OperationCenter;
using MECF.Framework.UI.Client.CenterViews.Configs.SystemConfig;
using MECF.Framework.UI.Client.ClientBase;
using OpenSEMI.ClientBase.Command;
using OpenSEMI.ClientBase;
using SciChart.Core.Extensions;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MECF.Framework.Common.Aitex.Core.RT.EMS;
using Aitex.Core.Util;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using log4net;
using System.Windows.Forms;
using System.Reflection;
using DocumentFormat.OpenXml.Wordprocessing;
using System.IO;
using Aitex.Common.Util;
using Caliburn.Micro;
using MECF.Framework.Common.Account.Extends;
using SciChart.Charting.Visuals.TradeChart.MultiPane;
using System.Runtime.InteropServices;
using System.Collections.ObjectModel;
using System.IO.Ports;
using Xceed.Wpf.Toolkit.Primitives;

namespace MECF.Framework.UI.Client.CenterViews.Maintain.ViewModels
{
    public class MaintainViewModel : UiViewModelBase, ISupportMultipleSystem
    {
        #region Properties

        private readonly IWindowManager _windowmanager;

        private List<MaintainerInfo> _infoList = new();

        private DateTime _handShake = DateTime.Now;
        private DateTime _listChangedHandShake = DateTime.Now;

        [Subscription("MaintainManager.MaintainerInfos")]
        public List<MaintainerInfo> InfoList
        {
            get
            {
                return _infoList;
            }
            set
            {
                foreach (var item in _infoList)
                {
                    foreach (var item2 in value)
                    {
                        if (item.EqualsTypeNameModule(item2.Type, item2.Name, item2.Module))
                        {
                            item.Update(item2);
                            break;
                        }
                    }
                }
            }
        }

        private MaintainerInfo _selectedItem=new();

        public MaintainerInfo SelectedItem
        {
            get { return _selectedItem; }
            set {
                if (value != null)
                {
                    _selectedItem = value;
                    SelectedMaintainerItemCollection = value.MaintainerItemInfos;
                }
                else
                {
                    _selectedItem = null;
                    SelectedMaintainerItemCollection=null;
                }
                NotifyOfPropertyChange(nameof(SelectedItem));
            }
        }

        private List<MaintainerItemInfo> _selectedMaintainerItemCollection = new();

        public List<MaintainerItemInfo> SelectedMaintainerItemCollection
        {
            get { return _selectedMaintainerItemCollection; }
            set
            {
                _selectedMaintainerItemCollection = value;
                NotifyOfPropertyChange(nameof(SelectedMaintainerItemCollection));
            }
        }

        bool _firstontimer = false;
        #endregion

        #region Constructors
        public MaintainViewModel()
        {
            _windowmanager = new WindowManager();

        }
        #endregion

        #region Functions

        protected override void OnInitialize()
        {
            List<MaintainerInfo> list = (List<MaintainerInfo>)QueryDataClient.Instance.Service.GetData($"MaintainManager.MaintainerInfos");
            list.ForEachDo(i => { 
                if (i.Enable && i.Name == DisplayName) 
                    _infoList.Add(i); 
            });

            if (_infoList.Count>0)
            {
                SelectedItem = _infoList[0];
            }
            
            ActiveUpdateData = false;   //关闭界面后不开启后台线程，减少开销

            base.OnInitialize();

        }
        protected override void OnDeactivate(bool close)
        {
            EnableTimer(false);
            base.OnDeactivate(close);
        }
        protected override bool OnTimer()
        {
            //更新数据时机由RT决定
            DateTime handShake = (DateTime)QueryDataClient.Instance.Service.GetData("MaintainManager.ItemHandShake");
            DateTime listChangedHandShake = (DateTime)QueryDataClient.Instance.Service.GetData("MaintainManager.ListChangedHandShake");

            if (_listChangedHandShake < listChangedHandShake)
            {
                _listChangedHandShake = listChangedHandShake;
                List<MaintainerInfo> list = ((List<MaintainerInfo>)QueryDataClient.Instance.Service.GetData($"MaintainManager.MaintainerInfos")).FindAll(i=> i.Enable && i.Name == DisplayName);
                _infoList = list;
                NotifyOfPropertyChange(nameof(InfoList));

                SelectedItem = _infoList.FirstOrDefault();
            }

            if (_handShake < handShake)
            {
                _handShake = handShake;
                return base.OnTimer();
            }

            return true;
        }

        public void SetMaintain(MaintainerItemInfo iteminfo, MaintainerInfo minfo)
        {
            var vm = new MaintainConfirmViewModel(iteminfo, minfo);
            _windowmanager.ShowDialog(vm);
            if (vm.DialogResult == ResultYesNo.YES)
            {
                InvokeClient.Instance.Service.DoOperation("MaintainManager.SetMaintain",
                    minfo.Type,
                    minfo.Name,
                    minfo.Module,
                    iteminfo.Config.ParentName,
                    iteminfo.Config.Name,
                    iteminfo.UID,
                    DateTime.Now,
                    "admintest",
                    vm.Record);
            }
        }

        public void FileOpen(string name)
        {
            InvokeClient.Instance.Service.DoOperation("MaintainManager.OpenFile", name);
        }

        #endregion
    }
}
