@using Moonlight.App.Services
@using Moonlight.App.Services.Interop

@inject RatingService RatingService
@inject ModalService ModalService
@inject SmartTranslateService SmartTranslateService
@inject ToastService ToastService

<div class="modal fade" tabindex="-1" style="display: none;" id="rating">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h3 class="modal-title">
                    <TL>Hey, can i borrow you for a second?</TL>
                </h3>
            </div>
            <div class="modal-body">
                <p class="fs-4">
                    <TL>We want to improve our services and get a little bit of feedback how we are currently doing. Please leave us a rating</TL>
                </p>
            </div>
            <div class="modal-body">
                <div class="d-flex justify-content-center rating">
                    <input class="rating-input" name="rating" value="0" checked="" type="radio">
                    <label class="rating-label" for="rating1">
                        <i class="bx bx-lg bx-star"></i>
                    </label>
                    <input class="rating-input" name="rating" value="1" type="radio" id="rating1" @onchange="SetRating">

                    <label class="rating-label" for="rating2">
                        <i class="bx bx-lg bx-star"></i>
                    </label>
                    <input class="rating-input" name="rating" value="2" type="radio" id="rating2" @onchange="SetRating">

                    <label class="rating-label" for="rating3">
                        <i class="bx bx-lg bx-star"></i>
                    </label>
                    <input class="rating-input" name="rating" value="3" type="radio" id="rating3" @onchange="SetRating">

                    <label class="rating-label" for="rating4">
                        <i class="bx bx-lg bx-star"></i>
                    </label>
                    <input class="rating-input" name="rating" value="4" type="radio" id="rating4" @onchange="SetRating">

                    <label class="rating-label" for="rating5">
                        <i class="bx bx-lg bx-star"></i>
                    </label>
                    <input class="rating-input" name="rating" value="5" type="radio" id="rating5" @onchange="SetRating">
                </div>
            </div>
            <div class="modal-footer">
                <WButton Text="@(SmartTranslateService.Translate("Rate"))"
                         WorkingText="@(SmartTranslateService.Translate("Saving"))"
                         CssClasses="btn-primary"
                         OnClick="Save">
                </WButton>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" tabindex="-1" style="display: none" id="ratingUrl">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h3 class="modal-title">
                    <TL>Thanks for your rating</TL>
                </h3>
            </div>
            <div class="modal-body">
                <p class="fs-4">
                    <TL>It would be really kind of you rating us on a external platform as it will help our project very much</TL>
                </p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><TL>Close</TL></button>
                <a href="@(Url)" class="btn btn-primary" target="_blank"><TL>Yes</TL></a>
            </div>
        </div>
    </div>
</div>

@code
{
    private int Rate = 0;
    private string Url = "";

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            Url = await RatingService.GetRateUrl();

            if (await RatingService.ShouldRate())
            {
                await Task.Run(async () =>
                {
                    await Task.Delay(1000);
                    await ModalService.Show("rating");
                });
            }
        }
    }

    private Task SetRating(ChangeEventArgs args)
    {
        if (args.Value == null)
            return Task.CompletedTask;

        if (int.TryParse(args.Value.ToString(), out int rate))
        {
            Rate = rate;
        }

        return Task.CompletedTask;
    }

    private async Task Save()
    {
        await ModalService.Hide("rating");

        if (await RatingService.Rate(Rate))
        {
            await ModalService.Show("ratingUrl");
        }
        else
        {
            await ToastService.Success(
                SmartTranslateService.Translate("Rating saved")
                );
        }
    }
}