using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class PVPModePopup : PopupBase
{
    [Header("Buttons")]
    [SerializeField] private Button twoPlayerBtn;
    [SerializeField] private Button threePlayerBtn;
    [SerializeField] private Button fourPlayerBtn;
    [SerializeField] private Button playBtn;

    [Header("Input Field")]
    [SerializeField] private TMP_InputField playerOneNameInput;
    [SerializeField] private TMP_InputField playerTwoNameInput;
    [SerializeField] private TMP_InputField playerThreeNameInput;
    [SerializeField] private TMP_InputField playerFourNameInput;

    [Header("Player Names")]
    private string playerOneName;
    private string playerTwoName;
    private string playerThreeName;
    private string playerFourName;

    private int selectedPlayerCount;
    public int SelectedPlayerCount => selectedPlayerCount;

    private void OnEnable()
    {
        twoPlayerBtn.onClick.AddListener(() => OnPlayerCountSelected(2));
        threePlayerBtn.onClick.AddListener(() => OnPlayerCountSelected(3));
        fourPlayerBtn.onClick.AddListener(() => OnPlayerCountSelected(4));
        playBtn.onClick.AddListener(OnPlayButtonClicked);
    }

    private void OnDisable()
    {
        twoPlayerBtn.onClick.RemoveAllListeners();
        threePlayerBtn.onClick.RemoveAllListeners();
        fourPlayerBtn.onClick.RemoveAllListeners();
        playBtn.onClick.RemoveAllListeners();
    }
    private void OnPlayerCountSelected(int count)
    {
        selectedPlayerCount = count;
        UpdateInputFieldsVisibility();
    }

    private void UpdateInputFieldsVisibility()
    {
        playerOneNameInput.gameObject.SetActive(true);
        playerTwoNameInput.gameObject.SetActive(selectedPlayerCount >= 2);
        playerThreeNameInput.gameObject.SetActive(selectedPlayerCount >= 3);
        playerFourNameInput.gameObject.SetActive(selectedPlayerCount >= 4);
    }

    private void OnPlayButtonClicked()
    {
        playerOneName = playerOneNameInput.text;
        playerTwoName = playerTwoNameInput.text;
        playerThreeName = playerThreeNameInput.text;
        playerFourName = playerFourNameInput.text;

        Debug.Log($"Starting PVP Mode with {selectedPlayerCount} players:");
        Debug.Log($"Player 1: {playerOneName}");
        Debug.Log($"Player 2: {playerTwoName}");
        if (selectedPlayerCount >= 3)
            Debug.Log($"Player 3: {playerThreeName}");
        if (selectedPlayerCount == 4)
            Debug.Log($"Player 4: {playerFourName}");

    }
}
