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 initparam;

        public Initparam_OBJProxy(int objsys_idx, UInt32 id, IInitParamService initparam) :base(objsys_idx)
        {
            ID = id;
            this.initparam = initparam;
            this.initparam.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 = initparam.PosLength,
                            auto_org_interval = initparam.AutoOrgInterval,
                            encoder1_mmpp = initparam.Encoder1_mmpp,
                            speed1scale = initparam.Speed1Scale,
                            adlag = initparam.ADLag,
                            isTimeGridAdvEnabled = initparam.IsTimeGridAdvEnabled,
                            posOfGrid = initparam.PosOfGrid,

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


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


                            hasProfileSample = initparam.HasProfileSample,
                            hasHold = initparam.HasHold,
                            dBKeepMonth = initparam.DBKeepMonth,
                            dbPath = initparam.DbPath
                        };
                        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);
                        
                        initparam.PosLength = p.poslength;
                        initparam.IsTimeGridAdvEnabled = p.isTimeGridAdvEnabled;
                        initparam.ADLag = p.adlag;
                        initparam.AutoOrgInterval = p.auto_org_interval;
                        initparam.Encoder2_mmpp= p.encoder2_mmpp;
                        initparam.Encoder2_comp = p.encoder2_comp;
                        initparam.Encoder1_mmpp = p.encoder1_mmpp;
                        initparam.VScan = p.vscan;
                        initparam.VJOG = p.vjog;
                        initparam.VAccuracy = p.vaccuracy;
                        initparam.SVelocity = p.svelocity;
                        initparam.ATime= p.atime;
                        initparam.DTime = p.dtime;
                        initparam.HVelocity1 = p.hvelocity1;
                        initparam.HVelocity2 = p.hvelocity2;
                        initparam.DataValidSrc = p.data_valid_src;
                        //mInitparam.Speed1Scale = p.speed1scale;
                        initparam.MmOfR = p.mmOfR;

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

                        initparam.HasProfileSample = p.hasProfileSample;
                        initparam.HasHold = p.hasHold;
                        initparam.DBKeepMonth = p.dBKeepMonth;
                        initparam.DbPath = p.dbPath;
                        initparam.Apply();
                    } break;
            }
        }

    }
}
