﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using eSpaceLib;

namespace com.huawei.ebg.esdk.ucsdk.client.control
{
    public class IECSExternEventHandler : IECSEventCallback
    {
        private static Guid m_guid;
        private eSpaceStatus m_parentInvoker;

        internal IECSExternEventHandler(eSpaceStatus invoker)
        {
            GuidAttribute IMyInterfaceAttribute = (GuidAttribute)Attribute.GetCustomAttribute(typeof(IECSEventCallback), typeof(GuidAttribute));
            m_guid = new Guid(IMyInterfaceAttribute.Value);
            m_parentInvoker = invoker;
        }

        public void SetParentInvoker(eSpaceStatus invoker)
        {
            m_parentInvoker = invoker;
        }

        //注册监听事件
        internal void RegistEventListener()
        {
            try
            {
                //IECSEventCallback callBackHandler = new IECSExternEventHandler() as IECSEventCallback;
                //if (callBackHandler != null)
                //{
                CtrlBusiness.MyApp.RegisterEventListener(ECSEventType.APP_SHUTDOWN, m_guid, this);
                CtrlBusiness.MyApp.RegisterEventListener(ECSEventType.CONTACT_STATE_CHANGE, m_guid, this);
                CtrlBusiness.MyApp.RegisterEventListener(ECSEventType.LOGIN_STATE_CHANGE, m_guid, this);
                //}
            }
            catch (Exception ex)
            {
                //throw ex;
            }
        }

        //撤销监听事件
        internal void UnregistEventListener()
        {
            try
            {
                //IECSEventCallback callBackHandler = new IECSExternEventHandler() as IECSEventCallback;
                //if (callBackHandler != null)
                //{
                CtrlBusiness.MyApp.UnregisterEventListener(ECSEventType.APP_SHUTDOWN, m_guid, this);
                CtrlBusiness.MyApp.UnregisterEventListener(ECSEventType.CONTACT_STATE_CHANGE, m_guid, this);
                CtrlBusiness.MyApp.UnregisterEventListener(ECSEventType.LOGIN_STATE_CHANGE, m_guid, this);
                //}
            }
            catch (Exception ex)
            {
                //throw ex;
            }
        }

        //eSpace回调
        public void OnEventReceived(ECSEventType eventId, ref object param1, ref object param2, ref object param3)
        {

            switch (eventId)
            {
                case ECSEventType.APP_SHUTDOWN:
                    CtrlBusiness.MyApp = null;
                    StatusCtrlChange(eventId, param1, param2);
                    break;
                case ECSEventType.CONTACT_STATE_CHANGE:
                    StatusCtrlChange(eventId, param1, param2);
                    break;
                case ECSEventType.LOGIN_STATE_CHANGE:
                    StatusCtrlChange(eventId, param1, param2);
                    break;
                default:
                    break;
            }
        }

        //触发状态呈现控件更新
        //espace从离线转为上线状态，先会触发登录状态
        private void StatusCtrlChange(ECSEventType eventId, object param1, object param2)
        {
            if (null != this.m_parentInvoker)
            {
                if (eventId == ECSEventType.CONTACT_STATE_CHANGE)
                {
                    String account = null;
                    int status = 0;
                    account = Convert.ToString(param1);
                    status = Convert.ToInt32(param2);
                    if (account.Equals(m_parentInvoker.UCAccount))
                    {
                        m_parentInvoker.Status = status;
                    }
                }
                else if (eventId == ECSEventType.LOGIN_STATE_CHANGE)
                {
                    int onlineTag = Convert.ToInt32(param1);
                    if (onlineTag == 0)//0表示上线，1表示离线
                    {
                        m_parentInvoker.FetchStatus();
                    }
                    else
                    {
                        m_parentInvoker.Status = 0;//离线状态
                    }
                }
                else
                {
                    m_parentInvoker.Status = 0;//离线状态
                }
            }

        }
    }
}
