using UnityEngine;

namespace WS
{
    [ObjectSystem]
    public class ClientLinkComponentAwake : AwakeSystem<ClientLinkComponent>
    {
        public override void Awake(ClientLinkComponent self)
        {
            self.Awake();
        }
    }
    [ObjectSystem]
    public class ClientLinkComponentUpdate : UpdateSystem<ClientLinkComponent>
    {
        public override void Update(ClientLinkComponent self)
        {
            self.Update();
        }
    }
    public enum ClientState
    {
        None,
        Client,
        ClientFail,
    }
    ///<summary>客户端链接</summary>
    public class ClientLinkComponent : WSComponent
    {
        public static ClientLinkComponent Instance;
        ///<summary>Session链接</summary>
        public Session ClientSession { get; set; }

        ///<summary>协议管理</summary>
        public ProtoManager Proto { get { return NetworkComponent.Instance.Proto; } }

        ///<summary>网络消息事件</summary>
        public NetworkEvent Event { get { return NetworkComponent.Instance.Event; } }
        public ClientState clientState = ClientState.None;
        public void Awake()
        {
            Instance = this;
        }
        public void LinkServer(string address)
        {
            ClientSession = NetworkComponent.Instance.CreateSession(address);
            ClientSession.OnLinkCallback = OnLinkCallback;
            ClientSession.OnErrorCallback = OnErrorCallback;
        }

        ///<summary>链接成功回调</summary>
        private void OnLinkCallback()
        {
            clientState = ClientState.Client;
        }

        ///<summary>网络错误回调</summary>
        private void OnErrorCallback(int e)
        {
            clientState = ClientState.ClientFail;
        }

        public void Update()
        {
            if (Application.internetReachability == NetworkReachability.NotReachable)
            {
                //当前设备网络不可用
            }
        }
    }
}