using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Misc;
using FObjBase;
using FLY.Thick.Base.IService;
using FLY.Thick.Base.OBJ_INTERFACE;
using FLY.Thick.Base.Common;

namespace FLY.Thick.Base.Client
{
    /// <summary>
    /// 边界查找服务 客户代理
    /// </summary>
    public class BorderSearchServiceClient: FObjServiceClient, IBorderSearchService
    {
        /// <summary>
        /// 边界查找服务 构造
        /// </summary>
        /// <param name="id"></param>
        public BorderSearchServiceClient(UInt32 id) : base(id) { }

        /// <summary>
        /// 边界查找服务 构造
        /// </summary>
        /// <param name="serviceId">服务id</param>
        /// <param name="connName">连接器</param>
        public BorderSearchServiceClient(UInt32 serviceId, string connName) : base(serviceId, connName) { }

        #region IBorderSearchService 成员

        public bool Enable { get; set; }


        /// <summary>
        /// 边界拐点检测，找到的边界更加精确
        /// </summary>
        public bool IsBreakDetect { get; set; } = true;

        [PropertyChanged.DoNotCheckEquality]
        public Range Valid { get; set; } = new Range();
        [PropertyChanged.DoNotCheckEquality]
        public Range Border_Backw { get; private set; } = new Range();
        [PropertyChanged.DoNotCheckEquality]
        public Range Border_Forw { get; private set; } = new Range();
        [PropertyChanged.DoNotCheckEquality]
        public Range Border { get; private set; } = new Range();


        /// <summary>
        /// 当前膜宽，测量出来的，单位是 脉冲
        /// </summary>
        public int Width { get; protected set; }


        /// <summary>
        /// 膜中间位置 单位 脉冲
        /// </summary>
        public int Mid { get; protected set; }


        /// <summary>
        /// 手动设置温修AD值
        /// </summary>
        public bool TempADBySet { get; set; }

        public int TempAD { get; set; }

        public int TempRange { get; set; }

        /// <summary>
        /// 温修范围是温修的百分比
        /// </summary>
        public bool IsTempRangeByPercent { get; set; } = true;

        /// <summary>
        ///  温修范围百分比
        /// </summary>
        public double TempRangePercent { get; set; } = 0.02;


        public int N { get; set; }

        /// <summary>
        /// 探头直径，单位脉冲， 膜宽 = 边界范围 - 探头直径
        /// </summary>
        public int SensorWidth { get; set; }

        public int N2 { get; set; }


        public int N3 { get; set; }

        /// <summary>
        /// 用于调试， 进入到边界查找的数据
        /// </summary>
        [PropertyChanged.DoNotCheckEquality]
        public int[] Datas { get; protected set; } = null;

        #endregion


        public void Apply() 
        {
            var p = new BORDERSEARCH_OBJ_INTERFACE.Pack_Params();
            p.enable = Enable;
            p.tempADBySet = TempADBySet;
            p.tempad = TempAD;
            p.temprange = TempRange;
            p.n = N;
            p.n2 = N2;
            p.n3 = N3;
            p.valid = Valid;
            p.sensorwidth = SensorWidth;
            p.isBreakDetect = IsBreakDetect;
            p.isTempRangeByPercent = IsTempRangeByPercent;
            p.tempRangePercent = TempRangePercent;
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(p);

            //获取所有数据，设置推送
            CurrObjSys.SetValueEx(
                mConn, mServerID, ID, 
                BORDERSEARCH_OBJ_INTERFACE.SET_PARAMS,
                Misc.Converter.StringToBytes(json));
        }

        public override void ConnectNotify(IFConn from)
        {
            base.ConnectNotify(from);
            if (from.IsConnected)
            {
                //获取所有数据，设置推送
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID, 
                    BORDERSEARCH_OBJ_INTERFACE.GET_PARAMS);
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID, 
                    BORDERSEARCH_OBJ_INTERFACE.GET_RESULT);
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID,
                    BORDERSEARCH_OBJ_INTERFACE.GET_DATAS);
                CurrObjSys.SenseConfigEx(
                    mConn, mServerID, ID, 0xffffffff, SENSE_CONFIG.ADD);
            }
        }
        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case BORDERSEARCH_OBJ_INTERFACE.GET_PARAMS:
                    {
                        string json = Misc.Converter.BytesToString(infodata);

                        var p = Newtonsoft.Json.JsonConvert.DeserializeObject<BORDERSEARCH_OBJ_INTERFACE.Pack_Params>(json);
                        
                        Enable = p.enable;
                        Valid = p.valid;
                        TempADBySet = p.tempADBySet;
                        TempAD = p.tempad;
                        TempRange = p.temprange;
                        N=p.n;
                        SensorWidth = p.sensorwidth;
                        N2=p.n2;
                        N3=p.n3;
                        IsBreakDetect = p.isBreakDetect;
                        IsTempRangeByPercent = p.isTempRangeByPercent;
                        TempRangePercent = p.tempRangePercent;
                    } break;
                case BORDERSEARCH_OBJ_INTERFACE.GET_RESULT:
                    {

                        string json = Misc.Converter.BytesToString(infodata);

                        var p = Newtonsoft.Json.JsonConvert.DeserializeObject<BORDERSEARCH_OBJ_INTERFACE.Pack_Result>(json);
                        Border_Forw = p.borderForw;
                        Border_Backw = p.borderBackw;
                        Border = p.border;
                        Width = p.width;
                        Mid = p.mid;
                    } break;
                case BORDERSEARCH_OBJ_INTERFACE.GET_DATAS:
                    {

                        string json = Misc.Converter.BytesToString(infodata);

                        var p = Newtonsoft.Json.JsonConvert.DeserializeObject<BORDERSEARCH_OBJ_INTERFACE.Pack_Data>(json);

                        Datas = p.data;
                    }
                    break;
            }
        }
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            PushGetValue(from, srcid, infoid, infodata);
        }


    }
}
