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 Newtonsoft.Json;

namespace FLY.Thick.Base.Server.OBJProxy
{
    public class Initparam_OBJProxy : FObj
    {
        #region 延时推送 MARKNO
        const int MARKNO_PUSH_PARAMS = 1;
        #endregion

        IInitParamService mInitparam;

        public Initparam_OBJProxy(int objsys_idx, UInt32 id, IInitParamService initparam) :base(objsys_idx)
        {
            ID = id;
            mInitparam = initparam;
            mInitparam.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(mInitparam_PropertyChanged);
        }

        void mInitparam_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate()
                {
                    byte[] buf;
                    GetValue(null, 0, INITPARAM_OBJ_INTERFACE.GET_PARAMS, out buf);
                    CurrObjSys.PushObjInfoEx(
                        this, INITPARAM_OBJ_INTERFACE.PUSH_PARAMS,
                        buf);
                }), this, MARKNO_PUSH_PARAMS);
            
        }

        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            switch (memid)
            {
                case INITPARAM_OBJ_INTERFACE.GET_PARAMS:
                    {
                        INITPARAM_OBJ_INTERFACE.Pack_Params p = new INITPARAM_OBJ_INTERFACE.Pack_Params()
                        {
                            poslength = mInitparam.PosLength,
                            auto_org_interval = mInitparam.AutoOrgInterval,
                            encoder1_mmpp = mInitparam.Encoder1_mmpp,
                            speed1scale = mInitparam.Speed1Scale,
                            adlag = mInitparam.ADLag,
                            posOfGrid = mInitparam.PosOfGrid,

                            filmVSrc = mInitparam.FilmVSrc,
                            filmVThreshold = mInitparam.FilmVThreshold,
                            encoder2_mmpp = mInitparam.Encoder2_mmpp,
                            encoder2_comp = mInitparam.Encoder2_comp,
                            mmOfR = mInitparam.MmOfR,
                            autof1f3 = mInitparam.AutoF1F3,
                            restartDelay = mInitparam.ReStartDelay,
                            data_valid_src = mInitparam.DataValidSrc,


                            vscan = mInitparam.VScan,
                            vjog = mInitparam.VJOG,
                            vaccuracy = mInitparam.VAccuracy,
                            svelocity = mInitparam.SVelocity,
                            atime = mInitparam.ATime,
                            dtime = mInitparam.DTime,
                            hvelocity1 = mInitparam.HVelocity1,
                            hvelocity2 = mInitparam.HVelocity2,


                            hasProfileSample = mInitparam.HasProfileSample,
                            hasHold = mInitparam.HasHold,
                            dBKeepMonth = mInitparam.DBKeepMonth
                        };
                        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 INITPARAM_OBJ_INTERFACE.SET_PARAMS:
                    {
                        string json = Misc.Converter.BytesToString(infodata);

                        var p = JsonConvert.DeserializeObject<INITPARAM_OBJ_INTERFACE.Pack_Params>(json);
                        
                            mInitparam.PosLength = p.poslength;
                            mInitparam.ADLag = p.adlag;
                            mInitparam.AutoOrgInterval = p.auto_org_interval;
                            mInitparam.Encoder2_mmpp= p.encoder2_mmpp;
                            mInitparam.Encoder2_comp = p.encoder2_comp;
                            mInitparam.Encoder1_mmpp = p.encoder1_mmpp;
                            mInitparam.VScan = p.vscan;
                            mInitparam.VJOG = p.vjog;
                            mInitparam.VAccuracy = p.vaccuracy;
                            mInitparam.SVelocity = p.svelocity;
                            mInitparam.ATime= p.atime;
                            mInitparam.DTime = p.dtime;
                            mInitparam.HVelocity1 = p.hvelocity1;
                            mInitparam.HVelocity2 = p.hvelocity2;
                            mInitparam.DataValidSrc = p.data_valid_src;
                            //mInitparam.Speed1Scale = p.speed1scale;
                            mInitparam.MmOfR = p.mmOfR;

                            mInitparam.AutoF1F3 = p.autof1f3;
                            mInitparam.ReStartDelay = p.restartDelay;
                            mInitparam.FilmVSrc = p.filmVSrc;
                            mInitparam.FilmVThreshold = p.filmVThreshold;

                            mInitparam.HasProfileSample = p.hasProfileSample;
                            mInitparam.HasHold = p.hasHold;
                        mInitparam.DBKeepMonth = p.dBKeepMonth;
                            mInitparam.Apply();
                    } break;
            }
        }

    }
}
