using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel;

using FObjBase;
using FLY.Thick.Base.IService;
using FLY.Thick.Base.OBJ_INTERFACE;
using FLY.Thick.Base.Common;

namespace FLY.Thick.Base.Client
{
    public class RejectServiceClient : FObjServiceClient, IRejectService
    {
        public RejectServiceClient(UInt32 id) : base(id) 
        {
        }
        public RejectServiceClient(UInt32 serviceId, string connName) : base(serviceId, connName) { }

        public void Apply() 
        {
            REJECT_OBJ_INTERFACE.Pack_Params p = new REJECT_OBJ_INTERFACE.Pack_Params()
            {
                enable = Enable,
                threshold_ratio = ThresholdRatio,
                ispercent = IsPercent,
                psigma = PSigma,
                sigma = Sigma,
                range1 = Range1,
                range2 = Range2
            };
            CurrObjSys.SetValueEx(
                mConn, mServerID, ID, 
                REJECT_OBJ_INTERFACE.SET_PARAMS, p.ToBytes());
                
        }
        #region IRejectService 成员

        public bool Enable { get; set; }
        public double ThresholdRatio { get; set; }

        public bool IsPercent { get; set; }

        public double PSigma { get; set; }


        public int Sigma { get; set; }

        public int Range1 { get; set; }


        public int Range2 { get; set; }

        [PropertyChanged.DoNotCheckEquality]
        public int[] FilterDatas { get; set; }

        [PropertyChanged.DoNotCheckEquality]
        public int[] RejectDatas { get; set; }


        [PropertyChanged.DoNotCheckEquality]
        public int[] SigmaDatas { get; set; }

        
        public int PosOfGrid { get; set; } = 10;

        private int poslen=8900;
        public int PosLen { get; set; } = 8900;

        public int Target { get; set; }

        #endregion



        public override void ConnectNotify(IFConn from)
        {
            base.ConnectNotify(from);
            if (from.IsConnected)
            {
                //获取所有数据，设置推送
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID, 
                    REJECT_OBJ_INTERFACE.GET_PARAMS);

                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID, 
                    REJECT_OBJ_INTERFACE.GET_POSINFO);

                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID, 
                    REJECT_OBJ_INTERFACE.GET_FILTERDATAS);

                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID, 
                    REJECT_OBJ_INTERFACE.GET_REJECTDATAS);

                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID, 
                    REJECT_OBJ_INTERFACE.GET_SIGMADATAS);

                CurrObjSys.SenseConfigEx(
                    mConn, mServerID, ID, 0xffffffff,
                    SENSE_CONFIG.ADD);
            }
        }
        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case REJECT_OBJ_INTERFACE.GET_PARAMS:
                    {
                        REJECT_OBJ_INTERFACE.Pack_Params p = new REJECT_OBJ_INTERFACE.Pack_Params();
                        if (p.TryParse(infodata))
                        {
                            Enable = p.enable;
                            ThresholdRatio = p.threshold_ratio;
                            IsPercent = p.ispercent;
                            PSigma = p.psigma;
                            Sigma = p.sigma;
                            Range1 = p.range1;
                            Range2 = p.range2;
                        }
                    } break;
                case REJECT_OBJ_INTERFACE.GET_POSINFO:
                    {
                        REJECT_OBJ_INTERFACE.Pack_PosInfo p = new REJECT_OBJ_INTERFACE.Pack_PosInfo();
                        if (p.TryParse(infodata))
                        {
                            PosOfGrid = p.posofgrid;
                            PosLen = p.poslen;
                            Target = p.target;
                        }
                    } break;
                case REJECT_OBJ_INTERFACE.GET_FILTERDATAS:
                    {
                        REJECT_OBJ_INTERFACE.Pack_Data p = new REJECT_OBJ_INTERFACE.Pack_Data();
                        if (p.TryParse(infodata))
                        {
                            FilterDatas = (int[])p.data.Clone();
                        }
                    } break;
                case REJECT_OBJ_INTERFACE.GET_REJECTDATAS:
                    {
                        REJECT_OBJ_INTERFACE.Pack_Data p = new REJECT_OBJ_INTERFACE.Pack_Data();
                        if (p.TryParse(infodata))
                        {
                            RejectDatas = (int[])p.data.Clone();
                        }
                    } break;
                case REJECT_OBJ_INTERFACE.GET_SIGMADATAS:
                    {
                        REJECT_OBJ_INTERFACE.Pack_Data p = new REJECT_OBJ_INTERFACE.Pack_Data();
                        if (p.TryParse(infodata))
                        {
                            SigmaDatas = (int[])p.data.Clone();
                        }
                    } break;
            }
        }
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            PushGetValue(from, srcid, infoid, infodata);
        }
    }
}
