using FLY.FeedbackRenZiJia.IService;
using FLY.FeedbackRenZiJia.OBJ_INTERFACE;
using FObjBase;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FLY.FeedbackRenZiJia.Server.OBJProxy
{
    public class HeatBuf_OBJProxy: FObj
    {
        #region markno
        public const UInt16 MARKNO_PUSH_PARAMS = 1;//参数
        public const UInt16 MARKNO_PUSH_STATE = 2;
        public const UInt16 MARKNO_PUSH_BM = 3;
        public const UInt16 MARKNO_PUSH_ISSTABLES = 4;
        #endregion

        IHeatBufService data;
        public HeatBuf_OBJProxy(int objsys_idx, UInt32 id, IHeatBufService data):base(objsys_idx)
        {
            ID = id;

            this.data = data;

            this.data.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(data_PropertyChanged);
        }


        void data_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "HeatEffectCurve") ||
                (e.PropertyName == "StableRange") ||
                (e.PropertyName == "StableRange0") ||
                (e.PropertyName == "ThresholdR") ||
                (e.PropertyName == "ThresholdMaxMin") ||
                (e.PropertyName == "ThresholdSigmaMax") ||
                (e.PropertyName == "IsUsedLocalKp") ||
                (e.PropertyName == "LocalKp")
                )
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                        new PollModule.PollHandler(delegate ()
                        {
                            byte[] buf;
                            GetValue(null, 0, HEATBUF_OBJ_INTERFACE.GET_PARAMS, out buf);
                            FObjBase.FObjSys.Current.PushObjInfoEx(
                                this, HEATBUF_OBJ_INTERFACE.PUSH_PARAMS,
                                buf);
                        }), this, MARKNO_PUSH_PARAMS);
            }
            else if (
                (e.PropertyName == "Delay") ||
                (e.PropertyName == "IsIntoAutoONo") ||
                (e.PropertyName == "AutoONoResult") ||
                (e.PropertyName == "BestOrgBoltNo") ||
                (e.PropertyName == "BestKp") ||
                (e.PropertyName == "MaxR") ||
                (e.PropertyName == "MaxMin") ||
                (e.PropertyName == "Stability") ||
                (e.PropertyName == "CurrR") ||
                (e.PropertyName == "Curr2Sigma") ||
                (e.PropertyName == "CurrBreakUp") ||
                (e.PropertyName == "CurrTime") ||
                (e.PropertyName == "CurrDirection")
                )
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                     new PollModule.PollHandler(delegate ()
                     {
                         byte[] buf;
                         GetValue(null, 0, HEATBUF_OBJ_INTERFACE.GET_STATE, out buf);
                         FObjBase.FObjSys.Current.PushObjInfoEx(
                             this, HEATBUF_OBJ_INTERFACE.PUSH_STATE,
                             buf);
                     }), this, MARKNO_PUSH_STATE);
            }
            else if ((e.PropertyName == "FirstBM") ||
                    (e.PropertyName == "LastBM")
                )
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                     new PollModule.PollHandler(delegate ()
                     {
                         byte[] buf;
                         GetValue(null, 0, HEATBUF_OBJ_INTERFACE.GET_BM, out buf);
                         FObjBase.FObjSys.Current.PushObjInfoEx(
                             this, HEATBUF_OBJ_INTERFACE.PUSH_BM,
                             buf);
                     }), this, MARKNO_PUSH_BM);
            }
            else if (e.PropertyName == "BoltIsStable")
                
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                     new PollModule.PollHandler(delegate ()
                     {
                         byte[] buf;
                         GetValue(null, 0, HEATBUF_OBJ_INTERFACE.GET_ISSTABLES, out buf);
                         FObjBase.FObjSys.Current.PushObjInfoEx(
                             this, HEATBUF_OBJ_INTERFACE.PUSH_ISSTABLES,
                             buf);
                     }), this, MARKNO_PUSH_ISSTABLES);
            }

        }
        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            infodata = null;
            switch (memid)
            {
                case HEATBUF_OBJ_INTERFACE.GET_PARAMS:
                    {
                        var p = new HEATBUF_OBJ_INTERFACE.Pack_Params()
                        {
                            HeatEffectCurve = data.HeatEffectCurve,
                            StableRange = data.StableRange,
                            StableRange0 = data.StableRange0,
                            ThresholdR = data.ThresholdR,
                            ThresholdMaxMin = data.ThresholdMaxMin,
                            ThresholdSigmaMax = data.ThresholdSigmaMax,
                            IsUsedLocalKp = data.IsUsedLocalKp,
                            LocalKp = data.LocalKp
                        };
                        string json =JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    }
                    break;
                case HEATBUF_OBJ_INTERFACE.GET_STATE:
                    {
                        var p = new HEATBUF_OBJ_INTERFACE.Pack_Status()
                        {
                            Delay = data.Delay,
                            IsIntoAutoONo = data.IsIntoAutoONo,
                            AutoONoResult = data.AutoONoResult,
                            BestOrgBoltNo = data.BestOrgBoltNo,
                            BestKp = data.BestKp,
                            MaxR = data.MaxR,
                            MaxMin = data.MaxMin,
                            Stability = data.Stability,
                            CurrR = data.CurrR,
                            Curr2Sigma = data.Curr2Sigma,
                            CurrBreakUp = data.CurrBreakUp,
                            CurrTime = data.CurrTime,
                            CurrDirection = data.CurrDirection
                        };
                        string json = JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    }
                    break;
                case HEATBUF_OBJ_INTERFACE.GET_BM:
                    {
                        var p = new HEATBUF_OBJ_INTERFACE.Pack_BM
                        {
                            firstbm = data.FirstBM,
                            lastbm = data.LastBM
                        };
                        string json = JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    }
                    break;
                case HEATBUF_OBJ_INTERFACE.GET_ISSTABLES:
                    {
                        var p = new HEATBUF_OBJ_INTERFACE.Pack_IsStables
                        {
                            isStables = data.BoltIsStable
                        };
                        string json = JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    }
                    break;
            }
        }
        public override void SetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case HEATBUF_OBJ_INTERFACE.SET_PARAMS:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        var p = JsonConvert.DeserializeObject<HEATBUF_OBJ_INTERFACE.Pack_Params>(json);
                        
                        data.HeatEffectCurve = p.HeatEffectCurve;
                        data.StableRange = p.StableRange;
                        data.StableRange0 = p.StableRange0;
                        data.ThresholdR = p.ThresholdR;
                        data.ThresholdMaxMin = p.ThresholdMaxMin;
                        data.ThresholdSigmaMax = p.ThresholdSigmaMax;
                        data.IsUsedLocalKp = p.IsUsedLocalKp;
                        data.LocalKp = p.LocalKp;
                        data.Apply();
                    }
                    break;
            }
        }
        public override void CallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] infodata)
        {
            switch (funcid)
            {
                case HEATBUF_OBJ_INTERFACE.CALL_GETHEATDATA:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        var p = JsonConvert.DeserializeObject<HEATBUF_OBJ_INTERFACE.Pack_GetHeatsDataRequest>(json);

                        data.GetHeatsData(p.bookmark,
                            new AsyncCBHandler(delegate (object AsyncState, object retdata)
                            {
                                ConnContext context = (ConnContext)AsyncState;
                                string json_reponse = JsonConvert.SerializeObject(retdata);

                                CurrObjSys.PushCallFunctionEx(
                                    context.from,
                                    context.srcid,
                                    ID,
                                    context.magic,
                                    HEATBUF_OBJ_INTERFACE.CALL_GETHEATDATA,
                                    Misc.Converter.StringToBytes(json_reponse));
                            }), new ConnContext(from, srcid, magic));
                    }
                    break;
                case HEATBUF_OBJ_INTERFACE.CALL_CLEARBOLTHEAT:
                    {
                        data.ClearBoltHeat();
                    }
                    break;
                case HEATBUF_OBJ_INTERFACE.CALL_GETBOLTHEATRECORD:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        var p = JsonConvert.DeserializeObject<HEATBUF_OBJ_INTERFACE.Pack_GetBoltHeatRecordRequest>(json);

                        data.GetBoltHeatRecord(p.no,
                            new AsyncCBHandler(delegate (object AsyncState, object retdata)
                            {
                                ConnContext context = (ConnContext)AsyncState;
                                string json_reponse = JsonConvert.SerializeObject(retdata);

                                CurrObjSys.PushCallFunctionEx(
                                    context.from,
                                    context.srcid,
                                    ID,
                                    context.magic,
                                    HEATBUF_OBJ_INTERFACE.CALL_GETBOLTHEATRECORD,
                                    Misc.Converter.StringToBytes(json_reponse));
                            }), new ConnContext(from, srcid, magic));
                    }
                    break;
            }
        }

    }
}