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;
using FLY.Thick.Base.Server;
using Newtonsoft.Json;

namespace FLY.Thick.Base.Server.OBJProxy
{
    public class ScanCorr_OBJProxy : FObj
    {
        #region 延时推送 MARKNO
        const int MARKNO_PUSH_GROUP_PARAMS = 1;
        const int MARKNO_PUSH_GROUP0_FORW = 2;
        const int MARKNO_PUSH_GROUP0_BACKW = 3;
        const int MARKNO_PUSH_GROUP1_FORW = 4;
        const int MARKNO_PUSH_GROUP1_BACKW = 5;
        const int MARKNO_PUSH_PROGRESS = 6;
        const int MARKNO_PUSH_START_PARAMS = 7;
        const int MARKNO_PUSH_ENABLE = 8;
        const int MARKNO_PUSH_VALIDRANGE = 9;
        #endregion

        IScanCorrService mScanCorr;

        public ScanCorr_OBJProxy(int objsys_idx, UInt32 id, IScanCorrService scancorr)
            : base(objsys_idx)
        {
            ID = id;
            mScanCorr = scancorr;

            mScanCorr.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(mScanCorr_PropertyChanged);
            mScanCorr.Group0.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Group0_PropertyChanged);
            mScanCorr.Group1.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Group1_PropertyChanged);
            
        }

        void Group0_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "AvgAD") || (e.PropertyName == "DataReady"))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, SCANCORR_OBJ_INTERFACE.GET_GROUP_PARAMS, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, SCANCORR_OBJ_INTERFACE.PUSH_GROUP_PARAMS,
                            buf);
                    }), this, MARKNO_PUSH_GROUP_PARAMS);
            }
            else if (e.PropertyName == "ForwDatas") 
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, SCANCORR_OBJ_INTERFACE.GET_GROUP0_FORW, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, SCANCORR_OBJ_INTERFACE.PUSH_GROUP0_FORW,
                            buf);
                    }), this, MARKNO_PUSH_GROUP0_FORW);
            }
            else if (e.PropertyName == "BackwDatas") 
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, SCANCORR_OBJ_INTERFACE.GET_GROUP0_BACKW, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, SCANCORR_OBJ_INTERFACE.PUSH_GROUP0_BACKW,
                            buf);
                    }), this, MARKNO_PUSH_GROUP0_BACKW);
            }
        }
        void Group1_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "AvgAD") || (e.PropertyName == "DataReady"))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, SCANCORR_OBJ_INTERFACE.GET_GROUP_PARAMS, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, SCANCORR_OBJ_INTERFACE.PUSH_GROUP_PARAMS,
                            buf);
                    }), this, MARKNO_PUSH_GROUP_PARAMS);
            }
            else if (e.PropertyName == "ForwDatas")
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, SCANCORR_OBJ_INTERFACE.GET_GROUP1_FORW, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, SCANCORR_OBJ_INTERFACE.PUSH_GROUP1_FORW,
                            buf);
                    }), this, MARKNO_PUSH_GROUP1_FORW);
            }
            else if (e.PropertyName == "BackwDatas")
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, SCANCORR_OBJ_INTERFACE.GET_GROUP1_BACKW, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, SCANCORR_OBJ_INTERFACE.PUSH_GROUP1_BACKW,
                            buf);
                    }), this, MARKNO_PUSH_GROUP1_BACKW);
            }
        }

        void mScanCorr_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "Progress") || (e.PropertyName == "Ing"))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, SCANCORR_OBJ_INTERFACE.GET_PROGRESS, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, SCANCORR_OBJ_INTERFACE.PUSH_PROGRESS,
                            buf);
                    }), this, MARKNO_PUSH_PROGRESS);
            }
            else if ((e.PropertyName == "ScanTimer") || (e.PropertyName == "SmoothFactor")|| (e.PropertyName == "PosLen")||(e.PropertyName == "PosOfGrid")) 
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, SCANCORR_OBJ_INTERFACE.GET_START_PARAMS, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, SCANCORR_OBJ_INTERFACE.PUSH_START_PARAMS,
                            buf);
                    }), this, MARKNO_PUSH_START_PARAMS);
            }
            else if (e.PropertyName == "Enable") 
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, SCANCORR_OBJ_INTERFACE.GET_ENABLE, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, SCANCORR_OBJ_INTERFACE.PUSH_ENABLE,
                            buf);
                    }), this, MARKNO_PUSH_ENABLE);
            }
            else if ((e.PropertyName == "ValidBegin")||
                (e.PropertyName == "ValidEnd"))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                    {
                        byte[] buf;
                        GetValue(null, 0, SCANCORR_OBJ_INTERFACE.GET_VALIDRANGE, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, SCANCORR_OBJ_INTERFACE.PUSH_VALIDRANGE,
                            buf);
                    }), this, MARKNO_PUSH_VALIDRANGE);
            }
        }


        public override void CallFunction(IFConn from, uint srcid,UInt32 magic, ushort funcid, byte[] infodata)
        {
            switch (funcid)
            {
                case SCANCORR_OBJ_INTERFACE.CALL_START:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        var p = JsonConvert.DeserializeObject<SCANCORR_OBJ_INTERFACE.Pack_CallStart>(json);
                        mScanCorr.SmoothFactor = p.smoothfactor;
                        mScanCorr.ScanTimer = p.scantimer;
                        mScanCorr.Start(p.groupindex);
                    } break;
                case SCANCORR_OBJ_INTERFACE.CALL_CLEAR:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        var p = JsonConvert.DeserializeObject<SCANCORR_OBJ_INTERFACE.Pack_ClearRequest>(json);
                        mScanCorr.Clear(p.groupindex);
                    } break;
                case SCANCORR_OBJ_INTERFACE.CALL_SMOOTH:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        var p = JsonConvert.DeserializeObject<SCANCORR_OBJ_INTERFACE.Pack_CallStart>(json);
                        mScanCorr.SmoothFactor = p.smoothfactor;
                        mScanCorr.Smooth(p.groupindex);
                    } break;
                default:
                    infodata = null;
                    break;
            }
        }
        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            switch (memid)
            {
                case SCANCORR_OBJ_INTERFACE.GET_PROGRESS:
                    {
                        SCANCORR_OBJ_INTERFACE.Pack_GetState p = new SCANCORR_OBJ_INTERFACE.Pack_GetState();
                        p.progress = mScanCorr.Progress;
                        p.ing = mScanCorr.Ing;
                        string json = JsonConvert.SerializeObject(p);

                        infodata = Misc.Converter.StringToBytes(json);
                    } break;
                case SCANCORR_OBJ_INTERFACE.GET_ENABLE:
                    {
                        string json = JsonConvert.SerializeObject(mScanCorr.Enable);
                        infodata = Misc.Converter.StringToBytes(json);
                    } break;
                case SCANCORR_OBJ_INTERFACE.GET_START_PARAMS:
                    {
                        SCANCORR_OBJ_INTERFACE.Pack_StartParams p = new SCANCORR_OBJ_INTERFACE.Pack_StartParams();
                        p.scantimer = mScanCorr.ScanTimer;
                        p.smoothfactor = mScanCorr.SmoothFactor;
                        p.posofgrid = mScanCorr.PosOfGrid;
                        p.poslen = mScanCorr.PosLen;
                        string json = JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    } break;
                case SCANCORR_OBJ_INTERFACE.GET_GROUP_PARAMS:
                    {
                        SCANCORR_OBJ_INTERFACE.Pack_GroupParams p = new SCANCORR_OBJ_INTERFACE.Pack_GroupParams();
                        p.avgad0 = mScanCorr.Group0.AvgAD;
                        p.avgad1 = mScanCorr.Group1.AvgAD;
                        string json = JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    }
                    break;
                case SCANCORR_OBJ_INTERFACE.GET_GROUP0_FORW:
                    {
                        string json = JsonConvert.SerializeObject(mScanCorr.Group0.ForwDatas);
                        infodata = Misc.Converter.StringToBytes(json);
                    }
                    break;
                case SCANCORR_OBJ_INTERFACE.GET_GROUP0_BACKW:
                    {
                        string json = JsonConvert.SerializeObject(mScanCorr.Group0.BackwDatas);
                        infodata = Misc.Converter.StringToBytes(json);
                    } break;
                case SCANCORR_OBJ_INTERFACE.GET_GROUP1_FORW:
                    {
                        string json = JsonConvert.SerializeObject(mScanCorr.Group1.ForwDatas);
                        infodata = Misc.Converter.StringToBytes(json);
                    } break;
                case SCANCORR_OBJ_INTERFACE.GET_GROUP1_BACKW:
                    {
                        string json = JsonConvert.SerializeObject(mScanCorr.Group1.BackwDatas);
                        infodata = Misc.Converter.StringToBytes(json);
                    } break;
                case SCANCORR_OBJ_INTERFACE.GET_VALIDRANGE:
                    {
                        SCANCORR_OBJ_INTERFACE.Pack_ValidRange p = new SCANCORR_OBJ_INTERFACE.Pack_ValidRange();
                        p.begin = mScanCorr.ValidBegin;
                        p.end = mScanCorr.ValidEnd;
                        string json = JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    }
                    break;
                default:
                    infodata = null;
                    break;
            }
        }
        public override void SetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case SCANCORR_OBJ_INTERFACE.SET_ENABLE:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        var p = JsonConvert.DeserializeObject<bool>(json);
                        mScanCorr.Enable = p;
                        mScanCorr.Apply();
                    } break;
                case SCANCORR_OBJ_INTERFACE.SET_VALIDRANGE:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        var p = JsonConvert.DeserializeObject<SCANCORR_OBJ_INTERFACE.Pack_ValidRange>(json);
                        mScanCorr.ValidBegin = p.begin;
                        mScanCorr.ValidEnd = p.end;
                        mScanCorr.Apply();
                    } break;
            }
        }

    }
}