using UnityEngine;
using UnityEngine.UI;
using TMPro;
using IStalk.Core;

namespace IStalk.UI
{
    public class LobbyUI : MonoBehaviour
    {
        [Header("Panels")]
        [SerializeField] private GameObject _mainMenuPanel;
        [SerializeField] private GameObject _lobbyPanel;

        [Header("Main Menu")]
        [SerializeField] private TMP_InputField _playerNameInput;
        [SerializeField] private Button _createRoomBtn;
        [SerializeField] private TMP_InputField _roomCodeInput;
        [SerializeField] private Button _joinRoomBtn;

        [Header("Lobby")]
        [SerializeField] private TextMeshProUGUI _roomCodeDisplay;
        [SerializeField] private TextMeshProUGUI _playerCountText;
        [SerializeField] private Button _startGameBtn;
        [SerializeField] private TextMeshProUGUI _statusText;

        private void Start()
        {
            _mainMenuPanel.SetActive(true);
            _lobbyPanel.SetActive(false);

            _createRoomBtn.onClick.AddListener(OnCreateRoom);
            _joinRoomBtn.onClick.AddListener(OnJoinRoom);
            _startGameBtn.onClick.AddListener(OnStartGame);

            var lobby = LobbyManager.Instance;
            lobby.OnRoomCreated += ShowLobby;
            lobby.OnRoomJoined += ShowLobby;
            lobby.OnPlayerCountChanged += UpdatePlayerCount;
            lobby.OnLobbyError += ShowError;
            lobby.OnGameStarting += OnGameStarting;

            lobby.ConnectToRelay();
        }

        private void OnCreateRoom()
        {
            string name = _playerNameInput.text;
            if (string.IsNullOrWhiteSpace(name)) name = "Player";
            LobbyManager.Instance.CreateRoom(name);
            SetStatus("Creating room...");
        }

        private void OnJoinRoom()
        {
            string name = _playerNameInput.text;
            string code = _roomCodeInput.text.ToUpper();
            if (string.IsNullOrWhiteSpace(name)) name = "Player";
            if (string.IsNullOrWhiteSpace(code)) return;
            LobbyManager.Instance.JoinRoom(code, name);
            SetStatus("Joining...");
        }

        private void OnStartGame()
        {
            LobbyManager.Instance.StartGame();
            _startGameBtn.interactable = false;
            SetStatus("Starting game...");
        }

        private void ShowLobby(string roomCode)
        {
            _mainMenuPanel.SetActive(false);
            _lobbyPanel.SetActive(true);
            _roomCodeDisplay.text = roomCode;
            _startGameBtn.gameObject.SetActive(Network.GameStartData.IsHost);
        }

        private void UpdatePlayerCount(int count)
        {
            _playerCountText.text = $"Players: {count}";
            _startGameBtn.interactable = count >= 2;
        }

        private void ShowError(string error)
        {
            SetStatus($"Error: {error}");
        }

        private void OnGameStarting()
        {
            SetStatus("Loading game...");
        }

        private void SetStatus(string msg)
        {
            if (_statusText != null) _statusText.text = msg;
        }

        private void OnDestroy()
        {
            if (LobbyManager.Instance == null) return;
            LobbyManager.Instance.OnRoomCreated -= ShowLobby;
            LobbyManager.Instance.OnRoomJoined -= ShowLobby;
            LobbyManager.Instance.OnPlayerCountChanged -= UpdatePlayerCount;
            LobbyManager.Instance.OnLobbyError -= ShowError;
            LobbyManager.Instance.OnGameStarting -= OnGameStarting;
        }
    }
}
