using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; using Microsoft.EntityFrameworkCore.Metadata.Internal; using Portfolio.Application.Services.PokemonService; using Portfolio.Domain.Features.Pokemon; namespace Portfolio.WebUI.Server.Components.Component.Pokemon_Components { public partial class PokemonForm { // To Add or Edit Pokemon [Parameter] public string formUse { get; set; } [Parameter] public EventCallback OnPokemonReady { get; set; } [Parameter] public EventCallback RemoveForm { get; set; } // When Adding [Parameter] public bool mostRecentForm { get; set; } private Pokemon NewPokemon = 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 }; // When Editing [Parameter] public Pokemon? PokemonToEdit { get; set; } private int PokemonToEditId { get; set; } // General Form protected static readonly string[] PokemonTypes = new[] { "Grass","Fire","Water","Normal","Flying","Bug","Poison","Electric","Ground","Rock","Ice", "Steel","Fighting","Psychic","Dark","Fairy","Ghost","Dragon" }; protected static readonly string[] SleepTypes = new[] { "Dozing", "Snoozing", "Slumbering" }; protected static readonly string[] Specialities = new[] { "Berries", "Ingredients", "Skills", "All" }; private bool HideLabel { get; set; } private bool showErrors { get; set; } = false; protected override async Task OnInitializedAsync() { if (formUse == "EDIT") { if (PokemonToEdit.IsVariation == true) { HideLabel = false; } PokemonToEditId = PokemonToEdit.Id; } else { HideLabel = true; } } private void Toggle() { HideLabel = !HideLabel; } // CSS-function to get proper styling for form elements private string GetRoundingClass() { if (!HideLabel) { return "rounded-start"; } return "rounded-start rounded-end"; } // Minimal "complete" check (no data annotations needed) private bool IsComplete => 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 IEnumerable MissingFields() { if (NewPokemon.PokemonId <= 0) yield return "Pokédex #"; if (string.IsNullOrWhiteSpace(NewPokemon.PokemonName)) yield return "Name"; if (string.IsNullOrWhiteSpace(NewPokemon.PokemonType)) yield return "Type"; if (string.IsNullOrWhiteSpace(NewPokemon.SleepType)) yield return "Sleep Type"; if (string.IsNullOrWhiteSpace(NewPokemon.Speciality)) yield return "Specialty"; if (NewPokemon.IsVariation && string.IsNullOrWhiteSpace(NewPokemon.VariationName)) yield return "Variation Name"; } private async Task HandleRemove() { await RemoveForm.InvokeAsync(); } private async Task SendPokemon() { if(formUse == "ADD") { if (!IsComplete) { showErrors = true; StateHasChanged(); return; } // Optionally send a copy to avoid later mutation by the child var copy = new Pokemon { PokemonId = NewPokemon.PokemonId, PokemonName = NewPokemon.PokemonName, PokemonType = NewPokemon.PokemonType, SleepType = NewPokemon.SleepType, Speciality = NewPokemon.Speciality, IsVariation = NewPokemon.IsVariation, VariationName = NewPokemon.VariationName, PokemonImageUrl = NewPokemon.PokemonImageUrl, PokemonShinyImageUrl = NewPokemon.PokemonShinyImageUrl, FlavorText = NewPokemon.FlavorText }; await OnPokemonReady.InvokeAsync(copy); } else { // Optionally send a copy to avoid later mutation by the child var edit = new Pokemon { Id = PokemonToEditId, PokemonId = PokemonToEdit.PokemonId, PokemonName = PokemonToEdit.PokemonName, PokemonType = PokemonToEdit.PokemonType, SleepType = PokemonToEdit.SleepType, Speciality = PokemonToEdit.Speciality, IsVariation = PokemonToEdit.IsVariation, VariationName = PokemonToEdit.VariationName, PokemonImageUrl = PokemonToEdit.PokemonImageUrl, PokemonShinyImageUrl = PokemonToEdit.PokemonShinyImageUrl, FlavorText = PokemonToEdit.FlavorText }; await OnPokemonReady.InvokeAsync(edit); Console.WriteLine(edit); } } } }