using CommunityToolkit.Mvvm.Input;
using FLY.OBJComponents.IService;
using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows.Threading;
using Unity;

namespace FLY.Thick.Base.UI
{
    class CtMicroGageVm : INotifyPropertyChanged
    {
        #region 延时推送 MARKNO
        const int MARKNO_UPDATEERROR = 1;
        #endregion

        public event PropertyChangedEventHandler PropertyChanged;


        public double Thick { get; set; }
        public int AD { get; set; }
        public int ADMax { get; set; }
        public int Position { get; set; }
        public int PosLength { get; set; }

        public double PosMm { get; set; }
        public double Velocity { get; set; }
        public double FilmVelocity { get; set; }
        public string ControllerState { get; set; }

        public UInt16 OStatus { get; set; }
        public UInt16 IStatus { get; set; }

        public bool IsError { get; set; }
        /// <summary>
        /// 异常消息
        /// </summary>
        public string ErrMsg { get; set; }

        public RelayCommand StopCmd { get; private set; }

        public RelayCommand ForwCmd { get; private set; }

        public RelayCommand BackwCmd { get; private set; }

        public RelayCommand OrgCmd { get; private set; }


        DynArea dynArea;
        ITDGageService gageService;
        IInitParamService initParam;
        IWarningSystem2Service warningSystem;
        DispatcherTimer timer_error;
        private int reason_list_index = -1;

        public CtMicroGageVm()
        {
            StopCmd = new RelayCommand(() =>
            {
                gageService.StartP2(STARTP2_MODE.STOP);
            });
            OrgCmd = new RelayCommand(() =>
            {
                gageService.StartP2(STARTP2_MODE.ORG);
            });
            ForwCmd = new RelayCommand(() =>
            {
                gageService.StartP2(STARTP2_MODE.FORW);
            });
            BackwCmd = new RelayCommand(() =>
            {
                gageService.StartP2(STARTP2_MODE.BACKW);
            });
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initParam"></param>
        /// <param name="gageService"></param>
        /// <param name="warningSystem"></param>
        [InjectionMethod]
        public void Init(
            IInitParamService initParam,
            ITDGageService gageService,
            IWarningSystem2Service warningSystem)
        {

            this.gageService = gageService;
            dynArea = gageService.DynArea;
            this.initParam = initParam;
            this.warningSystem = warningSystem;

            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.Thk), this, nameof(Thick));
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.AD), this, nameof(AD));
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.ADMax), this, nameof(ADMax));
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.Position), this, nameof(Position));
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.PosMm), this, nameof(PosMm));
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.Velocity), this, nameof(Velocity));
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.FilmVelocity), this, nameof(FilmVelocity));
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.ControllerState), () =>
            {
                FLY.Thick.Base.UI.Converter.ControllerStateConverter converter = new FLY.Thick.Base.UI.Converter.ControllerStateConverter();
                ControllerState = (string)converter.Convert(dynArea.ControllerState, typeof(string), null, null);
            });

            Misc.BindingOperations.SetBinding(this.initParam, nameof(CtMicroGageVm.initParam.PosLength), this, nameof(PosLength));
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.OStatus), this, nameof(OStatus));
            Misc.BindingOperations.SetBinding(dynArea, nameof(dynArea.IStatus), this, nameof(IStatus));


            //报警原因轮流显示
            timer_error = new DispatcherTimer();
            timer_error.Interval = TimeSpan.FromSeconds(3);
            timer_error.Tick += (s, e) =>
            {
                reason_list_index--;
                if (reason_list_index < 0)
                {
                    if (warningSystem.ReasonList != null && warningSystem.ReasonList.Count() > 0)
                        reason_list_index = warningSystem.ReasonList.Count() - 1;
                    else
                        reason_list_index = -1;
                }
                UpdateError();
            };
            warningSystem.PropertyChanged += WarningSystem_PropertyChanged;

            if (this.gageService is FObjBase.FObjServiceClient)
            {
                var client = this.gageService as FObjBase.FObjServiceClient;
                client.PropertyChanged += (s, e) =>
                {
                    if (e.PropertyName == nameof(FObjBase.FObjServiceClient.IsConnected))
                    {
                        UpdateError();
                    }
                };
            }

            UpdateError();


        }

        private void WarningSystem_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(warningSystem.ReasonList))
            {
                if (warningSystem.ReasonList != null && warningSystem.ReasonList.Count() > 0)
                    reason_list_index = warningSystem.ReasonList.Count() - 1;
                else
                    reason_list_index = -1;

                UpdateError();
            }
        }

        void UpdateError()
        {
            if (this.gageService is FObjBase.FObjServiceClient)
            {
                var client = this.gageService as FObjBase.FObjServiceClient;
                if (!client.IsConnected)
                {
                    ErrMsg = "服务器连接断开";
                    IsError = true;
                    reason_list_index = -1;
                    timer_error.Stop();
                    return;
                }
            }

            if (warningSystem.ReasonList != null && warningSystem.ReasonList.Count() > 0)
            {
                if (reason_list_index >= warningSystem.ReasonList.Count())
                    reason_list_index = warningSystem.ReasonList.Count() - 1;
                else if (reason_list_index < 0)
                    reason_list_index = 0;


                ErrMsg = warningSystem.ReasonList[reason_list_index].Description;
                IsError = true;
                timer_error.Start();
            }
            else
            {
                IsError = false;
                ErrMsg = "";
                reason_list_index = -1;
                timer_error.Stop();
            }
        }
    }
}
