using Microsoft.AspNetCore.Components;
using Portfolio.Domain.Features.Pokemon;
using Portfolio.Domain.Features.Pokemon_Natures;
using Portfolio.Domain.Features.Pokemon_Subskills;

namespace Portfolio.WebUI.Server.Components.Pages.Pokemon_Pages
{
    public partial class PokemonRate
    {
        private List<Pokemon> PokemonList;
        private List<PokemonNature> NatureList;
        private List<PokemonSubskill> SubskillList;

        private Dictionary<int, bool> isShiny = new Dictionary<int, bool>();

        private int _selectedPokemonId;
        private int SelectedPokemonId
        {
            get => _selectedPokemonId;
            set
            {
                _selectedPokemonId = value;
                OnPokemonSelected();
            }
        }

        private int SelectedNatureId;
        private int subskillSelect1;
        private int subskillSelect2;
        private int subskillSelect3;
        private int subskillSelect4;
        private int subskillSelect5;

        private int[] SelectedSubskills = new int[3];

        private Pokemon SelectedPokemon;

        private int FinalScore;
        private string ScoreBackgroundColor;
        private string ScoreColor;


        private string PokemonSearchTerm = string.Empty;

        // Filter Pokémon based on the search input term
        private List<Pokemon> FilteredPokemonList => string.IsNullOrWhiteSpace(PokemonSearchTerm)
            ? PokemonList
            : PokemonList.Where(p => p.PokemonName.Contains(PokemonSearchTerm, StringComparison.OrdinalIgnoreCase)).ToList();

        private async Task OnSearchTextChanged(ChangeEventArgs e)
        {
            // Trigger filtering as the user types
            PokemonSearchTerm = e.Value.ToString();
            
        }

        private async Task SelectPokemon(Pokemon pokemon)
        {
            SelectedPokemon = pokemon;
            PokemonSearchTerm = string.Empty;  // Reset search term
            StateHasChanged();
            //FilteredPokemonList.Clear();  // Clear the filtered list to hide dropdown
        }

        private async void OnPokemonSelected()
        {
            if (SelectedPokemonId > 0)
            {
                SelectedPokemon = await PokemonService.GetPokemonByIdAsync(SelectedPokemonId);
                StateHasChanged(); // Force UI to refresh
            }
        }

        private void CalculateScore()
        {
            if (SelectedPokemon == null || SelectedNatureId == 0 || subskillSelect1 == 0 || subskillSelect2 == 0 || subskillSelect3 == 0)
            {
                return;
            }

            var nature = NatureList.FirstOrDefault(n => n.Id == SelectedNatureId);
            var subskill1 = SubskillList.FirstOrDefault(s => s.Id == subskillSelect1);
            var subskill2 = SubskillList.FirstOrDefault(s => s.Id == subskillSelect2);
            var subskill3 = SubskillList.FirstOrDefault(s => s.Id == subskillSelect3);

            if (nature == null || subskill1 == null || subskill2 == null || subskill3 == null)
            {
                return;
            }

            FinalScore = SelectedPokemon.Speciality switch
            {
                "Berries" => nature.BerryRating + subskill1.BerryRank + subskill2.BerryRank + subskill3.BerryRank,
                "Ingredients" => nature.IngredientRating + subskill1.IngredientRank + subskill2.IngredientRank + subskill3.IngredientRank,
                "Skills" => nature.SkillRating + subskill1.SkillRank + subskill2.SkillRank + subskill3.SkillRank,
                _ => 0
            };

            ScoreBackgroundColor = FinalScore switch
            {
                8 => "#16C47F",
                7 => "#33CB8F",
                6 => "#50D39F",
                5 => "#6DDAAF",
                4 => "#8BE2BF",
                3 => "#A8E9CF",
                2 => "#C5F0DF",
                1 => "#E2F8EF",
                0 => "#FFFFFF",
                -1 => "#FFE7E7",
                -2 => "#FED0D0",
                -3 => "#FEB8B8",
                -4 => "#FDA0A0",
                -5 => "#FD8888",
                -6 => "#FC7171",
                -7 => "#FC5959",
                -8 => "#FB4141",
                _ => "#FFFFFF"

            };
            ScoreColor = FinalScore switch
            {
                8 => "#FFFFFF",
                7 => "#FFFFFF",
                6 => "#FFFFFF",
                5 => "#FFFFFF",
                4 => "#FFFFFF",
                3 => "#FFFFFF",
                2 => "#FFFFFF",
                1 => "#FFFFFF",
                0 => "#000",
                -1 => "#FFFFFF",
                -2 => "#FFFFFF",
                -3 => "#FFFFFF",
                -4 => "#FFFFFF",
                -5 => "#FFFFFF",
                -6 => "#FFFFFF",
                -7 => "#FFFFFF",
                -8 => "#FFFFFF",
                _ => "#FFFFFF"

            };
        }

        protected override async Task OnInitializedAsync()
        {
            PokemonList = await PokemonService.GetAllPokemonAsync();
            NatureList = await PokemonNatureService.GetAllPokemonNaturesAsync();
            SubskillList = await PokemonSubskillService.GetAllPokemonSubskillsAsync();

            if (PokemonList is not null)
            {

                PokemonList.Sort((x, y) => x.PokemonId.CompareTo(y.PokemonId));

                // Initialize dictionary with false (show base image first)
                foreach (var pokemon in PokemonList)
                {
                    isShiny[pokemon.Id] = false;
                }

            }

        }

        private void ToggleImage(int Id)
        {
            if (isShiny.ContainsKey(Id))
            {
                isShiny[Id] = !isShiny[Id];
            }
        }
    }
}
