using System.Collections;
using Agora.Rtc.LitJson;
using Newtonsoft.Json.Linq;
using SC.XR.Unity;
using UnityEngine;
using UnityEngine.UI;

namespace GHZLangChao
{
    public class LangChaoLogin : AbstractController
    {
        private SCInputField mAccount_Input;
        private SCInputField mPassword_Input;
        private Button mLogin_Btn;
        private GameObject mLoginError_go;

        private string mAccountStr;
        private string mPasswordStr;

        private void Awake()
        {
            mAccount_Input = transform.Find("Account/SCInputField").GetComponent<SCInputField>();
            mPassword_Input = transform.Find("Password/SCInputField").GetComponent<SCInputField>();
            mLogin_Btn = transform.Find("LoginBtn").GetComponent<Button>();
            mLoginError_go = transform.Find("LoginError").gameObject;

            mAccount_Input.onEndEdit.AddListener(OnAccountEndEdit);
            mPassword_Input.onEndEdit.AddListener(OnPasswordEndEdit);
            mLogin_Btn.onClick.AddListener(OnClickLogin);
#if UNITY_EDITOR
            OnClickLogin();
#endif
        }

        /// <summary>
        /// 账号输入完成
        /// </summary>
        private void OnAccountEndEdit(string str)
        {
            mAccountStr = str;
        }

        /// <summary>
        /// 密码输入完成
        /// </summary>
        private void OnPasswordEndEdit(string str)
        {
            mPasswordStr = str;
        }

        /// <summary>
        /// 登录
        /// </summary>
        private void OnClickLogin()
        {
#if UNITY_EDITOR
            mAccountStr = "cy1";
            mPasswordStr = "1";
            mAccount_Input.text = mAccountStr;
            mPassword_Input.text = mPasswordStr;
#endif

            if (string.IsNullOrWhiteSpace(mAccountStr) || string.IsNullOrWhiteSpace(mPasswordStr)) return;
            EndustryLoginUserInfo sendLogin = new EndustryLoginUserInfo()
            {
                account = mAccountStr,
                password = mPasswordStr
            };
            string jsonLogin = JsonMapper.ToJson(sendLogin);
            HttpTool.Instance.PostLogin(HttpActionLang.login, jsonLogin, LoginCallBack);
        }

        /// <summary>
        /// 登录返回数据
        /// </summary>
        public void LoginCallBack(string mes)
        {
            JObject obj = JObject.Parse(mes);
            if (obj["code"].ToString() != "200")
                StartCoroutine(PopUp());
            else
            {
                if (!string.IsNullOrWhiteSpace(obj["data"].ToString()) && !string.IsNullOrWhiteSpace(obj["data"]["token"].ToString()))
                {
                    login.UserInfo.Instance.Token = obj["data"]["token"].ToString();
                    ScenesManager.Instance.showWindow(ScenesManager.SceneType.ShowChoose);

                    MQTTManager.Instance.SetAccoutInfo(mAccountStr);
                   
                    GameStart.Instance.StartCoroutine(HttpTool.Instance.SendHttp(HttpActionLang.userinfo, "", UserInfoCallBack));
                }
                else
                    StartCoroutine(PopUp());
            }
        }

        private IEnumerator PopUp()
        {
            mLoginError_go.SetActive(true);
            yield return new WaitForSeconds(1);
            mLoginError_go.SetActive(false);
        }

        private void UserInfoCallBack(string msg)
        {
            Debug.Log(msg);

            JObject obj = JObject.Parse(msg);
            if (obj["code"].ToString() == "200")
            {
                MQTTManager.Instance._username = obj["data"]["name"].ToString();
                MQTTManager.Instance.roleId = obj["data"]["roleId"].ToString();
                MQTTManager.Instance.email = obj["data"]["email"].ToString();
                MQTTManager.Instance.phone = obj["data"]["phone"].ToString();
                MQTTManager.Instance.resourcePool = obj["data"]["resourcePool"].ToString();
                MQTTManager.Instance.expertType = obj["data"]["expertType"].ToString();
                MQTTManager.Instance.room = obj["data"]["room"].ToString();
              

                MQTTManager.Instance.Signal();
            }
            else
            {
                Debug.LogError("获取用户信息失败 !!!" + obj["code"].ToString());
            }
        }
    }



}