using AutoMapper;
using FLY.OBJComponents.Server;
using FLY.Thick.Base.Common;
using FLY.Thick.FilmCasting.IService;
using FLY.Thick.FilmCasting.Server.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FLY.Thick.FilmCasting.Server
{
    public class ScanWarning : INotifyPropertyChanged, IScanWarningService
    {
        private WarningSystem2 mWarning;
        private IBulkDbService mBulkDB;
        private DynArea dynArea;
        /// <summary>
        /// 使能
        /// </summary>
        public bool Enable { get; set; }

        /// <summary>
        /// 目标值，绑定 profileparam.Target
        /// </summary>
        public double Target { get; set; }

        /// <summary>
        /// 产品公差%，绑定 profileparam.Tolerance
        /// </summary>
        public double TolerancePercent { get; set; }

        /// <summary>
        /// 连续N个点，大于报警线才算报警
        /// </summary>
        public int AlarmCnt { get; set; } = 2;

        public double AlarmPercent { get; set; } = 1;

        public int Mix { get; set; } = 6;

        


        public enum CheckResult
        {
            Idle,
            Warning
        }
        public CheckResult Check(double[] data)
        {
            int cnt = 0;
            for (int i = 0; i < data.Length; i++)
            {
                double d = data[i];
                if (double.IsNaN(d))
                {
                    cnt = 0;
                }
                else
                {

                    double delta = Math.Abs(d - Target);
                    double alarm = Target * TolerancePercent * AlarmPercent;

                    if (delta > alarm)
                    {
                        //触发
                        cnt++;
                        if (cnt >= AlarmCnt)
                        {
                            //需要报警
                            return CheckResult.Warning;
                        }
                    }
                    else
                    {
                        cnt = 0;
                    }
                }
            }
            return CheckResult.Idle;
        }
        public void Apply()
        {
            Save();
        }
        public ScanWarning()
        {
            Load();
        }

        public void Init(
            WarningSystem2 warning,
            FLY.Thick.FilmCasting.Common.FilmCastingProfileParam profileParam,
            IBulkDbService bulkDB,
            DynArea dynArea)
        {
            mWarning = warning;
            mBulkDB = bulkDB;
            this.dynArea = dynArea;
            Misc.BindingOperations.SetBinding(profileParam, nameof(profileParam.Target), this, nameof(Target));
            Misc.BindingOperations.SetBinding(profileParam, nameof(profileParam.TolerancePercent), this, nameof(TolerancePercent));


            mBulkDB.PropertyChanged += MBulkDB_PropertyChanged;

        }

        private void MBulkDB_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(mBulkDB.LastScanDataId))
            {
                if (!Enable)//没启动
                    return;

                if (!dynArea.DataValid)//停着，不报警
                    return;

                mBulkDB.GetFrame(
                new Pack_GetFrameRequest()
                {
                    Id = 0,
                    TimeRange = TimeSpan.FromMinutes(5),
                    AvgRange = Target * TolerancePercent * 2,
                    Mix = Mix
                },
                GetFrameCB, null);

            }
        }

        void GetFrameCB(object ayncContext, object retData)
        {
            Pack_GetFrameReponse reponse = retData as Pack_GetFrameReponse;

            CheckResult result;
            Lc_ScanData lc_ScanData = null;
            if (reponse.scanData != null && reponse.scanData.Thicks.Count() > 0)
            {
                lc_ScanData = reponse.scanData;
                result = Check(lc_ScanData.Thicks);
            }
            else
            {
                result = CheckResult.Idle;
            }

            switch (result)
            {
                case CheckResult.Warning:
                    //报警, 
                    //附加了ID信息
                    //就算现在已经报警，也必须刷新
                    mWarning.Update(
                        ERRNOs.Instance.SCAN_ERRNO_OVERTOL.Code,
                        ERRNOs.Instance.SCAN_ERRNO_OVERTOL.Descrption,
                        Newtonsoft.Json.JsonConvert.SerializeObject(new { ScanDataID = lc_ScanData.ID }));
                    break;
                case CheckResult.Idle:
                    {
                        //报警解除
                        mWarning.Remove(
                            ERRNOs.Instance.SCAN_ERRNO_OVERTOL.Code);
                    }
                    break;
            }
        }

        private string file_path = "scanwarning.json";

        bool Load()
        {
            return ScanWarningJsonDb.Load(this, file_path);

        }
        bool Save()
        {
            return ScanWarningJsonDb.Save(this, file_path);
        }


        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
    public class ScanWarningJsonDb
    {
        static Mapper Mapper { get; } = new AutoMapper.Mapper(new MapperConfiguration(c =>
        {
            c.CreateMap<ScanWarning, ScanWarningJsonDb>().ReverseMap();
        }));

        public static bool Load(ScanWarning src, string file_path)
        {
            try
            {
                if (File.Exists(file_path))
                {
                    string json = File.ReadAllText(file_path);
                    var p = JsonConvert.DeserializeObject<ScanWarningJsonDb>(json);
                    Mapper.Map(p, src);
                    return true;
                }
            }
            catch
            {
                //异常，没有json 解码失败
            }

            return false;
        }
        public static bool Save(ScanWarning src, string file_path)
        {
            var p = ScanWarningJsonDb.Mapper.Map<ScanWarningJsonDb>(src);
            try
            {
                File.WriteAllText(file_path, JsonConvert.SerializeObject(p, Formatting.Indented));
                return true;
            }
            catch
            {
                //异常，没有json 编码失败

            }
            return false;

        }
        /// <summary>
        /// 使能
        /// </summary>
        public bool Enable { get; set; }

        /// <summary>
        /// 连续N个点，大于报警线才算报警
        /// </summary>
        public int AlarmCnt { get; set; } = 2;

        public double AlarmPercent { get; set; } = 100;

        public int Mix { get; set; } = 6;

    }


}
