using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class AuthorizationController : MonoBehaviour
{
    public static string login;
    public static string password;
    public InputField Login;
    public InputField Password;
    public GameObject Error;
    public GameObject loading;

    public static bool send = false; // маяк для client
    public static bool success = false; // маяк для client
    public static bool error = false; // маяк для client
    static string textError = "Неправильный логин или пароль";

    public GameObject player;

    //Client client;

    // Start is called before the first frame update
    void Start()
    {
        //client = Client.instance;
        var waslogin = PlayerPrefs.GetString("lastlogin");
        var waspass = PlayerPrefs.GetString("lastpass");

        if (waslogin != null)
            Login.text = waslogin;
        if (waspass != null)
            Password.text = waspass;

        DebugHelper.ActivateConsole();        
    }

    // Update is called once per frame
    void Update()
    {
        if (success)
        {//SceneManager.LoadScene("Location");
            gameObject.SetActive(false);
            //client.SendGetUsers((uint)PlayerController.active_company + 1);
            //if (ModeController.editor == true && PlayerController.active_location != PlayerController.locations.Count) client.BeaconsRequest((uint)PlayerController.locations[PlayerController.active_location].id);
            //if (ModeController.editor == false) client.BeaconsRequest((uint)PlayerController.locations[PlayerController.active_location].id);
        }
        if(error) Error.SetActive(true);
        if (!Client.instance.connected) gameObject.SetActive(true);
    }

    public void Authorization()
    {
        login = Login.text;
        password = Password.text;

        PlayerPrefs.SetString("lastlogin", login);
        PlayerPrefs.SetString("lastpass", password);

        if (!string.IsNullOrEmpty(login) && !string.IsNullOrEmpty(password))
        {
            Client.instance.SendLogin(login, password);// "8*9Efc2%" "vsheiko"
            error = false;
            Error.SetActive(false);           
        }
    }
    void OnGUI()
    {
        DebugHelper.DrawConsole();
    }
}
    
