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

namespace UI
{
    public class ChampionSelector : MonoBehaviour
    {
        [Header("Configuration")]
        [SerializeField]
        private bool _defaultSelection = false;

        [SerializeField]
        private GameObject _championUIPrefab;

        [Header("Layout")]
        [SerializeField]
        private Transform _cost1Parent;

        [SerializeField]
        private Transform _cost2Parent;

        [SerializeField]
        private Transform _cost3Parent;

        [SerializeField]
        private Transform _cost4Parent;

        [SerializeField]
        private Transform _cost5Parent;

        [Header("Optional: Champion Icons")]
        [SerializeField]

        private Dictionary<ChampionsEnum, ChampionBaseUI> _championUIs =
            new Dictionary<ChampionsEnum, ChampionBaseUI>();


        // Champion definitions with display names and costs
        private readonly Dictionary<ChampionsEnum, ChampionDisplayInfo> _championDisplayInfo =
            new Dictionary<ChampionsEnum, ChampionDisplayInfo>
            {
                // Cost 1 Champions
                { ChampionsEnum.AATROX, new ChampionDisplayInfo("Aatrox", 1) },
                { ChampionsEnum.EZREAL, new ChampionDisplayInfo("Ezreal", 1) },
                { ChampionsEnum.GAREN, new ChampionDisplayInfo("Garen", 1) },
                { ChampionsEnum.GNAR, new ChampionDisplayInfo("Gnar", 1) },
                { ChampionsEnum.KALISTA, new ChampionDisplayInfo("Kalista", 1) },
                { ChampionsEnum.KAYLE, new ChampionDisplayInfo("Kayle", 1) },
                { ChampionsEnum.KENNEN, new ChampionDisplayInfo("Kennen", 1) },
                { ChampionsEnum.LUCIAN, new ChampionDisplayInfo("Lucian", 1) },
                { ChampionsEnum.MALPHITE, new ChampionDisplayInfo("Malphite", 1) },
                { ChampionsEnum.NAAFIRI, new ChampionDisplayInfo("Naafiri", 1) },
                { ChampionsEnum.RELL, new ChampionDisplayInfo("Rell", 1) },
                { ChampionsEnum.SIVIR, new ChampionDisplayInfo("Sivir", 1) },
                { ChampionsEnum.SYNDRA, new ChampionDisplayInfo("Syndra", 1) },
                { ChampionsEnum.ZAC, new ChampionDisplayInfo("Zac", 1) },
                // Cost 2 Champions
                { ChampionsEnum.DRMUNDO, new ChampionDisplayInfo("Dr. Mundo", 2) },
                { ChampionsEnum.GANGPLANK, new ChampionDisplayInfo("Gangplank", 2) },
                { ChampionsEnum.JANNA, new ChampionDisplayInfo("Janna", 2) },
                { ChampionsEnum.JHIN, new ChampionDisplayInfo("Jhin", 2) },
                { ChampionsEnum.KAISA, new ChampionDisplayInfo("Kai'Sa", 2) },
                { ChampionsEnum.KATARINA, new ChampionDisplayInfo("Katarina", 2) },
                { ChampionsEnum.KOBUKO, new ChampionDisplayInfo("Kobuko", 2) },
                { ChampionsEnum.LUX, new ChampionDisplayInfo("Lux", 2) },
                { ChampionsEnum.RAKAN, new ChampionDisplayInfo("Rakan", 2) },
                { ChampionsEnum.SHEN, new ChampionDisplayInfo("Shen", 2) },
                { ChampionsEnum.VI, new ChampionDisplayInfo("Vi", 2) },
                { ChampionsEnum.XAYAH, new ChampionDisplayInfo("Xayah", 2) },
                { ChampionsEnum.XINZHAO, new ChampionDisplayInfo("Xin Zhao", 2) },
                // Cost 3 Champions
                { ChampionsEnum.AHRI, new ChampionDisplayInfo("Ahri", 3) },
                { ChampionsEnum.CAITLYN, new ChampionDisplayInfo("Caitlyn", 3) },
                { ChampionsEnum.DARIUS, new ChampionDisplayInfo("Darius", 3) },
                { ChampionsEnum.JAYCE, new ChampionDisplayInfo("Jayce", 3) },
                { ChampionsEnum.KOGMAW, new ChampionDisplayInfo("Kog'Maw", 3) },
                { ChampionsEnum.LULU, new ChampionDisplayInfo("Lulu", 3) },
                { ChampionsEnum.MALZAHAR, new ChampionDisplayInfo("Malzahar", 3) },
                { ChampionsEnum.NEEKO, new ChampionDisplayInfo("Neeko", 3) },
                { ChampionsEnum.RAMMUS, new ChampionDisplayInfo("Rammus", 3) },
                { ChampionsEnum.SENNA, new ChampionDisplayInfo("Senna", 3) },
                { ChampionsEnum.SMOLDER, new ChampionDisplayInfo("Smolder", 3) },
                { ChampionsEnum.SWAIN, new ChampionDisplayInfo("Swain", 3) },
                { ChampionsEnum.UDYR, new ChampionDisplayInfo("Udyr", 3) },
                { ChampionsEnum.VIEGO, new ChampionDisplayInfo("Viego", 3) },
                { ChampionsEnum.YASUO, new ChampionDisplayInfo("Yasuo", 3) },
                { ChampionsEnum.ZIGGS, new ChampionDisplayInfo("Ziggs", 3) },
                // Cost 4 Champions
                { ChampionsEnum.AKALI, new ChampionDisplayInfo("Akali", 4) },
                { ChampionsEnum.ASHE, new ChampionDisplayInfo("Ashe", 4) },
                { ChampionsEnum.JARVANIV, new ChampionDisplayInfo("Jarvan IV", 4) },
                { ChampionsEnum.JINX, new ChampionDisplayInfo("Jinx", 4) },
                { ChampionsEnum.KARMA, new ChampionDisplayInfo("Karma", 4) },
                { ChampionsEnum.KSANTE, new ChampionDisplayInfo("K'Sante", 4) },
                { ChampionsEnum.LEONA, new ChampionDisplayInfo("Leona", 4) },
                { ChampionsEnum.POPPY, new ChampionDisplayInfo("Poppy", 4) },
                { ChampionsEnum.RYZE, new ChampionDisplayInfo("Ryze", 4) },
                { ChampionsEnum.SAMIRA, new ChampionDisplayInfo("Samira", 4) },
                { ChampionsEnum.SETT, new ChampionDisplayInfo("Sett", 4) },
                { ChampionsEnum.VOLIBEAR, new ChampionDisplayInfo("Volibear", 4) },
                { ChampionsEnum.YUUMI, new ChampionDisplayInfo("Yuumi", 4) },
                // Cost 5 Champions
                { ChampionsEnum.BRAUM, new ChampionDisplayInfo("Braum", 5) },
                { ChampionsEnum.EKKO, new ChampionDisplayInfo("Ekko", 5) },
                { ChampionsEnum.GWEN, new ChampionDisplayInfo("Gwen", 5) },
                { ChampionsEnum.LEESIN, new ChampionDisplayInfo("Lee Sin", 5) },
                { ChampionsEnum.SERAPHINE, new ChampionDisplayInfo("Seraphine", 5) },
                { ChampionsEnum.TWISTEDFATE, new ChampionDisplayInfo("Twisted Fate", 5) },
                { ChampionsEnum.VARUS, new ChampionDisplayInfo("Varus", 5) },
            };

        [System.Serializable]
        public class ChampionIconData
        {
            public ChampionsEnum champion;
            public Sprite icon;
        }

        void Start()
        {
            CreateChampionUIs();
        }



        private void CreateChampionUIs()
        {
            if (_championUIPrefab == null)
            {
                Debug.LogError("Champion UI Prefab is not assigned!");
                return;
            }

            foreach (var kvp in _championDisplayInfo)
            {
                var champion = kvp.Key;
                var displayInfo = kvp.Value;

                Transform parent = GetParentForCost(displayInfo.Cost);
                if (parent == null)
                    continue;

                GameObject championUI = Instantiate(_championUIPrefab, parent);
                ChampionBaseUI championBaseUI = championUI.GetComponent<ChampionBaseUI>();

                if (championBaseUI != null)
                {
                    championBaseUI.Initialize(champion, displayInfo.DisplayName, _defaultSelection);
                    _championUIs[champion] = championBaseUI;
                }
                else
                {
                    Debug.LogError($"ChampionBaseUI component not found on prefab for {champion}");
                }
            }
        }

        private Transform GetParentForCost(int cost)
        {
            return cost switch
            {
                1 => _cost1Parent,
                2 => _cost2Parent,
                3 => _cost3Parent,
                4 => _cost4Parent,
                5 => _cost5Parent,
                _ => null,
            };
        }

        public HashSet<Set.Data.ChampionsEnum> GetSelectedChampions()
        {
            var selectedChampions = new HashSet<Set.Data.ChampionsEnum>();

            foreach (var kvp in _championUIs)
            {
                if (kvp.Value.IsSelected)
                {
                    selectedChampions.Add(kvp.Key);
                }
            }

            return selectedChampions;
        }

        public void Reset()
        {
            ResetToDefault();
        }

        public void SetTo(bool on)
        {
            if (on)
                SelectAll();
            else
                SelectNone();
        }

        public void SetTo(Set.Data.ChampionsEnum championValue)
        {
            foreach (var kvp in _championUIs)
            {
                var champion = kvp.Key;
                var championUI = kvp.Value;

                bool shouldBeSelected = (championValue & champion) == champion;
                championUI.SetSelected(shouldBeSelected);
            }
        }

        public void ResetToDefault()
        {
            foreach (var championUI in _championUIs.Values)
            {
                championUI.ResetToDefault();
            }
        }

        public void SelectAll()
        {
            foreach (var championUI in _championUIs.Values)
            {
                championUI.SetSelected(true);
            }
        }

        public void SelectNone()
        {
            foreach (var championUI in _championUIs.Values)
            {
                championUI.SetSelected(false);
            }
        }

        public void SetInteractable(bool interactable)
        {
            foreach (var championUI in _championUIs.Values)
            {
                championUI.SetInteractable(interactable);
            }
        }
    }
}
