using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;
using FLY.Thick.Base.OBJ_INTERFACE;
using FLY.Thick.Base.Server;
using FLY.Thick.Base.IService;
namespace FLY.Thick.Base.Server.OBJProxy
{
    public class BorderSearch_OBJProxy : FObj
    {
        #region 延时推送 MARKNO
        const int MARKNO_PUSH_PARAMS = 0;
        const int MARKNO_PUSH_RESULT = 1;
        const int MARKNO_PUSH_DATAS = 2;
        #endregion
        IBorderSearchService mBorderSearch;

        public BorderSearch_OBJProxy(int objsys_idx, UInt32 id, IBorderSearchService borderSearch) :base(objsys_idx)
        {
            ID = id;
            mBorderSearch = borderSearch;
            mBorderSearch.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(mBorderSearch_PropertyChanged);

        }

        void mBorderSearch_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if ((e.PropertyName == "Enable") ||
                (e.PropertyName == "Valid") ||
                (e.PropertyName == "TempADBySet") ||
                (e.PropertyName == "TempAD") ||
                (e.PropertyName == "TempRange") ||
                (e.PropertyName == "TempRangePercent") ||
                (e.PropertyName == "IsTempRangeByPercent") ||
                (e.PropertyName == "SensorWidth") ||
                (e.PropertyName == "N") ||
                (e.PropertyName == "N2") ||
                (e.PropertyName == "N3") ||
                (e.PropertyName == "IsDetectBreak")
                )
            {

                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate ()
                {
                    byte[] buf;
                    GetValue(null, 0, BORDERSEARCH_OBJ_INTERFACE.GET_PARAMS, out buf);
                    CurrObjSys.PushObjInfoEx(
                        this, BORDERSEARCH_OBJ_INTERFACE.PUSH_PARAMS,
                        buf);
                }), this, MARKNO_PUSH_PARAMS);
            }
            else if (
                (e.PropertyName == "Border_Backw") ||
                (e.PropertyName == "Border_Forw") ||
                (e.PropertyName == "Width") ||
                (e.PropertyName == "Mid"))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate ()
                    {
                        byte[] buf;
                        GetValue(null, 0, BORDERSEARCH_OBJ_INTERFACE.GET_RESULT, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, BORDERSEARCH_OBJ_INTERFACE.PUSH_RESULT,
                            buf);
                    }), this, MARKNO_PUSH_RESULT);
            }
            else if (e.PropertyName == "Datas")
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate ()
                    {
                        byte[] buf;
                        GetValue(null, 0, BORDERSEARCH_OBJ_INTERFACE.GET_DATAS, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, BORDERSEARCH_OBJ_INTERFACE.PUSH_DATAS,
                            buf);
                    }), this, MARKNO_PUSH_DATAS);
            }
        }


        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            switch (memid)
            {
                case BORDERSEARCH_OBJ_INTERFACE.GET_PARAMS:
                    {
                        BORDERSEARCH_OBJ_INTERFACE.Pack_Params p = new BORDERSEARCH_OBJ_INTERFACE.Pack_Params()
                        {
                            enable = mBorderSearch.Enable,
                            valid = mBorderSearch.Valid,
                            tempADBySet = mBorderSearch.TempADBySet,
                            tempad = mBorderSearch.TempAD,
                            temprange = mBorderSearch.TempRange,
                            n = mBorderSearch.N,
                            sensorwidth = mBorderSearch.SensorWidth,
                            n2 = mBorderSearch.N2,
                            n3 = mBorderSearch.N3,
                            isBreakDetect = mBorderSearch.IsBreakDetect,
                            isTempRangeByPercent = mBorderSearch.IsTempRangeByPercent,
                            tempRangePercent = mBorderSearch.TempRangePercent
                        };
                        string json = Newtonsoft.Json.JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    } break;
                case BORDERSEARCH_OBJ_INTERFACE.GET_RESULT:
                    {
                        BORDERSEARCH_OBJ_INTERFACE.Pack_Result p = new BORDERSEARCH_OBJ_INTERFACE.Pack_Result()
                        {
                            borderForw = mBorderSearch.Border_Forw,
                            borderBackw = mBorderSearch.Border_Backw,
                            width = mBorderSearch.Width,
                            mid = mBorderSearch.Mid
                        };
                        string json = Newtonsoft.Json.JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    } break;
                case BORDERSEARCH_OBJ_INTERFACE.GET_DATAS:
                    {
                        BORDERSEARCH_OBJ_INTERFACE.Pack_Data p = new BORDERSEARCH_OBJ_INTERFACE.Pack_Data()
                        {
                            data = mBorderSearch.Datas
                        };
                        string json = Newtonsoft.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 BORDERSEARCH_OBJ_INTERFACE.SET_PARAMS:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        var p = Newtonsoft.Json.JsonConvert.DeserializeObject<BORDERSEARCH_OBJ_INTERFACE.Pack_Params>(json);
                        
                        mBorderSearch.Enable = p.enable;
                        mBorderSearch.Valid.Begin = p.valid.Begin;
                        mBorderSearch.Valid.End = p.valid.End;
                        mBorderSearch.TempRange = p.temprange;
                        mBorderSearch.TempADBySet = p.tempADBySet;

                        mBorderSearch.TempAD = p.tempad;
                        mBorderSearch.N = p.n;
                        mBorderSearch.SensorWidth = p.sensorwidth;
                        mBorderSearch.N2 = p.n2;
                        mBorderSearch.N3 = p.n3;

                        mBorderSearch.IsBreakDetect = p.isBreakDetect;
                        mBorderSearch.IsTempRangeByPercent = p.isTempRangeByPercent;
                        mBorderSearch.TempRangePercent = p.tempRangePercent;
                        mBorderSearch.Apply();
                        
                    } break;
            }
        }

    }
}
