using PLClass;
using System;
using System.ComponentModel;
using System.Threading.Tasks;

namespace PlaneGcsSdk.Contract.DtuClient
{
    partial class EhNetLoginMgr
    {
        #region Fileds
        private PLNet _PLNet;
        private PLNet.LoginState _loginState;
        #endregion

        #region Private Methods
        private void InstanceEhNetObject()
        {
            _PLNet = new PLNet(AsyncOperationManager.CreateOperation(null));
        }

        private async Task EhNetStart(string ehNetServerIp, string username, string pwd)
        {
            _PLNet.Start(ehNetServerIp, username, pwd);
        }

        private bool CheckIfLogined()
        {
            return _loginState == PLNet.LoginState.LoginOk;
        }

        private void ResetLoginStatus()
        {
            _loginState = PLNet.LoginState.LoginNoUser;
        }

        private async Task SendCommandToDtuServer(string msg)
        {
            _PLNet.SendCommand(DtuServerUsername, msg);
        }
        #endregion

        #region Event Handlers
        private void _ehNet_Login(PLNet.LoginState loginState)
        {
            _loginState = loginState;
            if (_loginState == PLNet.LoginState.LoginOk)
            {
                NickName = _PLNet.MyNick;
                _loginEnNet.Set();
            }
        }

        //private void _ehNet_ExceptionThrown(object sender, PLNet.UWP.ExceptionThrownEventArgs e)
        //{
        //    RaiseExceptionThrown(e.Exception);
        //}

        private void _ehNet_DiscoveredOneUser(PLNet.UserProperty userProperty)
        {
        }

        private void _ehNet_Disconnect(PLNet e)
        {
            _loginState = PLNet.LoginState.LoginOtherError;
            NickName = null;
        }

        private void _ehNet_CommandArrival(string friendID, string msg)
        {
            AcceptProtocolCommand(friendID, msg);
            Console.WriteLine(msg);
        }
        #endregion
    }
}
