using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class OnlinePlayerInfoScript : MonoBehaviour
{

    public Text playerName;
    public Text playerStatus;

    public Button acceptButton;
    public Button declineButton;

    public Transform buttonPanel;
    public Transform statusPanel;

    private int gameId;

    private string PENDING = "ONLINE_PLAYER_STATUS_WAITING";
    private string ACCEPTED = "ONLINE_PLAYER_STATUS_ACCEPTED";
    private string DECLINED = "ONLINE_PLAYER_STATUS_DECLINED";

    public int GameId { get => gameId; set => gameId = value; }

    private void Start() {
        acceptButton.onClick.AddListener(AcceptGame);
        declineButton.onClick.AddListener(DeclineGame);
    }

    private void DeclineGame() {
        OnlineDatabase.Instance.DeclineOnlineGame(playerName.text, gameId);
        SetPlayerStatus("DECLINED");
        HideDecitionButtons();
    }

    private void AcceptGame() {
        OnlineDatabase.Instance.AcceptOnlineGame(playerName.text, gameId);
        SetPlayerStatus("ACCEPTED");
        HideDecitionButtons();
    }

    public void SetPlayerName(string name) {
        playerName.text = name;
    }


    public void SetPlayerStatus(string status) {
        if ("WAITING".Equals(status)) {
            playerStatus.GetComponent<TextLocalization>().key = PENDING;
            playerStatus.text = LocalizationManager.Instance.GetText(PENDING);
        } else if ("ACCEPTED".Equals(status)) {
            playerStatus.GetComponent<TextLocalization>().key = ACCEPTED;
            playerStatus.text = LocalizationManager.Instance.GetText(ACCEPTED);
        } else if ("DECLINED".Equals(status)) {
            playerStatus.GetComponent<TextLocalization>().key = DECLINED;
            playerStatus.text = LocalizationManager.Instance.GetText(DECLINED);
        }
    }

    internal void ShowDecitionButtons() {
        statusPanel.GetComponent<CanvasGroup>().alpha = 0f;
        CanvasGroup buttonCanvasGroup = buttonPanel.GetComponent<CanvasGroup>();

        buttonCanvasGroup.alpha = 1f;
        buttonCanvasGroup.interactable = true;
        buttonCanvasGroup.blocksRaycasts = true;
    }

    internal void HideDecitionButtons() {
        statusPanel.GetComponent<CanvasGroup>().alpha = 1f;
        CanvasGroup buttonCanvasGroup = buttonPanel.GetComponent<CanvasGroup>();

        buttonCanvasGroup.alpha = 0f;
        buttonCanvasGroup.interactable = false;
        buttonCanvasGroup.blocksRaycasts = false;
    }
}
