@using Moonlight.Features.Servers.Entities
@using Moonlight.Features.Servers.UI.Components.VariableViews
@using MoonCore.Abstractions
@using Microsoft.EntityFrameworkCore
@using MoonCoreUI.Services
@using Moonlight.Features.Servers.Entities.Enums

@inject Repository<Server> ServerRepository
@inject Repository<ServerImage> ImageRepository
@inject Repository<ServerVariable> ServerVariableRepository
@inject ToastService ToastService

<LazyLoader @ref="LazyLoader" Load="Load" ShowAsCard="true">
    <div class="row mt-1 g-5">
        <div class="col-md-6 col-12">
            <div class="card card-body p-5 h-100">
                <label class="form-label fs-5">Docker image</label>
                @if (Image.AllowDockerImageChange)
                {
                    <SmartSelect @bind-Value="SelectedDockerImage"
                                 Items="Image.DockerImages"
                                 DisplayField="@(x => x.DisplayName)"
                                 OnChange="OnDockerImageChanged"/>
                }
                else
                {
                    <select class="form-select disabled" disabled="disabled">
                        <option>@SelectedDockerImage.DisplayName</option>
                    </select>
                }
            </div>
        </div>

        @foreach (var variable in Server.Variables)
        {
            var imageVariable = Image.Variables.FirstOrDefault(x => x.Key == variable.Key);

            if (imageVariable != null && imageVariable.AllowView)
            {
                <div class="d-flex flex-column col-md-3 col-12">
                    <div class="card card-body p-5">
                        <label class="form-label fs-5">@(imageVariable.DisplayName)</label>
                        <div class="form-text text-gray-700 fs-5 mb-2 mt-0">
                            @imageVariable.Description
                        </div>
                        <div class="mt-auto">
                            @switch (imageVariable.Type)
                            {
                                case ServerImageVariableType.Number:
                                    <NumberVariableView Variable="variable" ImageVariable="imageVariable" OnChanged="Refresh"/>
                                    break;
                                case ServerImageVariableType.Toggle:
                                    <ToggleVariableView Variable="variable" ImageVariable="imageVariable" OnChanged="Refresh"/>
                                    break;
                                case ServerImageVariableType.Select:
                                    <SelectVariableView Variable="variable" ImageVariable="imageVariable" OnChanged="Refresh"/>
                                    break;
                                default:
                                    <TextVariableView Variable="variable" ImageVariable="imageVariable" OnChanged="Refresh"/>
                                    break;
                            }
                        </div>
                    </div>
                </div>
            }
        }
    </div>
</LazyLoader>

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

    private ServerImage Image;
    private LazyLoader LazyLoader;

    private ServerDockerImage SelectedDockerImage;

    private async Task Load(LazyLoader lazyLoader)
    {
        await lazyLoader.SetText("Loading server image data");

        Image = ImageRepository
            .Get()
            .Include(x => x.Variables)
            .Include(x => x.DockerImages)
            .First(x => x.Id == Server.Image.Id);

        if (Server.DockerImageIndex >= Image.DockerImages.Count || Server.DockerImageIndex == -1)
            SelectedDockerImage = Image.DockerImages.Last();
        else
            SelectedDockerImage = Image.DockerImages[Server.DockerImageIndex];
    }

    private async Task OnDockerImageChanged()
    {
        Server.DockerImageIndex = Image.DockerImages.IndexOf(SelectedDockerImage);
        ServerRepository.Update(Server);

        await ToastService.Success("Successfully changed docker image");
        await LazyLoader.Reload();
    }

    private async Task Refresh() => await LazyLoader.Reload();
}