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

namespace FLY.FeedbackRenZiJia.Server.OBJProxy
{
    public class HeatCell_OBJProxy : FObj
    {
        #region markno
        public const UInt16 MARKNO_PUSH_PARAMS = 1;//参数
        public const UInt16 MARKNO_PUSH_STATE = 2;

        public const UInt16 MARKNO_PUSH_THICKS = 4;
        public const UInt16 MARKNO_PUSH_HEATS = 5;
        public const UInt16 MARKNO_PUSH_OFFSETS = 6;
        public const UInt16 MARKNO_PUSH_PREHEATS = 7;
        #endregion

        IHeatCell data;
        public HeatCell_OBJProxy(int objsys_idx, IHeatCell data):base(objsys_idx)
        {
            ID = OBJ_INTERFACE_ID.HEATCELL_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 == "Kp") ||
                (e.PropertyName == "IsAutoONo") ||
                (e.PropertyName == "AutoONoKp") ||
                (e.PropertyName == "CtrlLine") ||
                (e.PropertyName == "ThresholdHeatSigma") ||
                (e.PropertyName == "IsForbidUpDown")||
                (e.PropertyName == "IsBreakUpMode") 
                )
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                        new PollModule.PollHandler(delegate ()
                        {
                            byte[] buf;
                            GetValue(null, 0, HEATCELL_OBJ_INTERFACE.GET_PARAMS, out buf);
                            FObjBase.FObjSys.Current.PushObjInfoEx(
                                this, HEATCELL_OBJ_INTERFACE.PUSH_PARAMS,
                                buf);
                        }), this, MARKNO_PUSH_PARAMS);
            }
            else if (
                (e.PropertyName == "MaxHeatSigma") ||
                (e.PropertyName == "IsInAutoONo")
                )
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                     new PollModule.PollHandler(delegate ()
                     {
                         byte[] buf;
                         GetValue(null, 0, HEATCELL_OBJ_INTERFACE.GET_STATE, out buf);
                         FObjBase.FObjSys.Current.PushObjInfoEx(
                             this, HEATCELL_OBJ_INTERFACE.PUSH_STATE,
                             buf);
                     }), this, MARKNO_PUSH_STATE);
            }
            else if ((e.PropertyName == "ThickPercents"))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                     new PollModule.PollHandler(delegate ()
                     {
                         byte[] buf;
                         GetValue(null, 0, HEATCELL_OBJ_INTERFACE.GET_THICKS, out buf);
                         FObjBase.FObjSys.Current.PushObjInfoEx(
                             this, HEATCELL_OBJ_INTERFACE.PUSH_THICKS,
                             buf);
                     }), this, MARKNO_PUSH_THICKS);
            }
            else if ((e.PropertyName == "Heats"))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                         new PollModule.PollHandler(delegate ()
                         {
                             byte[] buf;
                             GetValue(null, 0, HEATCELL_OBJ_INTERFACE.GET_HEATS, out buf);
                             FObjBase.FObjSys.Current.PushObjInfoEx(
                                 this, HEATCELL_OBJ_INTERFACE.PUSH_HEATS,
                                 buf);
                         }), this, MARKNO_PUSH_HEATS);
            }
            else if ((e.PropertyName == "Offsets"))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                     new PollModule.PollHandler(delegate ()
                     {
                         byte[] buf;
                         GetValue(null, 0, HEATCELL_OBJ_INTERFACE.GET_OFFSETS, out buf);
                         FObjBase.FObjSys.Current.PushObjInfoEx(
                             this, HEATCELL_OBJ_INTERFACE.PUSH_OFFSETS,
                             buf);
                     }), this, MARKNO_PUSH_OFFSETS);
            }
            else if ((e.PropertyName == "PreHeats"))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                         new PollModule.PollHandler(delegate ()
                         {
                             byte[] buf;
                             GetValue(null, 0, HEATCELL_OBJ_INTERFACE.GET_PREHEATS, out buf);
                             FObjBase.FObjSys.Current.PushObjInfoEx(
                                 this, HEATCELL_OBJ_INTERFACE.PUSH_PREHEATS,
                                 buf);
                         }), this, MARKNO_PUSH_PREHEATS);
            }
        }


        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            infodata = null;
            switch (memid)
            {
                case HEATCELL_OBJ_INTERFACE.GET_PARAMS:
                    {
                        infodata = new HEATCELL_OBJ_INTERFACE.Pack_Params()
                        {
                            kp = data.Kp,
                            autoONoKp = data.AutoONoKp,
                            thresholdHeatSigma = data.ThresholdHeatSigma,
                            ctrlLine = data.CtrlLine,
                            isAutoONo = data.IsAutoONo,
                            isForbidUpDown = data.IsForbidUpDown,
                            isBreakUpMode = data.IsBreakUpMode
                        }.ToBytes();
                    }
                    break;
                case HEATCELL_OBJ_INTERFACE.GET_STATE:
                    {
                        infodata = new HEATCELL_OBJ_INTERFACE.Pack_Status()
                        {
                            isInAutoONo = data.IsInAutoONo,
                            maxHeatSigma = data.MaxHeatSigma
                        }.ToBytes();
                    }
                    break;
                case HEATCELL_OBJ_INTERFACE.GET_THICKS:
                    {
                        infodata = new HEATCELL_OBJ_INTERFACE.Pack_Frame()
                        {
                            frame = data.ThickPercents.ToArray()
                        }.ToBytes();
                    }
                    break;
                case HEATCELL_OBJ_INTERFACE.GET_HEATS:
                    {
                        infodata = new HEATCELL_OBJ_INTERFACE.Pack_Frame()
                        {
                            frame = data.Heats.ToArray()
                        }.ToBytes();
                    }
                    break;
                case HEATCELL_OBJ_INTERFACE.GET_OFFSETS:
                    {
                        infodata = new HEATCELL_OBJ_INTERFACE.Pack_Frame()
                        {
                            frame = data.Offsets.ToArray()
                        }.ToBytes();
                    }
                    break;
                case HEATCELL_OBJ_INTERFACE.GET_PREHEATS:
                    {
                        infodata = new HEATCELL_OBJ_INTERFACE.Pack_Frame()
                        {
                            frame = data.PreHeats.ToArray()
                        }.ToBytes();
                    }
                    break;
            }
        }
        public override void SetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case HEATCELL_OBJ_INTERFACE.SET_PARAMS:
                    {
                        HEATCELL_OBJ_INTERFACE.Pack_Params p = new HEATCELL_OBJ_INTERFACE.Pack_Params();
                        if (!p.TryParse(infodata))
                            return;

                        data.Kp = p.kp;
                        data.IsAutoONo = p.isAutoONo;
                        data.AutoONoKp = p.autoONoKp;
                        data.ThresholdHeatSigma = p.thresholdHeatSigma;
                        data.IsForbidUpDown = p.isForbidUpDown;
                        data.IsBreakUpMode = p.isBreakUpMode;
                        data.CtrlLine = p.ctrlLine;
                        data.Apply();

                    }
                    break;
            }
        }

        public override void CallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] infodata)
        {
            switch (funcid)
            {
                case HEATCELL_OBJ_INTERFACE.CALL_HEATAPPLY:
                    {
                        data.HeatApply();
                    }
                    break;
                case HEATCELL_OBJ_INTERFACE.CALL_CAL:
                    {
                        data.Cal();
                    }
                    break;

                case HEATCELL_OBJ_INTERFACE.CALL_MODIFYHEATOFFSET:
                    {
                        HEATCELL_OBJ_INTERFACE.Pack_ModifyHeatOffset p = new HEATCELL_OBJ_INTERFACE.Pack_ModifyHeatOffset();
                        if (!p.TryParse(infodata))
                            return;
                        data.ModifyOffset(p.index, p.heatoffset);
                    }
                    break;
                case HEATCELL_OBJ_INTERFACE.CALL_MODIFYBASE:
                    {
                        HEATCELL_OBJ_INTERFACE.Pack_ModifyBase p = new HEATCELL_OBJ_INTERFACE.Pack_ModifyBase();
                        if (!p.TryParse(infodata))
                            return;

                        data.ModifyBase(p.change);
                    }
                    break;
                case HEATCELL_OBJ_INTERFACE.CALL_CLEARHEATOFFSETS:
                    {
                        data.ClearOffsets();
                    }
                    break;
                case HEATCELL_OBJ_INTERFACE.CALL_CLEARHEATS:
                    {
                        data.ClearPreHeats();
                    }
                    break;
                case HEATCELL_OBJ_INTERFACE.CALL_SMOOTH:
                    {
                        HEATCELL_OBJ_INTERFACE.Pack_CallSmoothRequest p = new HEATCELL_OBJ_INTERFACE.Pack_CallSmoothRequest();
                        if (!p.TryParse(infodata))
                            return;
                        data.ThresholdHeatSigma = p.thresholdHeatSigma;
                        data.Smooth();
                    }
                    break;
            }
        }

    }
}