using FLY.OBJComponents.IService;
using MultiLayout.UiModule; 
using Misc;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using FLY.Thick.Base.UI;
using Unity;
using FLY.Thick.Base.Common;

namespace FLY.Thick.Base.UI.OnInit
{

    /// <summary>
    /// ERRNOs.Instance.SCAN_ERRNO_OVERCTRL
    /// ERRNOs.Instance.SCAN_ERRNO_OVERTOL
    /// 扫描报警时，全屏显示
    /// </summary>
    public class OnInitError : IOnInit
    {
        #region 延时推送 MARKNO
        const int MARKNO_WARNING_RING = 1;
        #endregion

        FLY.OBJComponents.IService.IWarningSystem2Service warningService;
        ParamDictionary paramDictionary;
        IUnityContainer container;

        bool enableScanErrBigTip;
        int warningTipId=-1;
        public int Level { get; }
        public OnInitError(
            IUnityContainer container,
            ParamDictionary paramDictionary, 
            FLY.OBJComponents.IService.IWarningSystem2Service warningService,
            int lv = 1) 
        {
            Level = lv;
            this.container = container;
            this.paramDictionary = paramDictionary;
            this.warningService = warningService;

        }
        public void OnInit()
        {
            paramDictionary.ValueChanged += ParamDictionary_ValueChanged;

            warningService.PropertyChanged += WarningService_PropertyChanged;

            updateErrCodesOfTip();


        }
    

        private void WarningService_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(warningService.ReasonList)) {
                if (!enableScanErrBigTip)
                    return;

                FObjBase.PollModule.Current.Poll_JustOnce(
                    new FObjBase.PollModule.PollHandler(delegate ()
                    {
                        List<int> errCodes = new List<int> { ERRNOs.Instance.SCAN_ERRNO_OVERCTRL.Code, ERRNOs.Instance.SCAN_ERRNO_OVERTOL.Code };

                        var record = warningService.ReasonList;
                        if (record == null)
                        {
                            //什么报警都没有
                            //不需要提示
                            if (warningTipId != -1)
                            {
                                //需要把之前的报警关闭
                                FLY.ControlLibrary.Window_WarningTip.Stop(warningTipId);
                                warningTipId = -1;
                            }
                            return;
                        }

                        var rs = from r in record where errCodes.Contains(r.ErrCode) select r;
                        if (rs.Count() == 0)
                        {
                            //不需要提示
                            if (warningTipId != -1)
                            {
                                //需要把之前的报警关闭
                                FLY.ControlLibrary.Window_WarningTip.Stop(warningTipId);
                                warningTipId = -1;
                            }
                            return;
                        }
  
                        int errcode = rs.First().ErrCode;
                        string msg = rs.First().Description;

                        string path = paramDictionary.GetValue<string>(ParamDistItemKeys.WarningTipPath);

                        //报警！！！！！！！！
                        //记录打开的报警提示框Id，用于以后关闭
                        warningTipId = FLY.ControlLibrary.Window_WarningTip.Show(
                                $"编码={errcode}",
                                msg,
                                TimeSpan.MaxValue,
                                path,
                                warningService.Reset
                                );
                        
                    }), this, MARKNO_WARNING_RING);
            }
        }

        private void ParamDictionary_ValueChanged(object sender, ParamDictionaryValueChangedEventArgs e)
        {
            if (e.Key == ParamDistItemKeys.EnableScanErrBigTip)
            {
                updateErrCodesOfTip();
            }
        }


        void updateErrCodesOfTip()
        {
            enableScanErrBigTip = paramDictionary.GetValue(ParamDistItemKeys.EnableScanErrBigTip, false);

        }
    }
}
