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.Component.Pokemon_Components
{
    partial class PokemonRatingPanel
    {
        [Parameter] public Pokemon PokemonToRate { get; set; }
        [Parameter] public List<PokemonNature> NatureList { get; set; } = new();
        [Parameter] public List<PokemonSubskill> SubskillList { get; set; } = new();

        private int SelectedNatureId;
        private int Subskill1;
        private int Subskill2;
        private int Subskill3;
        private int Subskill4;
        private int Subskill5;

        private int FinalScore;
        private string ScoreBackgroundColor = "#FFFFFF";
        private string ScoreColor = "#000000";


        private int lastNatureId;
        private int lastS1, lastS2, lastS3;
        private int lastnaturescore, lastS1score, lastS2score, lastS3score;

        protected override void OnAfterRender(bool firstRender)
        {
            // If last Nature or Subskill is different than previous
            if (PokemonToRate != null) {
                if (SelectedNatureId != lastNatureId)
                {
                    lastNatureId = SelectedNatureId;
                    var nature = NatureList.FirstOrDefault(n => n.Id == lastNatureId);
                    lastnaturescore = PokemonToRate.Speciality switch
                    {
                        "Berries" => nature.BerryRating,
                        "Ingredients" => nature.IngredientRating,
                        "Skills" => nature.SkillRating,
                        _ => 0
                    };

                }
                if (Subskill1 != lastS1)
                {

                    lastS1 = Subskill1;
                    var s1 = SubskillList.FirstOrDefault(s => s.Id == Subskill1);
                    lastS1score = PokemonToRate.Speciality switch
                    {
                        "Berries" => s1.BerryRank,
                        "Ingredients" => s1.IngredientRank,
                        "Skills" => s1.SkillRank,
                        _ => 0
                    };

                }
                if (Subskill2 != lastS2)
                {

                    lastS2 = Subskill2;
                    var s2 = SubskillList.FirstOrDefault(s => s.Id == Subskill2);
                    lastS2score = PokemonToRate.Speciality switch
                    {
                        "Berries" => s2.BerryRank,
                        "Ingredients" => s2.IngredientRank,
                        "Skills" => s2.SkillRank,
                        _ => 0
                    };



                }
                if (Subskill3 != lastS3)
                {
                    lastS3 = Subskill3;
                    var s3 = SubskillList.FirstOrDefault(s => s.Id == Subskill3);
                    lastS3score = PokemonToRate.Speciality switch
                    {
                        "Berries" => s3.BerryRank,
                        "Ingredients" => s3.IngredientRank,
                        "Skills" => s3.SkillRank,
                        _ => 0
                    };
                }
            }

            CalculateScore();
            StateHasChanged();

        }

        private void CalculateScore()
        {
            if (PokemonToRate == null || SelectedNatureId == 0 || lastS1 == 0 || lastS2 == 0 || lastS3 == 0)
            {
                
                FinalScore = 0;
                ScoreBackgroundColor = "#FFFFFF";
                ScoreColor = "#000000";
                return;
            }

            var nature = NatureList.FirstOrDefault(n => n.Id == SelectedNatureId);
            var s1 = SubskillList.FirstOrDefault(s => s.Id == Subskill1);
            var s2 = SubskillList.FirstOrDefault(s => s.Id == Subskill2);
            var s3 = SubskillList.FirstOrDefault(s => s.Id == Subskill3);

            if (nature == null || s1 == null || s2 == null || s3 == null)
            {
                FinalScore = 0;
                ScoreBackgroundColor = "#FFFFFF";
                ScoreColor = "#000000";
                return;
            }


            FinalScore = PokemonToRate.Speciality switch
            {
                "Berries" => nature.BerryRating + s1.BerryRank + s2.BerryRank + s3.BerryRank,
                "Ingredients" => nature.IngredientRating + s1.IngredientRank + s2.IngredientRank + s3.IngredientRank,
                "Skills" => nature.SkillRating + s1.SkillRank + s2.SkillRank + s3.SkillRank,
                _ => 0
            };


            // Set score background based on value
            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 == 0 ? "#000000" : "#FFFFFF";
        }
    }
}
