using FLY.Thick.Base.OBJ_INTERFACE;
using FLY.Thick.Blowing.Common;
using FLY.Thick.Blowing.IService;
using FLY.Thick.Blowing.OBJ_INTERFACE;
using FObjBase;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FLY.Thick.Blowing.Server.OBJProxy
{
    public class BlowingFixProfile_OBJProxy: FObj
    {
        #region 延时推送 MARKNO
        const int MARKNO_PUSH_PARAMS = 1;
        #endregion

        IBlowingFixProfileService mProfile;

        public BlowingFixProfile_OBJProxy(int objsys_idx, IBlowingFixProfileService profile)
            : base(objsys_idx)
        {
            ID = FLY.Thick.Blowing.OBJ_INTERFACE.OBJ_INTERFACE_ID.BFPROFILE_ID;
            mProfile = profile;
            mProfile.Param.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(mProfile_PropertyChanged);
        }

        void mProfile_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            FObjBase.PollModule.Current.Poll_JustOnce(
                new PollModule.PollHandler(delegate ()
                {
                    byte[] buf;
                    GetValue(null, ID, BLOWINGFIX_PROFILE_OBJ_INTERFACE.GET_PARAMS, out buf);
                    CurrObjSys.PushObjInfoEx(
                        this, BLOWINGFIX_PROFILE_OBJ_INTERFACE.PUSH_PARAMS,
                        buf);
                }), this, MARKNO_PUSH_PARAMS);

        }

        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            infodata = null;
            switch (memid)
            {
                case BLOWINGFIX_PROFILE_OBJ_INTERFACE.GET_PARAMS:
                    {
                        string json = JsonConvert.SerializeObject(mProfile.Param);
                        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 BLOWINGFIX_PROFILE_OBJ_INTERFACE.SET_PARAMS:
                    {
                        string json = Misc.Converter.BytesToString(infodata);

                        BlowingFixProfileParam p = JsonConvert.DeserializeObject<BlowingFixProfileParam>(json);
                        Misc.PropertiesManager.CopyTo(p, mProfile.Param);
                        mProfile.Apply();
                    }
                    break;
            }
        }
        public override void CallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] infodata)
        {
            switch (funcid)
            {
                case BLOWINGFIX_PROFILE_OBJ_INTERFACE.CALL_GETLIST:
                    {
                        mProfile.GetList(
                            new AsyncCBHandler(delegate (object AsyncState, object retdata)
                            {
                                ConnContext context = (ConnContext)AsyncState;
                                string json = JsonConvert.SerializeObject(retdata);
                                
                                CurrObjSys.PushCallFunctionEx(
                                    context.from,
                                    context.srcid,
                                    ID,
                                    context.magic,
                                    BLOWINGFIX_PROFILE_OBJ_INTERFACE.CALL_GETLIST,
                                    Misc.Converter.StringToBytes(json));
                            }), new ConnContext(from, srcid, magic));
                    }
                    break;
                case BLOWINGFIX_PROFILE_OBJ_INTERFACE.CALL_DEL:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        string p = JsonConvert.DeserializeObject<string>(json);
                        mProfile.Del(p);
                    }
                    break;
                case BLOWINGFIX_PROFILE_OBJ_INTERFACE.CALL_READ:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        string p = JsonConvert.DeserializeObject<string>(json);

                        mProfile.Read(
                            p,
                            new AsyncCBHandler(delegate (object AsyncState, object retdata)
                            {
                                ConnContext context = (ConnContext)AsyncState;
                                json = JsonConvert.SerializeObject(retdata);

                                CurrObjSys.PushCallFunctionEx(
                                    context.from,
                                    context.srcid,
                                    ID,
                                    context.magic,
                                    BLOWINGFIX_PROFILE_OBJ_INTERFACE.CALL_READ,
                                    Misc.Converter.StringToBytes(json));
                            }), new ConnContext(from, srcid, magic));
                    }
                    break;
            }
        }
    }
}