using Microsoft.AspNetCore.Components.Web;
using Microsoft.JSInterop;
using Portfolio.Domain.Features.Pokemon;

namespace Portfolio.WebUI.Server.Components.Pages.Pokemon_Pages
{
    public partial class PokemonCreate
    {

        private bool pokemon2FormView { get; set; } = false;
        private bool pokemon3FormView { get; set; } = false;

        private bool HideLabel { get; set; } = true;
        private void Toggle()
        {
            HideLabel = !HideLabel;
        }
        private void TogglePokemon2FormView()
        {
            pokemon2FormView = !pokemon2FormView;
        }
        private void TogglePokemon3FormView()
        {
            pokemon3FormView = !pokemon3FormView;
            TogglePokemon2FormView();
            
        }

        private Pokemon pokemon1, pokemon2, pokemon3 = new Pokemon
        {
            PokemonId = 0, // Or any default ID logic
            PokemonName = string.Empty, // Required fields cannot be null
            SleepType = string.Empty,
            Speciality = string.Empty,
            IsVariation = false
        };
        private bool isSubmitting = false;

        private bool ToggleVariationName { get; set; }

        private void onDisable()
        {
            this.ToggleVariationName = true;
        }
        private bool IsComplete(Pokemon NewPokemon) =>
            NewPokemon.PokemonId > 0 &&
            !string.IsNullOrWhiteSpace(NewPokemon.PokemonName) &&
            !string.IsNullOrWhiteSpace(NewPokemon.PokemonType) &&
            !string.IsNullOrWhiteSpace(NewPokemon.SleepType) &&
            !string.IsNullOrWhiteSpace(NewPokemon.Speciality) &&
            (!NewPokemon.IsVariation || !string.IsNullOrWhiteSpace(NewPokemon.VariationName));

        private async Task HandleAdd()
        {
            /*
             Okay there are three versions of submits which need to be checked
            1. Single submit, only one, if both pokemon#FormView is false
            2. 2 Submit, if pokemon2FormView is true
            3. 3 Submit, if pokemon3FormView is true
             */
            if (!pokemon2FormView && !pokemon3FormView) 
            {
                //if(IsComplete(pokemon1))
                //{
                    await HandleSubmit(pokemon1);
                    Navigation.NavigateTo("/pokemon");
                //}
            }
            else if (pokemon2FormView)
            {
                //if (IsComplete(pokemon1) && IsComplete(pokemon2))
                //{
                    await HandleSubmit(pokemon1);
                    await HandleSubmit(pokemon2);
                    Navigation.NavigateTo("/pokemon");

               // }
            }
            else if (pokemon3FormView)
            {
                //if (IsComplete(pokemon1) && IsComplete(pokemon2) && IsComplete(pokemon3))
                //{
                    await HandleSubmit(pokemon1);
                    await HandleSubmit(pokemon2);
                    await HandleSubmit(pokemon3);
                    Navigation.NavigateTo("/pokemon");

                //}
            }

        }

        private async Task HandleSubmit(Pokemon NewPokemon)
        {
            isSubmitting = true;
            
            await PokemonService.AddPokemonAsync(NewPokemon);
            isSubmitting = false;
            
        }

        protected void Cancel(MouseEventArgs e)
        {
            Console.WriteLine("Testing in Cancel");
            Navigation.NavigateTo("/pokemon");
        }

        private async Task ReceivePokemon1(Pokemon p)
        {
            // Save received pokemon as Pokemon 1
            pokemon1 = p;

            // Server console (Blazor Server)
            Console.WriteLine($"[Pokemon 1] #{pokemon1.PokemonId} {pokemon1.PokemonName} | {pokemon1.PokemonType} | {pokemon1.SleepType} | {pokemon1.Speciality} | Var:{pokemon1.IsVariation} {pokemon1.VariationName}");

            // Browser console
            await JS.InvokeVoidAsync("console.log", "Pokemon 1:", pokemon1);
        }

        private async Task ReceivePokemon2(Pokemon p)
        {
            // Save received pokemon as Pokemon 1
            pokemon2 = p;

            // Server console (Blazor Server)
            Console.WriteLine($"[Pokemon 2] #{pokemon2.PokemonId} {pokemon2.PokemonName} | {pokemon2.PokemonType} | {pokemon2.SleepType} | {pokemon2.Speciality} | Var:{pokemon2.IsVariation} {pokemon2.VariationName}");

            // Browser console
            await JS.InvokeVoidAsync("console.log", "Pokemon 2:", pokemon2);
        }

        private async Task ReceivePokemon3(Pokemon p)
        {
            // Save received pokemon as Pokemon 1
            pokemon3 = p;

            // Server console (Blazor Server)
            Console.WriteLine($"[Pokemon 3] #{pokemon3.PokemonId} {pokemon3.PokemonName} | {pokemon3.PokemonType} | {pokemon3.SleepType} | {pokemon3.Speciality} | Var:{pokemon3.IsVariation} {pokemon3.VariationName}");

            // Browser console
            await JS.InvokeVoidAsync("console.log", "Pokemon 3:", pokemon3);
        }

    }
}
