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_STATE = 1;
        #endregion
        private static string[] propertyname_params = new string[] {
            "Enable","Valid","TempADBySet","TempAD","TempRange","TempRangePercent","IsTempRangeByPercent",
            "SensorWidth","N","N2","N3","IsDetectBreak"
        };
        private static string[] propertyname_state = new string[] {
            "Border_Backw","Border_Forw","Border","Width","Mid","UpdateTime"
        };
        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 (propertyname_params.Contains(e.PropertyName))
            {

                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 (propertyname_state.Contains(e.PropertyName))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate ()
                    {
                        byte[] buf;
                        GetValue(null, 0, BORDERSEARCH_OBJ_INTERFACE.GET_STATE, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, BORDERSEARCH_OBJ_INTERFACE.PUSH_STATE,
                            buf);
                    }), this, MARKNO_PUSH_STATE);
            }
        }


        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            switch (memid)
            {
                case BORDERSEARCH_OBJ_INTERFACE.GET_PARAMS:
                    {
                        var p = BORDERSEARCH_OBJ_INTERFACE.Mapper.Map<BORDERSEARCH_OBJ_INTERFACE.Pack_Params>(mBorderSearch);
                        string json = Newtonsoft.Json.JsonConvert.SerializeObject(p);
                        infodata = Misc.Converter.StringToBytes(json);
                    } break;
                case BORDERSEARCH_OBJ_INTERFACE.GET_STATE:
                    {
                        var p = BORDERSEARCH_OBJ_INTERFACE.Mapper.Map<BORDERSEARCH_OBJ_INTERFACE.Pack_State>(mBorderSearch);
                        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);
                        Newtonsoft.Json.JsonConvert.PopulateObject(json, mBorderSearch);
                        mBorderSearch.Apply();
                        
                    } break;
            }
        }
        public override void CallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] infodata)
        {
            switch (funcid)
            {
                case BORDERSEARCH_OBJ_INTERFACE.CALL_GETVIEW:
                    {
                        mBorderSearch.GetView((asyncContext, retData) =>
                        {
                            string json = Newtonsoft.Json.JsonConvert.SerializeObject(retData);
                            CurrObjSys.PushCallFunctionEx(from, srcid, ID, magic, funcid, Misc.Converter.StringToBytes(json));
                        }, null);

                    }
                    break;
            }
        }

    }
}