using UnityEngine;
using Subsystems;
using GeoSus.Client;
using System.ComponentModel;
using System.Threading;

namespace Subsystems
{
    public class GameManager_UI
    {
        private GameClient _gameClient;
        private Canvas _CreateJoinLobby;
        private Canvas _InLobby;
        private Canvas _LoadingScreen;
        private Canvas _GameScreen;
        public GameManager_UI(GameClient gameClient, Canvas CreateJoinLobby, Canvas InLobby, Canvas LoadingScreen, Canvas GameScreen)
        {
            _gameClient = gameClient;
            _CreateJoinLobby = CreateJoinLobby;
            _LoadingScreen = LoadingScreen;
            _GameScreen = GameScreen;
            _InLobby = InLobby;
            _CreateJoinLobby.enabled = true;
            _InLobby.enabled = false;
            _GameScreen.enabled = false;
            _LoadingScreen.enabled = false;
        }
        public void UpdateLobbyUI()
        {
            if (_gameClient.CurrentLobbyState == null)
            {
                _CreateJoinLobby.enabled = true;
                _InLobby.enabled = false;
                _GameScreen.enabled = false;
                _LoadingScreen.enabled = false;
                return;
            }
            else if (_gameClient.CurrentLobbyState.Phase == GamePhase.Loading)
            {
                _CreateJoinLobby.enabled = false;
                _InLobby.enabled = false;
                _GameScreen.enabled = false;
                _LoadingScreen.enabled = true;
                return;
            }
            else if (_gameClient.CurrentLobbyState.Phase == GamePhase.Lobby)
            {
                _InLobby.enabled = true;
                _CreateJoinLobby.enabled = false;
                var playerList = _InLobby.transform.Find("PlayerList").GetComponent<TMPro.TMP_Text>();
                playerList.text = "";
                foreach (var player in _gameClient.CurrentLobbyState.Players)
                {
                    playerList.text += player.DisplayName + "\n";
                }
                _InLobby.transform.Find("JoinCode").GetComponent<TMPro.TMP_Text>().text = _gameClient.CurrentLobbyState.JoinCode;
                return;
            }
            else if (_gameClient.CurrentLobbyState.Phase == GamePhase.Playing)
            {
                _CreateJoinLobby.enabled = false;
                _InLobby.enabled = false;
                _GameScreen.enabled = true;
                _LoadingScreen.enabled = false;
                _GameScreen.transform.Find("Role").GetComponent<TMPro.TMP_Text>().text = _gameClient.MyRole.ToString() ;
                return;
            }
            
        }
    }
}
