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 FixService : FObj, IFixService
    {
        IFConn mConn;
        UInt32 mServerID;

        public FixService()
        {
            mServerID = FIX_OBJ_INTERFACE.ID;
        }

        #region IFlyAD接口

        /// <summary>
        /// 注册timegrid 事件
        /// </summary>
        /// <param name="handler"></param>
        public void RegistTimeGridEvent(TimeGridEventHandler handler)
        {
            TimeGridEvent += 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(TimeGridEventHandler handler)
        {
            TimeGridEvent -= handler;
            if (TimeGridEvent == null)
            {
                CurrObjSys.SenseConfigEx(
                    mConn, mServerID, ID,
                    Misc.MyBase.BIT(FIX_OBJ_INTERFACE.PUSH_TIMEGRID),
                    SENSE_CONFIG.REMOVE);
            }
        }

        event TimeGridEventHandler TimeGridEvent;

        #endregion


        public override void Dispose()
        {
            CurrObjSys.ObjRemove(
                this, mConn);
        }
        public override void ConnectNotify(IFConn from)
        {
            mConn = from;
            if (from.IsConnected)
            {
                //设置推送

                if (TimeGridEvent != 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:
                    {
                        TimeGridEventArgs p = new TimeGridEventArgs();
                        if (!p.TryParse(infodata))
                            return;
                        if (TimeGridEvent != null)
                            TimeGridEvent(this, p);
                    } break;
            }

        }

    }
}