using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Multiplayer;
using TMPro;
public class HomeScreenManager : MonoBehaviour {


    public TMP_InputField IP;
    public TMP_InputField Port;
    public TMP_InputField Name;

    public GameObject Server;
    public string[] DefaultNames;

    public void Start()
    {

        int rand = Random.Range(0, DefaultNames.Length);

        IP.text = PlayerPrefs.GetString("IP", "");
        Port.text = PlayerPrefs.GetString("PORT", "4444");
        Name.text = PlayerPrefs.GetString("NAME", DefaultNames[rand]);

    }
    public void Connect()
    {
        int intVal = 0;
        bool Succeeded = int.TryParse(Port.text, out intVal);
        if (Succeeded)
        {
            //Destroy(ServerManager.Instance.gameObject);
            ClientManager.Instance.StartClient(IP.text, intVal);
        }
        PlayerPrefs.SetString("IP", IP.text);
        PlayerPrefs.SetString("PORT", Port.text);
        PlayerPrefs.SetString("NAME", Name.text);

    }

    public void Host()
    {
        Server.SetActive(true);
        int intVal = 0;

        bool Succeeded = int.TryParse(Port.text, out intVal);
        if (Succeeded)
        {
            ServerManager.Instance.StartServer();
        }
        else
        {
            Server.SetActive(false);
        }

        IP.text = IPGrabber.GetIP(ADDRESSFAM.IPv4);

        PlayerPrefs.SetString("PORT", Port.text);
        PlayerPrefs.SetString("NAME", Name.text);
        PlayerPrefs.SetString("IP", IP.text);

        //Connect();
    }

    public void ChangeName()
    {
        int rand = Random.Range(0, DefaultNames.Length);
        

        Name.text = DefaultNames[rand];
        Debug.Log("Name" + DefaultNames[rand]);
    }
}
