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

namespace FLY.Thick.Base.Server.OBJProxy
{
    public class Reject_OBJProxy : FObj
    {
        #region 延时推送 MARKNO
        const int MARKNO_PUSH_PARAMS = 1;
        const int MARKNO_PUSH_POSINFO = 2;
        const int MARKNO_PUSH_FILTERDATAS = 3;
        const int MARKNO_PUSH_REJECTDATAS= 4;
        const int MARKNO_PUSH_SIGMADATAS = 5;
        #endregion

        IRejectService mRejectService;
        public Reject_OBJProxy(int objsys_idx, UInt32 id, IRejectService rejectService) :base(objsys_idx)
        {
            mRejectService = rejectService;

            ID = id;
            
            mRejectService.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(mRejectService_PropertyChanged);
        }
        public Reject_OBJProxy(int objsys_idx, IRejectService rejectService,UInt32 id)
            : base(objsys_idx)
        {
            mRejectService = rejectService;

            ID = id;

            mRejectService.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(mRejectService_PropertyChanged);
        }
        void mRejectService_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "Enable") ||
                (e.PropertyName == "ThresholdRatio")||
                (e.PropertyName == "IsPercent")||
                (e.PropertyName == "PSigma")||
                (e.PropertyName == "Sigma")||
                (e.PropertyName == "Range1")||
                (e.PropertyName == "Range2"))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, REJECT_OBJ_INTERFACE.GET_PARAMS, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, REJECT_OBJ_INTERFACE.PUSH_PARAMS,
                            buf);
                    }), this, MARKNO_PUSH_PARAMS);
            }
            else if((e.PropertyName == "PosOfGrid")||
                (e.PropertyName == "PosLen")||
                (e.PropertyName == "Target"))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, REJECT_OBJ_INTERFACE.GET_POSINFO, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, REJECT_OBJ_INTERFACE.PUSH_POSINFO,
                            buf);
                    }), this, MARKNO_PUSH_POSINFO);
            }
            else if (e.PropertyName == "FilterDatas")
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, REJECT_OBJ_INTERFACE.GET_FILTERDATAS, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, REJECT_OBJ_INTERFACE.PUSH_FILTERDATAS,
                            buf);
                    }), this, MARKNO_PUSH_FILTERDATAS);
            }
            else if (e.PropertyName == "RejectDatas")
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, REJECT_OBJ_INTERFACE.GET_REJECTDATAS, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, REJECT_OBJ_INTERFACE.PUSH_REJECTDATAS,
                            buf);
                    }), this, MARKNO_PUSH_REJECTDATAS);
            }
            else if (e.PropertyName == "SigmaDatas")
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, REJECT_OBJ_INTERFACE.GET_SIGMADATAS, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, REJECT_OBJ_INTERFACE.PUSH_SIGMADATAS,
                            buf);
                    }), this, MARKNO_PUSH_SIGMADATAS);
            }
        }

        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            switch (memid)
            {
                case  REJECT_OBJ_INTERFACE.GET_PARAMS:
                    {
                        REJECT_OBJ_INTERFACE.Pack_Params p = new REJECT_OBJ_INTERFACE.Pack_Params()
                        {
                            enable = mRejectService.Enable,
                            threshold_ratio = mRejectService.ThresholdRatio,
                            ispercent = mRejectService.IsPercent,
                            sigma = mRejectService.Sigma,
                            psigma = mRejectService.PSigma,
                            range1 = mRejectService.Range1,
                            range2 = mRejectService.Range2
                        };
                        infodata = p.ToBytes();
                    } break;
                case REJECT_OBJ_INTERFACE.GET_POSINFO:
                    {
                        REJECT_OBJ_INTERFACE.Pack_PosInfo p = new REJECT_OBJ_INTERFACE.Pack_PosInfo()
                        {
                            posofgrid = mRejectService.PosOfGrid,
                            poslen = mRejectService.PosLen,
                            target = mRejectService.Target
                        };
                        infodata = p.ToBytes();
                    }break;
                case REJECT_OBJ_INTERFACE.GET_FILTERDATAS:
                    {
                        REJECT_OBJ_INTERFACE.Pack_Data p = new REJECT_OBJ_INTERFACE.Pack_Data()
                        {
                            data = mRejectService.FilterDatas
                        };
                        infodata = p.ToBytes();
                    }break;
                case REJECT_OBJ_INTERFACE.GET_REJECTDATAS:
                    {
                        REJECT_OBJ_INTERFACE.Pack_Data p = new REJECT_OBJ_INTERFACE.Pack_Data()
                        {
                            data = mRejectService.RejectDatas
                        };
                        infodata = p.ToBytes();
                    } break;
                case REJECT_OBJ_INTERFACE.GET_SIGMADATAS:
                    {
                        REJECT_OBJ_INTERFACE.Pack_Data p = new REJECT_OBJ_INTERFACE.Pack_Data()
                        {
                            data = mRejectService.SigmaDatas
                        };
                        infodata = p.ToBytes();
                    } break;
                default:
                    infodata = null;
                    break;
            }
        }
        public override void SetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case REJECT_OBJ_INTERFACE.SET_PARAMS:
                    {
                        REJECT_OBJ_INTERFACE.Pack_Params p = new REJECT_OBJ_INTERFACE.Pack_Params();
                        if(p.TryParse(infodata))
                        {

                            mRejectService.Enable = p.enable;
                            mRejectService.ThresholdRatio = p.threshold_ratio;
                            mRejectService.PSigma = p.psigma;
                            mRejectService.Sigma = p.sigma;
                            mRejectService.IsPercent = p.ispercent;
                            mRejectService.Range1 = p.range1;
                            mRejectService.Range2 = p.range2;
                            mRejectService.Apply();
                        }
                    } break;
            }
        }
    }
}
