using FLY.Thick.Blowing.IService;
using FLY.Thick.Blowing.IService.IBulkDBServicePack;
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 BulkDB_OBJProxy : FObj
    {
        #region 延时推送 MARKNO
        const int MARKNO_PUSH_PARAMS = 0;
        const int MARKNO_PUSH_STATE = 1;
        #endregion

        IBulkDbService bulkDB;



        public BulkDB_OBJProxy(int objsys_idx, UInt32 id, IBulkDbService bulkDB)
        {
            ID = id;
            this.bulkDB = bulkDB;
            bulkDB.PropertyChanged += BulkDB_PropertyChanged;
            bulkDB.TempFrameChanged += BulkDB_TempFrameChanged;
        }

        private void BulkDB_TempFrameChanged(object sender, BulkDBTempFrameChangedEventArgs e)
        {
            string json = JsonConvert.SerializeObject(e);
            CurrObjSys.PushObjInfoEx(
                this, BULKDB_OBJ_INTERFACE.PUSH_TEMP_FRAME_CHANGED,
                Misc.Converter.StringToBytes(json));
        }


        static readonly string[] propertynames_state = new string[] { "Count", "StartTime", "EndTime", "IsFinished" };

        private void BulkDB_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (propertynames_state.Contains(e.PropertyName))
            {
                FObjBase.PollModule.Current.Poll_JustOnce(
                    new PollModule.PollHandler(delegate ()
                    {
                        byte[] buf;
                        GetValue(null, ID, BULKDB_OBJ_INTERFACE.GET_STATE, out buf);
                        CurrObjSys.PushObjInfoEx(
                            this, BULKDB_OBJ_INTERFACE.PUSH_STATE,
                            buf);
                    }), this, MARKNO_PUSH_STATE);
            }
        }

        public override void GetValue(IFConn from, uint srcid, ushort memid, out byte[] infodata)
        {
            infodata = null;
            switch (memid)
            {
                case BULKDB_OBJ_INTERFACE.GET_STATE:
                    {
                        BULKDB_OBJ_INTERFACE.Pack_State p = new BULKDB_OBJ_INTERFACE.Pack_State()
                        {
                            Count = bulkDB.Count,
                            StartTime = bulkDB.StartTime,
                            EndTime = bulkDB.EndTime,
                            IsFinished = bulkDB.IsFinished
                            
                        };

                        string json = JsonConvert.SerializeObject(p);

                        infodata = Misc.Converter.StringToBytes(json);
                    }
                    break;
                default:
                    infodata = null;
                    break;
            }
        }



        public override void CallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] infodata)
        {
            switch (funcid)
            {
                case BULKDB_OBJ_INTERFACE.CALL_GET_FRAME:
                    {
                        string json = Misc.Converter.BytesToString(infodata);

                        var request = JsonConvert.DeserializeObject<Pack_GetFrameRequest>(json);

                        bulkDB.GetFrame(request,
                            delegate (object AsyncContext, object retData)
                            {
                                ConnContext context = (ConnContext)AsyncContext;
                                string j = JsonConvert.SerializeObject(retData);

                                CurrObjSys.PushCallFunctionEx(
                                    context.from,
                                    context.srcid,
                                    ID,
                                    context.magic,
                                    BULKDB_OBJ_INTERFACE.CALL_GET_FRAME,
                                    Misc.Converter.StringToBytes(j));
                            }, new ConnContext(from, srcid, magic));
                    }
                    break;
                case BULKDB_OBJ_INTERFACE.CALL_GET_TREND:
                    {
                        string json = Misc.Converter.BytesToString(infodata);

                        var request = JsonConvert.DeserializeObject<Pack_GetTrendRequest>(json);

                        bulkDB.GetTrend(request,
                            delegate (object AsyncContext, object retData)
                            {
                                ConnContext context = (ConnContext)AsyncContext;
                                string j = JsonConvert.SerializeObject(retData);

                                CurrObjSys.PushCallFunctionEx(
                                    context.from,
                                    context.srcid,
                                    ID,
                                    context.magic,
                                    BULKDB_OBJ_INTERFACE.CALL_GET_TREND,
                                    Misc.Converter.StringToBytes(j));
                            }, new ConnContext(from, srcid, magic));
                    }
                    break;
                case BULKDB_OBJ_INTERFACE.CALL_FINISH:
                    {
                        bulkDB.Finish();
                    }break;
            }
        }
    }
}