@page "/admin/domains/shared"
@using Moonlight.App.Repositories.Domains
@using Moonlight.App.Services
@using CloudFlare.Client.Api.Zones
@using Moonlight.App.Database.Entities
@using Moonlight.App.Services.Interop

@inject SharedDomainRepository SharedDomainRepository
@inject SmartTranslateService SmartTranslateService
@inject DomainService DomainService
@inject AlertService AlertService
@inject ToastService ToastService

<OnlyAdmin>
    <LazyLoader @ref="LazyLoader" Load="Load">
        <div class="row mb-5">
            <div class="card card-body">
                <div class="mb-5">
                    <label class="form-label">
                        <TL>Domain</TL>
                    </label>
                    <select @bind="DomainId" class="form-select">
                        @if (Zones.Any())
                        {
                            foreach (var zone in Zones)
                            {
                                <option value="@(zone.Id)">@(zone.Name)</option>
                            }
                        }
                        else
                        {
                            <option value="">
                                <TL>No domains available</TL>
                            </option>
                        }
                    </select>
                </div>
                <WButton Text="@(SmartTranslateService.Translate("Add"))"
                         WorkingText="@(SmartTranslateService.Translate("Adding"))"
                         CssClasses="btn-success"
                         OnClick="Add">
                </WButton>
            </div>
        </div>
        @foreach (var sharedDomain in SharedDomains)
        {
            <div class="row mb-5">
            <div class="card card-body">
                <div class="d-flex flex-row justify-content-between align-items-center">
                    <div class="p-2">
                        <i class="text-primary bx bx-purchase-tag bx-md"></i>
                    </div>
                    <div class="p-2">
                        <span>@(sharedDomain.Name)</span>
                    </div>
                    <div class="p-2">
                        <span>@(sharedDomain.Id)</span>
                    </div>
                    <div class="p-2">
                        <WButton Text="@(SmartTranslateService.Translate("Delete"))"
                                 WorkingText="@(SmartTranslateService.Translate("Deleting"))"
                                 CssClasses="btn-danger"
                                 OnClick="() => Delete(sharedDomain)">
                        </WButton>
                    </div>
                </div>
            </div>
            </div>
        }
    </LazyLoader>
</OnlyAdmin>

@code
{
    private string DomainId = "";
    private Zone[] Zones;
    private SharedDomain[] SharedDomains;

    private LazyLoader LazyLoader;

    private async Task Load(LazyLoader lazyLoader)
    {
        Zones = await DomainService.GetAvailableDomains();

        SharedDomains = SharedDomainRepository.Get().ToArray();
    }

    private async Task Add()
    {
        if(string.IsNullOrEmpty(DomainId))
            return;

        var domain = Zones.First(x => x.Id == DomainId);

        var sd = new SharedDomain()
        {
            CloudflareId = domain.Id,
            Name = domain.Name
        };

        SharedDomainRepository.Add(sd);
        await ToastService.Success(SmartTranslateService.Translate("Shared domain successfully added"));

        await LazyLoader.Reload();
    }

    private async Task Delete(SharedDomain sharedDomain)
    {
        try
        {
            SharedDomainRepository.Delete(sharedDomain);
            await ToastService.Success(SmartTranslateService.Translate("Shared domain successfully deleted"));

            await LazyLoader.Reload();
        }
        catch (Exception e)
        {
            //TODO: Add check if any domains are left
            
            await AlertService.Error(
                SmartTranslateService.Translate("Error"),
                SmartTranslateService.Translate("Something went wrong. Are any domains associated with this shared domain still there?")
            );
        }
    }
}