using System;
using System.Collections.Generic;
using System.Linq;
using NBF.Setting;

namespace NBF
{
    public class Settings : MonoService<Settings>, ISettings
    {
        private readonly Dictionary<string, List<OptionBase>> _dictionary = new Dictionary<string, List<OptionBase>>();

        protected override void OnAwake()
        {
            List<OptionBase> list = new List<OptionBase>();
            var options = Reflection.GetAllNonAbstractDerivedTypes<OptionBase>();
            foreach (var option in options)
            {
                var settingOption = Activator.CreateInstance(option);
                if (settingOption is OptionBase optionSetting)
                {
                    list.Add(optionSetting);
                    optionSetting.Initialize(this);
                }
            }

            list.SortBySortAttribute();

            AddRangeToTab(list);
        }

        public void LoadAllSettings()
        {
            foreach (var list in _dictionary.Values)
            {
                foreach (var option in list)
                {
                    option.Load();
                }
            }
        }

        /// <summary>
        /// 是否有未保存的设置
        /// </summary>
        public bool HaveNotAppleSettings()
        {
            return _dictionary.Values.SelectMany(list => list).Any(option => option.HaveNotApple());
        }

        #region 设置组

        public T GetSettingOption<T>() where T : OptionBase
        {
            foreach (var option in _dictionary.Values.SelectMany(options => options))
            {
                if (option is T optionSetting)
                {
                    return optionSetting;
                }
            }

            return null;
        }

        /// <summary>
        /// 获取指定 Tab 的所有 OptionBase
        /// </summary>
        public List<OptionBase> GetOptionsByTab(string group)
        {
            if (string.IsNullOrEmpty(group))
            {
                throw new ArgumentException("Tab name cannot be null or empty.");
            }

            return _dictionary.TryGetValue(group, out var options) ? options : new List<OptionBase>();
        }

        /// <summary>
        /// 获取所有 Tab 名字列表
        /// </summary>
        public IEnumerable<string> GetAllTabs()
        {
            return _dictionary.Keys;
        }

        /// <summary>
        /// 将 OptionBase 按 Tab 分类存入字典
        /// </summary>
        private void AddToTab(OptionBase option)
        {
            if (option == null) return;

            if (string.IsNullOrEmpty(option.Tab)) return;

            // 如果字典中没有该 Tab，则新建一个 List 并加入字典
            if (!_dictionary.ContainsKey(option.Tab))
            {
                _dictionary[option.Tab] = new List<OptionBase>();
            }

            // 将 Option 添加到对应的 Tab List 中
            _dictionary[option.Tab].Add(option);
        }

        /// <summary>
        /// 批量添加 OptionBase 到字典
        /// </summary>
        private void AddRangeToTab(IEnumerable<OptionBase> options)
        {
            if (options == null) return;

            foreach (var option in options)
            {
                AddToTab(option);
            }
        }

        #endregion
    }
}