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

namespace FLY.Thick.Base.Client
{
    /// <summary>
    /// 动态数据服务 客户端代理
    /// </summary>
    public class DynAreaServiceClient : FObjServiceClient, IDynAreaService
    {
        /// <summary>
        /// 动态数据 
        /// </summary>
        public DynArea DynArea { get; } = new DynArea();

        /// <summary>
        /// 动态数据服务 构造
        /// </summary>
        /// <param name="serviceId">服务id</param>
        public DynAreaServiceClient(UInt32 serviceId) : base(serviceId) { }

        /// <summary>
        /// 动态数据服务 构造
        /// </summary>
        /// <param name="serviceId">服务id</param>
        /// <param name="connName">连接器</param>
        public DynAreaServiceClient(UInt32 serviceId, string connName) : base(serviceId, connName) { }
        #region IFObj 成员

        /// <summary>
        /// 连接通知
        /// </summary>
        /// <param name="from"></param>
        public override void ConnectNotify(IFConn from)
        {
            base.ConnectNotify(from);
            DynArea.ServerIsConnected = from.IsConnected;
            if (from.IsConnected) 
            {
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_ADPOS);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_SCANINFO);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_CONTROLLER_STATE);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_PROFILE);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_BOLTMAP);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_SYSTEM);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_IO);
                CurrObjSys.GetValueEx(mConn, mServerID, ID, DYNAREA_OBJ_INTERFACE.GET_POSITION2);

                CurrObjSys.SenseConfigEx(mConn, mServerID, ID,0xffffffff, SENSE_CONFIG.ADD);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="from"></param>
        /// <param name="srcid"></param>
        /// <param name="memid"></param>
        /// <param name="infodata"></param>
        public override void PushGetValue(IFConn from, uint srcid, ushort memid, byte[] infodata)
        {
            PushInfo(from,srcid,memid,infodata);
        }
        
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="from"></param>
        /// <param name="srcid"></param>
        /// <param name="infoid"></param>
        /// <param name="infodata"></param>
        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            switch (infoid)
            {
                case DYNAREA_OBJ_INTERFACE.PUSH_ADPOS:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        JsonConvert.PopulateObject(json, DynArea);
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_SCANINFO:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        JsonConvert.PopulateObject(json, DynArea);
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_PROFILE:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        JsonConvert.PopulateObject(json, DynArea);
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_SYSTEM:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        JsonConvert.PopulateObject(json, DynArea);
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_BOLTMAP:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        JsonConvert.PopulateObject(json, DynArea);
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_IO:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        JsonConvert.PopulateObject(json, DynArea);
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_CONTROLLER_STATE:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        JsonConvert.PopulateObject(json, DynArea);
                    } break;
                case DYNAREA_OBJ_INTERFACE.PUSH_POSITION2:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        JsonConvert.PopulateObject(json, DynArea);
                    }
                    break;
            }
        }
        #endregion


    }
}
