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

public class NoLoginScreen : MonoBehaviour
{
    public GameObject loadingScreen;
    public string sceneName;
    public InputField usernameInput;
    public Button goBtn;
    void Start()
    {
        if(!HasUsername()){
            loadingScreen.SetActive(false);
        }else{
            SceneManager.LoadScene(sceneName); 
        }

        goBtn.interactable=false;
        usernameInput.onValueChange.AddListener(OnInputChange);
        goBtn.onClick.AddListener(LetsGo);
    }
    void OnInputChange(string newVal){
        goBtn.interactable = (usernameInput.text.Length > 2);
    }
    public void LetsGo(){
        PlayerPrefs.SetString("username", usernameInput.text);
        loadingScreen.SetActive(true);
        SceneManager.LoadScene(sceneName);
    }

    bool HasUsername(){
        if(!PlayerPrefs.HasKey("username")){
            return false;
        }

        if(PlayerPrefs.GetString("username").Length < 1){
            return false;
        }

        return true;
    }
}
