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
{
    public class OnInitError : IOnInit
    {
        #region 延时推送 MARKNO
        const int MARKNO_WARNING_RING = 1;
        #endregion

        IWarningService warningService;
        WarningReasonWindow warningReasonWindow;
        ParamDictionary paramDictionary;
        IUnityContainer container;

        bool enableScanErrBigTip;
        int warningTipId=-1;
        public int Level { get; }
        public OnInitError(
            IUnityContainer container,
            ParamDictionary paramDictionary, 
            WarningReasonWindow warningReasonWindow,
            IWarningService warningService,
            int lv = 1) 
        {
            Level = lv;
            this.container = container;
            this.paramDictionary = paramDictionary;
            this.warningReasonWindow = warningReasonWindow;
            this.warningService = warningService;
        }
        public void OnInit()
        {
            paramDictionary.ValueChanged += ParamDictionary_ValueChanged;

            warningReasonWindow.Record.CollectionChanged += Record_CollectionChanged;

            updateErrCodesOfTip();
        }

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


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

        }



        private void Record_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (!enableScanErrBigTip)
                return;

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

                    var record = warningReasonWindow.Record;
                    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;
                        }
                    }
                    else { 
                        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);
        }
    }
}
