using Assets.Cyp;
using Assets.Scripts;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class LoginController : MonoBehaviour
{
    [SerializeField] InputField phone;
    [SerializeField] InputField pwd;
    [SerializeField] Button login;
    [SerializeField] RawImage loading;
    // Start is called before the first frame update
    void Start()
    {
        if (login != null) 
        {
            login.onClick.AddListener(Submit);
        }
        if (loading != null) 
        {
            loading.texture.wrapMode = TextureWrapMode.Repeat;
        }
    }
    
    async void Submit() 
    {
        if (phone == null || pwd == null)
        {
            Utils.showToast(gameObject, "未绑定", 1);
            return;
        }
        //if (string.IsNullOrEmpty(phone.text) || string.IsNullOrEmpty(pwd.text))
        //{
        //    Utils.showToast(gameObject, "请输入信息", 1);
        //    return;
        //}
        //var res = await Global.userApi.Login(phone.text, pwd.text, "");
        //测试用
        var res = await ConfigHelper.userApi.Login("15651831367", "123456", "");
        //var res = await NoAuthApi.Login(phone.text, pwd.text);
        if (res.result)
        {
            ConfigHelper.CurrentUser = res.data;
            SceneManager.LoadScene("MainScene");
        }
        else 
        {

        }
    }
    // Update is called once per frame
    void Update()
    {
        if (phone.isFocused && pwd != null && Input.GetKeyDown(KeyCode.Tab))
        {
            pwd.ActivateInputField();
        }
        //if (Input.GetKeyDown(KeyCode.Return) && )
        //{
        //}
        //if (Input.GetKeyDown(KeyCode.Return)) {
        //    Debug.Log("回车键");
        //    if (pwd.isFocused) 
        //    {
        //        this.Submit();
        //    }
        //}
    }
    
}
