@page "/profile/subscriptions"

@using Moonlight.Shared.Components.Navigations
@using Moonlight.App.Services
@using Moonlight.App.Database.Entities
@using Moonlight.App.Helpers
@using Moonlight.App.Services.Interop

@inject ConfigService ConfigService
@inject AlertService AlertService
@inject SubscriptionService SubscriptionService
@inject SmartTranslateService SmartTranslateService

<ProfileNavigation Index="3"/>

<div class="card mb-3">
    <div class="row g-0">
        <div class="col-md-4 p-10">
            <img src="/assets/media/svg/subscription.svg" class="img-fluid rounded-start" alt="Subscription">
        </div>
        <div class="col-md-8">
            <div class="card-body">
                <LazyLoader @ref="LazyLoader" Load="Load">
                    @if (Subscription == null)
                    {
                        var config = ConfigService
                            .GetSection("Moonlight")
                            .GetSection("Subscriptions")
                            .GetSection("Sellpass");

                        var enableSellpass = config.GetValue<bool>("Enable");
                        var url = config.GetValue<string>("Url");
                        
                        <h3 class="mb-2">
                            <div class="input-group mb-3">
                              <input @bind="Code" type="text" class="form-control" placeholder="@(SmartTranslateService.Translate("Enter code"))">
                              <WButton Text="@(SmartTranslateService.Translate("Submit"))"
                                       WorkingText="@(SmartTranslateService.Translate("Working"))"
                                       CssClasses="btn btn-primary"
                                       OnClick="OnSubmit">
                              </WButton>
                            </div>
                        </h3>

                        if (enableSellpass)
                        {
                            <div class="d-flex justify-content-end pb-0 px-0">
                                <a href="@(url)" class="btn btn-light">Buy subscription</a>
                            </div>
                        }
                    }
                    else
                    {
                        var d = User.SubscriptionSince.AddDays(User.SubscriptionDuration).ToUniversalTime();

                        <h3 class="mb-2">
                            <TL>Active until</TL> @(Formatter.FormatDateOnly(d))
                        </h3>
                        <p class="fs-5 text-gray-600 fw-semibold">
                            <TL>Current subscription</TL>: @(Subscription.Name)
                        </p>
                        <p class="fs-6 text-gray-600 fw-semibold">
                            @(Subscription.Description)
                        </p>
                        <p class="fs-7 text-gray-600 fw-semibold">
                            <TL>We will send you a notification upon subscription expiration</TL>
                        </p>
                        <div class="d-flex justify-content-end pb-0 px-0">
                            <WButton Text="@(SmartTranslateService.Translate("Cancel"))"
                                     WorkingText="@(SmartTranslateService.Translate("Working"))"
                                     CssClasses="btn btn-light"
                                     OnClick="Cancel">
                            </WButton>
                        </div>
                    }
                </LazyLoader>
            </div>
        </div>
    </div>
</div>

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

    private Subscription? Subscription;
    private LazyLoader LazyLoader;

    private string Code = "";

    private async Task Load(LazyLoader arg)
    {
        Subscription = await SubscriptionService.GetCurrent();
    }

    private async Task Cancel()
    {
        if (await AlertService.ConfirmMath())
        {
            await SubscriptionService.Cancel();
            await LazyLoader.Reload();
        }
    }

    private async Task OnSubmit()
    {
        await SubscriptionService.ApplyCode(Code);
        Code = "";
        await LazyLoader.Reload();
    }
}