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;

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;

        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;
        }

        private void ParamDictionary_ValueChanged(object sender, ParamDictionaryValueChangedEventArgs e)
        {
            if (e.Key == ParamDistItemKeys.ErrCodesOfTip)
            {
                updateErrCodesOfTip();
            }
        }
        List<int> errCodes = new List<int>();



        void updateErrCodesOfTip()
        {
            errCodes.Clear();
            string str = paramDictionary.GetValue<string>(ParamDistItemKeys.ErrCodesOfTip);
            if (string.IsNullOrEmpty(str))
                return;

            int[] codes = null;
            try
            {
                codes = Newtonsoft.Json.JsonConvert.DeserializeObject<int[]>($"[{str}]");
            }
            catch (Exception ex)
            {
                return;
            }

            if (codes == null)
                return;
            if (codes.Count() == 0)
                return;
            errCodes.AddRange(codes);
        }



        private void Record_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (errCodes.Count() == 0)
                return;

            FObjBase.PollModule.Current.Poll_JustOnce(
                new FObjBase.PollModule.PollHandler(delegate ()
                {
                    var record = warningReasonWindow.Record;
                    //判断当前报警是否包含 errCodes
                    var rs = from r in record where errCodes.Contains(r.ErrCode) select r;
                    if (rs.Count() == 0)
                    {//没有一个需要报警
                        FLY.ControlLibrary.Window_WarningTip.Stop();
                    }
                    else
                    {
                        int errcode = rs.First().ErrCode;
                        string msg = rs.First().Description;

                        string path = paramDictionary.GetValue<string>(ParamDistItemKeys.WarningTipPath);
                        
                            //报警！！！！！！！！
                            FLY.ControlLibrary.Window_WarningTip.Show(
                                $"编码={errcode}",
                                msg,
                                TimeSpan.MaxValue,
                                path,
                                warningService.Reset
                                );
                        
                    }
                }), this, MARKNO_WARNING_RING);
        }
    }
}
