using JetBrains.Annotations;
using NEG.UI.UnityUi.Buttons;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
namespace NEG.UI.UnityUi
{
    [PublicAPI]
    public class CarouselList : MonoBehaviour
    {
        /// 
        /// Current option
        /// 
        public string CurrentOption { get; private set; }
        
        /// 
        /// Current selected option id
        /// 
        public int CurrentOptionId { get; private set; }
        [SerializeField] private BaseButton nextButton;
        [SerializeField] private BaseButton prevButton;
        [SerializeField] private TMP_Text currentOptionText;
        private List options;
        /// 
        /// Sets new options list, automatically first will be selected.
        /// 
        /// list of options names
        public void SetOptions(List options)
        {
            this.options = options;
            SelectOption(0);
        }
        public void SelectNextOption() => ChangeOption(true);
        public void SelectPrevOption() => ChangeOption(false);
        
        /// 
        /// Selects option with provided id.
        /// 
        /// option number
        public void SelectOption(int option)
        {
            if (option < 0 || option >= options.Count)
            {
                Debug.LogError("Invalid option number");
                return;
            }
            CurrentOptionId = option;
            CurrentOption = options[option];
            currentOptionText.text = CurrentOption;
        }
        /// 
        /// Select option with provided value. Use with caution, better use .
        /// 
        /// option value to select
        public void SelectOption(string option)
        {
            if (options.Count == 0)
            {
                Debug.LogError("Carousel List cannot be empty when selecting option");
                return;
            }
            int index = options.IndexOf(option);
            if (index == -1)
            {
                Debug.LogError($"Option {option} not found");
                return;
            }
            
            SelectOption(index);
        }
        private void Awake()
        {
            nextButton.OnButtonPressed += SelectNextOption;
            prevButton.OnButtonPressed += SelectPrevOption;
        }
        private void ChangeOption(bool next) => SelectOption((CurrentOptionId + (next ? 1 : -1) + options.Count) % options.Count);
    }
}