using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Networking.Client;
using TMPro;

public class LoginUIManager : MonoBehaviour
{
    [Header("References")]
    [SerializeField]
    private ClientLoginManager clientManager;
    [SerializeField]
    private TextMeshProUGUI Title;
    [SerializeField]
    private GameObject Content;

    [Header("Connection")]
    [SerializeField]
    private string ConnectionTitle = "Connect to Server";
    [SerializeField]
    private GameObject ConnectionObject;

    [Header("PlayerDetails")]
    [SerializeField]
    private string DetailsTitle = "Player Details";
    [SerializeField]
    private GameObject DetailsObject;

    [Header("WaitForPlayers")]
    [SerializeField]
    private string WaitTitle = "Wait for Players";
    [SerializeField]
    private string SceneToLoad;
    [SerializeField]
    private GameObject WaitObject;


    private string serverAddress;
    private int serverPort;
    private string playerName;
    private Color playerColor;


    #region Unity Functions
    private void OnEnable()
    {
        clientManager.OnConnectedToServer += OnConnect;
        clientManager.OnLoginSucess += OnLogin;
        clientManager.OnLoginFail += OnLoginFail;
    }

    private void OnDisable()
    {
        clientManager.OnConnectedToServer -= OnConnect;
        clientManager.OnLoginSucess -= OnLogin;
        clientManager.OnLoginFail -= OnLoginFail;
    }

    private void Start()
    {
        serverAddress = PlayerPrefs.GetString("LastUsedAddress", "");
        serverPort = PlayerPrefs.GetInt("LastUsedPort", 2222);
        playerName = PlayerPrefs.GetString("LastUsedName");

        OnLoginFail();
    }
    #endregion Unity Functions


    #region Event Functions
    public void OnConnect()
    {
        Title.text = DetailsTitle;

        CloseAll();
        DetailsObject.SetActive(true);
    }

    public void OnLogin()
    {
        Title.text = WaitTitle;
        CloseAll();
        WaitObject.SetActive(true);
    }

    public void OnLoginFail()
    {
        Title.text = ConnectionTitle;

        CloseAll();
        ConnectionObject.SetActive(true);
    }
    #endregion Event Functions

    #region UI Functionality
    public void OnChange_ServerAddress(string serverAddress)
    {
        this.serverAddress = serverAddress;
    }

    public void OnChange_ServerPort(string serverPort)
    {
        this.serverPort = int.Parse(serverPort);
    }

    public void OnClick_Connect()
    {
        clientManager.StartClient(serverAddress, serverPort);
        PlayerPrefs.SetString("LastUsedAddress", serverAddress);
        PlayerPrefs.SetInt("LastUsedPort", serverPort);
        PlayerPrefs.Save();
    }

    public void OnClick_Done()
    {
        clientManager.SendPlayerDetails(playerName, playerColor);
        PlayerPrefs.SetString("LastUsedName", playerName);
    }

    public void OnClick_Color(Color playerColor)
    {
        this.playerColor = playerColor;
    }

    public void OnChange_Name(string playerName)
    {
        this.playerName = playerName;
    }

    public void OnClick_Ready()
    {
        UnityEngine.SceneManagement.SceneManager.LoadScene(SceneToLoad);
    }
    #endregion UI Functionality

    #region Helper Functions

    private void CloseAll()
    {
        foreach (Transform child in Content.transform)
            child.gameObject.SetActive(false);
    }

    #endregion Helper Functions

}
