using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Net;

using FObjBase;
using FLY.Thick.Base.IService;
using FLY.Thick.Base.OBJ_INTERFACE;
using FLY.Thick.Base.Common;

namespace FLY.Thick.Base.Client
{
    public class FixServiceClient : FObjServiceClient, IFixService
    {
        

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

        public FixServiceClient(UInt32 serviceId, string connName) : base(serviceId, connName) { }
        #region IFlyAD接口

        /// <summary>
        /// 注册timegrid 事件
        /// </summary>
        /// <param name="handler"></param>
        public void RegistTimeGridEvent(FixEventHandler handler)
        {
            FixEvent += handler;
            CurrObjSys.SenseConfigEx(
                mConn, mServerID, ID,
                Misc.MyBase.BIT(FIX_OBJ_INTERFACE.PUSH_TIMEGRID),
                SENSE_CONFIG.ADD);
        }
        /// <summary>
        /// 关闭注册timegrid 事件
        /// </summary>
        /// <param name="handler"></param>
        public void UnRegistTimeGridEvent(FixEventHandler handler)
        {
            FixEvent -= handler;
            if (FixEvent == null)
            {
                CurrObjSys.SenseConfigEx(
                    mConn, mServerID, ID,
                    Misc.MyBase.BIT(FIX_OBJ_INTERFACE.PUSH_TIMEGRID),
                    SENSE_CONFIG.REMOVE);
            }
        }

        event FixEventHandler FixEvent;

        #endregion


        public override void ConnectNotify(IFConn from)
        {
            base.ConnectNotify(from);
            if (from.IsConnected)
            {
                //设置推送

                if (FixEvent != null)
                {
                    UInt32 sense_mask = Misc.MyBase.BIT(FIX_OBJ_INTERFACE.PUSH_TIMEGRID);

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

        public override void PushInfo(IFConn from, uint srcid, ushort infoid, byte[] infodata)
        {
            switch (infoid)
            {
                case FIX_OBJ_INTERFACE.PUSH_TIMEGRID:
                    {
                        string json = Misc.Converter.BytesToString(infodata);
                        var p = Newtonsoft.Json.JsonConvert.DeserializeObject<FixEventArgs>(json);

                        FixEvent?.Invoke(this, p);
                    } break;
            }

        }

    }
}