using TMPro;
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using RothenburgAR.Common;

namespace RothenburgAR.UI
{
    public class ColorSelectViewBehaviour : BaseViewBehaviour
    {
        public GameObject ColorSelectCircle;

        void Start()
        {
            CreateColorSelectionCircles();
        }

        private void CreateColorSelectionCircles()
        {
            var colorSchemes = UIColorSchemeManager.Instance.AvailableUIColorSchemes;
            for (var i = 0; i < colorSchemes.Count; i++)
            {
                var id = i;
                var baseColor = colorSchemes[i].BaseColorOpaque;
                var selectionCircle = InstatiateSelectionCircle(id, baseColor);

                selectionCircle.GetComponentInChildren<Button>().onClick.AddListener(() =>
                {
                    UIColorSchemeManager.Instance.SetUiColorScheme(id);
                    if (!UIManager.Instance.StartingUp) SettingsManager.Instance.PersistSettings();

                    if (UIManager.Instance.StartingUp) SwitchToFontsizeSelectView();
                    else SwitchToARView();
                });

                var label = LanguageManager.Instance.GetString(colorSchemes[i].Name);
                selectionCircle.GetComponentInChildren<TextMeshProUGUI>().SetText(label);
            }
        }

        private GameObject InstatiateSelectionCircle(int schemeId, Color baseColor)
        {
            var selectionCircle = GameObject.Instantiate(ColorSelectCircle, ColorSelectCircle.transform.parent, true);

            selectionCircle.name = "Color:" + schemeId;
            selectionCircle.GetComponentInChildren<Image>().color = baseColor;
            selectionCircle.SetActive(true);

            return selectionCircle;
        }
    }
}