@using Moonlight.App.Repositories
@using Moonlight.App.Database.Entities
@using Moonlight.App.Models.Forms
@using Moonlight.App.Services
@using Moonlight.App.Services.Interop
@using Mappy.Net

@inject Repository<User> UserRepository
@inject Repository<Server> ServerRepository
@inject ToastService ToastService
@inject SmartTranslateService SmartTranslateService

<LazyLoader Load="Load">
    <SmartForm Model="Model" OnValidSubmit="OnValidSubmit">
        <div class="card">
            <div class="card-body p-10">
                <label class="form-label">
                    <TL>Identifier</TL>
                </label>
                <div class="input-group mb-5">
                    <span class="input-group-text">
                        <i class="bx bx-id-card"></i>
                    </span>
                    <input type="number" class="form-control disabled" disabled="" value="@(Server.Id)">
                </div>
                <label class="form-label">
                    <TL>UuidIdentifier</TL>
                </label>
                <div class="input-group mb-5">
                    <span class="input-group-text">
                        <i class="bx bx-id-card"></i>
                    </span>
                    <input type="text" class="form-control disabled" disabled="" value="@(Server.Uuid)">
                </div>
                <label class="form-label">
                    <TL>Server name</TL>
                </label>
                <div class="input-group mb-5">
                    <span class="input-group-text">
                        <i class="bx bx-purchase-tag-alt"></i>
                    </span>
                    <InputText @bind-Value="Model.Name" type="text" class="form-control" placeholder="@(SmartTranslateService.Translate("Server name"))"></InputText>
                </div>
                <label class="form-label">
                    <TL>Owner</TL>
                </label>
                <div class="input-group mb-5">
                    <SmartDropdown T="User"
                                   @bind-Value="Model.Owner"
                                   Items="Users"
                                   DisplayFunc="@(x => x.Email)"
                                   SearchProp="@(x => x.Email)">
                    </SmartDropdown>
                </div>
            </div>
            <div class="card-footer">
                <div class="text-end">
                    <button type="submit" class="btn btn-success"><TL>Save changes</TL></button>
                </div>
            </div>
        </div>
    </SmartForm>
</LazyLoader>

@code
{
    [CascadingParameter]
    public Server Server { get; set; }

    private ServerOverviewDataModel Model;
    private User[] Users;

    private Task Load(LazyLoader arg)
    {
        Users = UserRepository.Get().ToArray();

        Model = Mapper.Map<ServerOverviewDataModel>(Server);

        return Task.CompletedTask;
    }

    private async Task OnValidSubmit()
    {
        Server = Mapper.Map(Server, Model);
        ServerRepository.Update(Server);

        await ToastService.Success(
            SmartTranslateService.Translate("Successfully saved changes")
            );
    }
}