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

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

    public Button settingsButton;

    public Button logoutButton;
    [SerializeField] Button BackButton;

    [SerializeField] bool settingsButtonVisible = false;
    [SerializeField] bool logoutButtonVisible = false;
    [SerializeField] bool quitButtonVisible = false;
    [SerializeField] bool backButtonVisible = false;
    [SerializeField] bool signedInPlayerVisible = false;

    // Start is called before the first frame update
    void Start() {
        setButtonVisibilities();
        quitButton.onClick.AddListener(quitButtonAction);
        BackButton.onClick.AddListener(backButtonAction);
        settingsButton.onClick.AddListener(OpenSettings);
        loggedInPlayerText.text = Database.Instance.GetSignedInUser().Value;
        if (logoutButton != null) {
            logoutButton.onClick.AddListener(() => {
                Database.Instance.LogoutUser();
                SceneManager.LoadScene("User");
            });
        }
    }

    private void setButtonVisibilities() {
        settingsButton.gameObject.SetActive(settingsButtonVisible);
        quitButton.gameObject.SetActive(quitButtonVisible);
        logoutButton.gameObject.SetActive(logoutButtonVisible);
        BackButton.gameObject.SetActive(backButtonVisible);
        loggedInPlayerText.gameObject.SetActive(signedInPlayerVisible);
    }

    void quitButtonAction() {
        Application.Quit();
    }

    void backButtonAction() {
        SceneManager.LoadSceneAsync("MainMenu");
    }

    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();
        }
    }

    private void OpenSettings() {
        SceneManager.LoadSceneAsync("Settings");
    }

    public Button getBackButton() {
        return BackButton;
    }
}