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

namespace Set.Data
{
    public class TraitSelectorManager : MonoBehaviour
    {
        [SerializeField]
        private TMP_InputField _compositionSize;

        [SerializeField]
        private ChampionSelector _mandatorychampionSelector;

        [SerializeField]
        private ChampionSelector _acceptablechampionSelector;

        [SerializeField]
        private EmblemSelector _emblemSelector;

        [SerializeField]
        private int _traitThreshold = 7;

        [SerializeField]
        public int _compositionPerFrame = 100;

        public void ListAllActivableCompo()
        {
            var emblemList = _emblemSelector.GetEmblems();
            var mandatoryChampions = ChampionUtils.ToLong(
                _mandatorychampionSelector.GetSelectedChampions()
            );
            var acceptableChampions = ChampionUtils.ToLong(
                _acceptablechampionSelector.GetSelectedChampions()
            );
            int compositionSize =
                _compositionSize.text == "" ? 1 : int.Parse(_compositionSize.text);

            Coroutine coroutine = StartCoroutine(
                ComputeCompositionAsync(
                    mandatoryChampions,
                    acceptableChampions,
                    compositionSize,
                    emblemList
                )
            );
        }

        public IEnumerator<float> ComputeCompositionAsync(
            long mandatoryChampions,
            long acceptableChampions,
            int compositionSize,
            Dictionary<int, int> emblemList
        )
        {
            var compositions = TraitsMapping.GenerateCombinations(
                mandatoryChampions,
                acceptableChampions,
                compositionSize
            );
            Debug.Log($"{compositions.Count} Compositions generated.");
            yield return 0f;
            int compHandled = 0;
            int totalCompHandled = 0;
            int totalSucessfulCompFound = 0;
            foreach (var composition in compositions)
            {
                int activeSynergies = GetActiveSynergy(composition, emblemList);
                compHandled++;
                totalCompHandled++;
                if (TraitUtils.TraitCountFromInt(activeSynergies) >= _traitThreshold)
                {
                    totalSucessfulCompFound++;
                    HashSet<ChampionsEnum> champions = ChampionUtils.FromLong(composition);
                    var s = TraitsMapping.CompositionToString(champions);
                    Debug.Log(
                        $"{totalSucessfulCompFound}: {s} - {TraitUtils.TraitCountFromInt(activeSynergies)}"
                    );
                }
                if (compHandled >= _compositionPerFrame)
                {
                    Debug.LogWarning(
                        $"{totalSucessfulCompFound} : {totalCompHandled} Compositions handled."
                    );
                    compHandled = 0;
                    yield return 0f;
                }
            }

            Debug.Log("Total successful compositions found: " + totalSucessfulCompFound);
        }

        private int GetActiveSynergy(long combination, Dictionary<int, int> emblemList)
        {
            var synergies = TraitsMapping.TraitCountInCompo(combination);

            var synergiesWithEmblem = TraitsMapping.MergeEmblems(synergies, emblemList);
            var activeSynergies = TraitsMapping.FilterActiveTraits(synergiesWithEmblem);
            return activeSynergies;
        }

        public void StopAll()
        {
            StopAllCoroutines();
        }
    }
}
