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

public class InformationPanelScript : MonoBehaviour
{
    public Button quitButton;
    public Text currentPlayerText;

    public Button swedishButton;
    public Button englishButton;

    public Button logoutButton;

    public bool returnToMenu = false;

    // Start is called before the first frame update
    void Start() {
        quitButton.onClick.AddListener(quitButtonAction);
        swedishButton.onClick.AddListener(() => SwitchLanguage(0));
        englishButton.onClick.AddListener(() => SwitchLanguage(1));
        if (logoutButton != null) {
            logoutButton.onClick.AddListener(() => {
                Database.Instance.LogoutUser();
                SceneManager.LoadScene("User");
            });
        }
    }

    public void SetCurrentPlayer(string playerName) {
        currentPlayerText.text = playerName;
    }

    void quitButtonAction() {
        if (returnToMenu) {
            SceneManager.LoadSceneAsync("MainMenu");
        } else {
            Application.Quit();
        }
    }

    private void SwitchLanguage(int langId) {
        LocalizationManager.Instance.currentLanguageID = langId;
        TextLocalization[] texts = Resources.FindObjectsOfTypeAll(typeof(TextLocalization)) as TextLocalization[];
        foreach (TextLocalization tl in texts) {
            tl.UpdateText();
        }

        InputFieldLocalization[] inputFields = FindObjectsOfType(typeof(InputFieldLocalization)) as InputFieldLocalization[];
        foreach (InputFieldLocalization ifl in inputFields) {
            ifl.UpdateText();
        }

        ButtonLocalization[] buttonLocale = FindObjectsOfType(typeof(ButtonLocalization)) as ButtonLocalization[];
        foreach (ButtonLocalization bl in buttonLocale) {
            bl.UpdateText();
        }
    }
}