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

public class SettingsScript : MonoBehaviour
{

    [SerializeField] GameObject LangaugeSelectBox;
    // Start is called before the first frame update

    private Dropdown languageSelector;
    void Start()
    {
        languageSelector = LangaugeSelectBox.GetComponent<Dropdown>();
        languageSelector.onValueChanged.AddListener(delegate { languageSelectionChange(languageSelector); });
        setLanguages();

    }

    private void languageSelectionChange(Dropdown selection) {
        PlayerPrefs.SetString("SelectedLanguage", selection.options[selection.value].text);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void setLanguages() {
        languageSelector.options.Clear();
        languageSelector.AddOptions(LocalizationManager.Instance.languages.Select(l => l.languageString).ToList<string>());

        string currentLang = PlayerPrefs.GetString("SelectedLanguage");
        if (!String.IsNullOrEmpty(currentLang)) {
            languageSelector.SetValueWithoutNotify(languageSelector.options.IndexOf(languageSelector.options.Where(od => od.text.Equals(currentLang)).First()));
        }
    }
}
