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.Client
{
    public class BlowingFixProfileServiceClient : FObjServiceClient, IBlowingFixProfileService
    {

        public BlowingFixProfileServiceClient(UInt32 serviceId) : base(serviceId) { }

        public BlowingFixProfileServiceClient(UInt32 serviceId, string connName) : base(serviceId, connName) { }

        #region IProfileService  接口

        public BlowingFixProfileParam Param { get; } = new BlowingFixProfileParam();



        /// <summary>
        /// 应用 &amp; 保存
        /// </summary>
        public void Apply()
        {
            string json = JsonConvert.SerializeObject(Param);
            CurrObjSys.SetValueEx(mConn, mServerID, ID,
                BLOWINGFIX_PROFILE_OBJ_INTERFACE.SET_PARAMS,
                Misc.Converter.StringToBytes(json));
        }



        /// <summary>
        /// 获取产品列表, 返回类型为 List&lt;string&gt;
        /// </summary>
        /// <param name="AsyncDelegate"></param>
        /// <param name="AsyncState"></param>
        public void GetList(AsyncCBHandler AsyncDelegate, object AsyncState)
        {
            CurrObjSys.CallFunctionEx(mConn, mServerID, ID,
                BLOWINGFIX_PROFILE_OBJ_INTERFACE.CALL_GETLIST,
                null,
                AsyncDelegate, AsyncState);
        }

        /// <summary>
        /// 删除指定产品
        /// </summary>
        /// <param name="productname"></param>
        public void Del(string productname)
        {
            string json = JsonConvert.SerializeObject(productname);

            CurrObjSys.CallFunctionEx(mConn, mServerID, ID,
                BLOWINGFIX_PROFILE_OBJ_INTERFACE.CALL_DEL, Misc.Converter.StringToBytes(json));
        }

        /// <summary>
        /// 读取指定产品,返回类型为 ProfileParam
        /// </summary>
        /// <param name="productname"></param>
        /// <param name="AsyncDelegate"></param>
        /// <param name="AsyncState"></param>
        public void Read(string productname, AsyncCBHandler AsyncDelegate, object AsyncState)
        {
            string json = JsonConvert.SerializeObject(productname);

            CurrObjSys.CallFunctionEx(mConn, mServerID, ID,
                BLOWINGFIX_PROFILE_OBJ_INTERFACE.CALL_READ,
                Misc.Converter.StringToBytes(json),
                AsyncDelegate, AsyncState);
        }
        #endregion




        public override void ConnectNotify(IFConn from)
        {
            base.ConnectNotify(from);
            if (from.IsConnected)
            {
                //获取所有数据,设置推送
                CurrObjSys.GetValueEx(
                    mConn, mServerID, ID,
                    BLOWINGFIX_PROFILE_OBJ_INTERFACE.GET_PARAMS);

                CurrObjSys.SenseConfigEx(
                    mConn, mServerID, ID,
                    0xffffffff,
                    SENSE_CONFIG.ADD);
            }
        }
        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            switch (memid)
            {
                case BLOWINGFIX_PROFILE_OBJ_INTERFACE.GET_PARAMS:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        BlowingFixProfileParam p = JsonConvert.DeserializeObject<BlowingFixProfileParam>(json);

                        Misc.PropertiesManager.CopyTo(p, Param);

                    }
                    break;
            }
        }
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {

            PushGetValue(from, srcid, infoid, infodata);


        }
        public override void PushCallFunction(IFConn from, uint srcid, uint magic, ushort funcid, byte[] retdata, object AsyncDelegate, object AsyncState)
        {
            switch (funcid)
            {
                case BLOWINGFIX_PROFILE_OBJ_INTERFACE.CALL_GETLIST:
                    {
                        string json = Misc.Converter.BytesToString(retdata);
                        List<string> p = JsonConvert.DeserializeObject<List<string>>(json);
                        ((AsyncCBHandler)AsyncDelegate)(AsyncState, p);
                    }
                    break;
                case BLOWINGFIX_PROFILE_OBJ_INTERFACE.CALL_READ:
                    {
                        string json = Misc.Converter.BytesToString(retdata);
                        BlowingFixProfileParam p = JsonConvert.DeserializeObject<BlowingFixProfileParam>(json);
                        
                        ((AsyncCBHandler)AsyncDelegate)(AsyncState, p);
                    }
                    break;
            }
        }
    }
}