@page "/pokemonsleep/edit/{id:int}"
@inject IPokemonService PokemonService
@inject NavigationManager Navigation

@attribute [StreamRendering]
@rendermode InteractiveServer

<PageTitle>Edit Pokémon</PageTitle>
<PokemonHeader />

@if (pokemon == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <div class="w-50 mt-5 m-auto create-container">
        <div class="card-header bg-secondary bg-gradient ml-0 py-3">
            <div class="row">
                <div class="col-12 text-center">
                    <h2 class="text-info">Edit Pokémon</h2>
                </div>
            </div>
        </div>

        <div class="container m-lg-1">
            <EditForm class="col mb-3" Model="pokemon" OnValidSubmit="HandleSubmit">
                <DataAnnotationsValidator />

                <div class="row">
                    <div class="col-sm-3 input-group mb-3">
                        <span for="PokemonId" class="input-group-text">#</span>
                        <InputNumber min="0" id="PokemonId" @bind-Value="pokemon.PokemonId" class="form-control" required disabled />
                        <InputText id="PokemonName" @bind-Value="pokemon.PokemonName" class="form-control w-75" required disabled />
                    </div>
                </div>

                <div class="row mb-3 m-auto">
                    <label for="SleepType" class="form-label">Sleep Type</label>
                    <InputSelect id="SleepType" @bind-Value="pokemon.SleepType" class="form-select">
                        <option value="Dozing">Dozing</option>
                        <option value="Snoozing">Snoozing</option>
                        <option value="Slumbering">Slumbering</option>
                    </InputSelect>
                </div>

                <div class="row mb-3 m-auto">
                    <label for="Speciality" class="form-label">Specialty</label>
                    <InputSelect id="Speciality" @bind-Value="pokemon.Speciality" class="form-select">
                        <option value="Berries">Berries</option>
                        <option value="Ingredients">Ingredients</option>
                        <option value="Skills">Skills</option>
                    </InputSelect>
                </div>

                <!-- New Image URL Field -->
                <div class="row mb-3 m-auto">
                    <label for="ImageUrl" class="form-label">Base Image URL</label>
                    <InputText id="ImageUrl" @bind-Value="pokemon.PokemonImageUrl" class="form-control" />
                </div>
                <div class="row mb-3 m-auto">
                    <label for="ImageUrl" class="form-label">Shiny Image URL</label>
                    <InputText id="ImageUrl" @bind-Value="pokemon.PokemonShinyImageUrl" class="form-control" />
                </div>

                <button type="submit" class="btn btn-primary mb-3">Save Changes</button>
                <button type="button" class="btn btn-secondary mb-3" @onclick="Cancel">Cancel</button>
            </EditForm>
        </div>
    </div>
}

@code {
    [Parameter] public int Id { get; set; }
    private Pokemon? pokemon;

    protected override async Task OnInitializedAsync()
    {
        pokemon = await PokemonService.GetPokemonByIdAsync(Id);
    }

    private async Task HandleSubmit()
    {
        await PokemonService.UpdatePokemonAsync(pokemon);
        Navigation.NavigateTo("/pokemonsleep");
    }

    private void Cancel()
    {
        Navigation.NavigateTo("/pokemonsleep");
    }
}