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

public class LoginManager : MonoBehaviour
{
    public TMP_InputField login_username;
    public TMP_InputField login_password;
    public Toggle rememberMe;
    public Button loginBtn;
    public TMP_InputField reg_username;
    public TMP_InputField reg_password;
    public Button regBtn;

    public OtherUIbuttons otherUI;
    public BuildingData defaultBuilding;

    void Start()
    {
        loginBtn.onClick.AddListener(OnLoginClicked);
        regBtn.onClick.AddListener(OnRegisterClicked);

        if(PlayerPrefs.HasKey("username") && PlayerPrefs.HasKey("password")){
            login_username.text = PlayerPrefs.GetString("username");
            login_password.text = PlayerPrefs.GetString("password");

        OnLoginClicked();
        }

    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void OnLoginClicked(){
        if(login_username.text.Length < 2){
            if(otherUI!=null){
                otherUI.ErrorAuth();
            }
         //   MessageDialogInstance.messageDialog.ShowDialog("Error", "Please use a valid Username (should be more than 2 characters)");
            return;
        }
        if(login_password.text.Length < 5){
            if(otherUI!=null){
                otherUI.ErrorAuth();
            }
          //  MessageDialogInstance.messageDialog.ShowDialog("Error", "Please use a Strong password (should be more than 5 characters)");
            return;
        }

        StartCoroutine(LoginPlayer());
    }
    public void OnRegisterClicked(){
        if(reg_username.text.Length < 2){
            MessageDialogInstance.messageDialog.ShowDialog("Error", "Please use a valid Username (should be more than 2 characters)");
            return;
        }
        if(reg_password.text.Length < 5){
            MessageDialogInstance.messageDialog.ShowDialog("Error", "Please use a Strong password (should be more than 5 characters)");
            return;
        }
        Debug.Log("Clicked register");
        StartCoroutine(Register());
    }


    IEnumerator LoginPlayer()
    {
        loginBtn.interactable=false;
        WWWForm form = new WWWForm();
        form.AddField("name", login_username.text);
        form.AddField("password", login_password.text);

        WWW www = new WWW(DBmanager.phpRoot+"login.php", form);
        yield return www;
        Debug.Log(www.text);
        if (www.text[0] == '0')
        {
            if(rememberMe.isOn){
            PlayerPrefs.SetString("username", login_username.text);
            PlayerPrefs.SetString("password", login_password.text);
            PlayerPrefs.Save();}
            DBmanager.username = login_username.text;
            DBmanager.SetPurchasedSkins(www.text.Split('\t')[9]);
            DBmanager.GetInventoryFromServer(www.text.Split('\t')[8]);

            DBmanager.SetExpPassCollected(www.text.Split('\t')[7]);
            DBmanager.SetXp(int.Parse(www.text.Split('\t')[6]),true);
            DBmanager.GetBuildingStates(www.text.Split('\t')[5]);
            DBmanager.SetOxygen(int.Parse(www.text.Split('\t')[4]),true);
            DBmanager.SetMetal(int.Parse(www.text.Split('\t')[3]),true);
            DBmanager.SetGems(int.Parse(www.text.Split('\t')[2]),true);
            DBmanager.SetCoins(int.Parse(www.text.Split('\t')[1]),true);

            Debug.Log("XP : " + DBmanager.Xp);

            UnityEngine.SceneManagement.SceneManager.LoadScene(1);
        }
        else
        {
            Debug.Log("User Login failed. Error #" + www.text);
        }
        loginBtn.interactable=true;
    }

    IEnumerator Register()
    {
        regBtn.interactable = false;
        WWWForm form = new WWWForm();
        form.AddField("name", reg_username.text);
        form.AddField("password", reg_password.text);
        Debug.Log("Registering");
        WWW www = new WWW(DBmanager.phpRoot + "register.php", form);
        yield return www;
        if (www.text == "0")
        {
            Debug.Log("User Registered succesfully");
            PlayerPrefs.SetString("username", reg_username.text);
            PlayerPrefs.SetString("password", reg_password.text);
            PlayerPrefs.Save();
            DBmanager.username = reg_username.text;
            DBmanager.AddBuilding(defaultBuilding);
            UnityEngine.SceneManagement.SceneManager.LoadScene(1);
        }
        else
        {
            Debug.Log("User creation failed " + www.text);
        }
        regBtn.interactable=true;
    }
}
