using Intrepid.Gameplay.Themes;
using Intrepid.Structures;
using Intrepid.Utilities;
using UnityEngine;

namespace Intrepid.Gameplay.Entities.Player.PDA.UI.Applications.ThemeChanger
{
    public class ThemeSelectorUI : MonoBehaviour
    {
        [GroupConfigurations, SerializeField] private ThemeSelectionAreaUI prefabThemeSelectionAreaUI;
        [GroupInjections, SerializeField] private ThemeSelectionAreaUI up;
        [GroupInjections, SerializeField] private ThemeSelectionAreaUI down;
        [GroupInjections, SerializeField] private ThemeSelectionAreaUI focus;

        private StackBelt<Theme> StackThemes { get; } = new();

        public void Load(PDADevice device)
        {
            var currentTheme = ThemeManager.Instance.CurrentTheme;
            foreach (var theme in device.GetThemesKnown())// .Where(t => !currentTheme.Equals(t)))
            {
                StackThemes.Push(theme);
            }
            UpdateContent();
        }
        
        private void Switch()
        {
            up.Dialog.Downy();
            down.Dialog.Upy();
            focus.Dialog.BlinkBorderSelected();
        }

        private void UpdateContent()
        {
            focus.Theme = StackThemes.Focus;
            up.Theme = StackThemes.PeekLeft;
            down.Theme = StackThemes.PeekRight;
            
            up.NotOperational();
            down.NotOperational();
            focus.Operational(true);
        }

        public void SwitchUp()
        {
            Switch();
            StackThemes.NextLeft();
            UpdateContent();
        }

        public void SwitchDown()
        {
            Switch();
            StackThemes.NextRight();
            UpdateContent();
        }

        public void Select()
        {
            ThemeManager.Instance.CurrentTheme = StackThemes.Focus;
        }
    }
}