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 : FObj, IRejectService
    {
        IFConn mConn;
        UInt32 mServerID;
        public bool IsConnected;

        public RejectServiceClient(UInt32 id)
        {
            mServerID = id;
            this.PropertyChanged += new PropertyChangedEventHandler(RejectService_PropertyChanged);
        }

        void RejectService_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            
        }

        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 成员
        private bool enable;
        public bool Enable
        {
            get
            {
                return enable;
            }
            set
            {
                if (enable != value) 
                {
                    enable = value;
                    NotifyPropertyChanged("Enable");
                }
            }
        }
        private double threshold_ratio;
        public double ThresholdRatio
        {
            get
            {
                return threshold_ratio;
            }
            set
            {
                if (threshold_ratio != value)
                {
                    threshold_ratio = value;
                    NotifyPropertyChanged("ThresholdRatio");
                }
            }
        }
        private bool ispercent;
        public bool IsPercent
        {
            get
            {
                return ispercent;
            }
            set
            {
                if (ispercent != value)
                {
                    ispercent = value;
                    NotifyPropertyChanged("IsPercent");
                }
            }
        }
        private double psigma;
        public double PSigma
        {
            get
            {
                return psigma;
            }
            set
            {
                if (psigma != value)
                {
                    psigma = value;
                    NotifyPropertyChanged("PSigma");
                }
            }
        }
        private int sigma;
        public int Sigma
        {
            get
            {
                return sigma;
            }
            set
            {
                if (sigma != value)
                {
                    sigma = value;
                    NotifyPropertyChanged("Sigma");
                }
            }
        }
        private int range1;
        public int Range1
        {
            get
            {
                return range1;
            }
            set
            {
                if (range1 != value)
                {
                    range1 = value;
                    NotifyPropertyChanged("Range1");
                }
            }
        }
        private int range2;
        public int Range2
        {
            get
            {
                return range2;
            }
            set
            {
                if (range2 != value)
                {
                    range2 = value;
                    NotifyPropertyChanged("Range2");
                }
            }
        }
        private int[] filterdatas;
        public int[] FilterDatas
        {
            get
            {
                return filterdatas;
            }
            set
            {
                if (filterdatas != value)
                {
                    filterdatas = value;
                    NotifyPropertyChanged("FilterDatas");
                }
            }
        }
        private int[] rejectdatas;
        public int[] RejectDatas
        {
            get
            {
                return rejectdatas;
            }
            set
            {
                if (rejectdatas != value)
                {
                    rejectdatas = value;
                    NotifyPropertyChanged("RejectDatas");
                }
            }
        }

        private int[] sigmadatas;
        public int[] SigmaDatas
        {
            get
            {
                return sigmadatas;
            }
            set
            {
                if (sigmadatas != value)
                {
                    sigmadatas = value;
                    NotifyPropertyChanged("SigmaDatas");
                }
            }
        }
        
        private int posofgrid=10;
        public int PosOfGrid
        {
            get
            {
                return posofgrid;
            }
            set
            {
                if (posofgrid != value)
                {
                    posofgrid = value;
                    NotifyPropertyChanged("PosOfGrid");
                }
            }
        }
        private int poslen=8900;
        public int PosLen
        {
            get
            {
                return poslen;
            }
            set
            {
                if (poslen != value)
                {
                    poslen = value;
                    NotifyPropertyChanged("PosLen");
                }
            }
        }
        private int target;
        public int Target {
            get {
                return target;
            }
            set {
                if (target != value) 
                {
                    target = value;
                    NotifyPropertyChanged("Target");
                }
            }
        }
        #endregion
        #region INotifyPropertyChanged 成员
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        public override void Dispose()
        {
            CurrObjSys.ObjRemove(
                this, mConn);
        }
        public override void ConnectNotify(IFConn from)
        {
            mConn = from;
            IsConnected = from.IsConnected;
            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);
        }
    }
}
