@addTagHelper *, BundlerMinifier.TagHelpers
@inject BTCPayServer.Services.LanguageService langService
@using Newtonsoft.Json
@using Newtonsoft.Json.Linq
@model PaymentModel
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <META NAME="robots" CONTENT="noindex,nofollow">
    <title>@Model.HtmlTitle</title>

    <bundle name="wwwroot/bundles/checkout-bundle.min.css" />

    <script type="text/javascript">
        var srvModel = @Html.Raw(Json.Serialize(Model));
    </script>

    <bundle name="wwwroot/bundles/checkout-bundle.min.js" />
    <script>vex.defaultOptions.className = 'vex-theme-btcpay'</script>

    @if (Model.CustomCSSLink != null)
    {
        <link href="@Model.CustomCSSLink" rel="stylesheet" />
    }

    @if (Model.IsModal)
    {
        <style type="text/css">
            body {
                background: rgba(55, 58, 60, 0.4);
            }

            .close-icon {
                display: flex;
            }
        </style>
    }
</head>
<body>
    <noscript>
        <center style="padding: 2em">
            <h2>Javascript is currently disabled in your browser.</h2>
            <h5>Please enable Javascript and refresh this page for the best experience.</h5>

            <p>Alternatively, click below to continue to our HTML-only invoice.</p>

            <a href="/invoice-noscript?id=@Model.InvoiceId" style="text-decoration: underline; color: blue">
                Continue to javascript-disabled invoice &gt;
            </a>
        </center>
    </noscript>

    <!--[if lte IE 8]>
    <center style="padding: 2em">
      <form action="/invoice-noscript" method="GET">
        <button style="text-decoration: underline; color: blue">Continue to legacy browser compatible invoice page
        </button>
      </form>
    </center>
    <![endif]-->

    <invoice>
        <div class="no-bounce" id="checkoutCtrl" v-cloak>
            <div class="modal page">
                <div class="modal-dialog open opened enter-purchaser-email" role="document">
                    <div class="modal-content long">
                        <div class="content">
                            <div class="invoice">
                                <partial name="Checkout-Body" />
                            </div>
                        </div>
                    </div>

                    <div style="margin-top: 10px; text-align: center;">
                        @* Not working because of nsSeparator: false, keySeparator: false,
                            {{$t("nested.lang")}} >>
                        *@

                        <select asp-for="DefaultLang"
                                class="cmblang reverse invisible"
                                onchange="changeLanguage($(this).val())"
                                asp-items="@langService.GetLanguages().Select((language) => new SelectListItem(language.DisplayName,language.Code, false))"></select>

                        <script>
                            var languageSelectorPrettyDropdown;
                            $(function () {
                                // REVIEW: don't use initDropdown method but rather directly initialize select whenever you are using it
                                $("#DefaultLang").val(startingLanguage);
                                languageSelectorPrettyDropdown = initDropdown("#DefaultLang");
                            });

                            function initDropdown(selector) {
                                return $(selector).prettyDropdown({
                                    classic: false,
                                    height: 32,
                                    reverse: true,
                                    hoverIntent: 5000
                                });
                            }
                        </script>
                    </div>
                    <div style="margin-top: 10px; text-align: center;" class="form-text small text-muted">
                        <span>Powered by <a target="_blank" href="https://github.com/btcpayserver/btcpayserver">BTCPay Server</a></span>
                    </div>
                </div>
            </div>
        </div>
    </invoice>
    <script type="text/javascript">
    var availableLanguages = @Html.Raw(Json.Serialize(langService.GetLanguages().Select((language) => language.Code)));;
    var storeDefaultLang = "@Model.DefaultLang";
    var fallbackLanguage = "en";
    startingLanguage = computeStartingLanguage();
      // initialization
    i18next
        .use(window.i18nextXHRBackend)
        .init({
            backend: {
                loadPath: '@(Model.RootPath)locales/{{lng}}.json'
            },
            lng: startingLanguage,
            fallbackLng: fallbackLanguage,
            nsSeparator: false,
            keySeparator: false
        });

    function computeStartingLanguage() {
        if (urlParams.lang && isLanguageAvailable(urlParams.lang)) {
            return urlParams.lang;
        }
        else if (isLanguageAvailable(storeDefaultLang)) {
            return storeDefaultLang;
        } else {
            return fallbackLanguage;
        }
    }

    function changeLanguage(lang) {
        if (isLanguageAvailable(lang)) {
            i18next.changeLanguage(lang);
        }
    }

    function isLanguageAvailable(languageCode) {
        return availableLanguages.indexOf(languageCode) >= 0;
    }

    const i18n = new VueI18next(i18next);

    // TODO: Move all logic from core.js to Vue controller
    Vue.config.ignoredElements = [
        'line-items',
        'low-fee-timeline',
        // Ignoring custom HTML5 elements, eg: bp-spinner
        /^bp-/
    ];

    var checkoutCtrl = new Vue({
        i18n: i18n,
        el: '#checkoutCtrl',
        components: {
            qrcode: VueQrcode,
            changelly: ChangellyComponent,
            coinswitch: CoinSwitchComponent
        },
        data: {
            srvModel: srvModel,
            lndModel: null,
            scanDisplayQr: "",
            expiringSoon: false,
            isModal: srvModel.isModal,
            lightningAmountInSatoshi: srvModel.lightningAmountInSatoshi,
            selectedThirdPartyProcessor: ""
        },
        computed: {
            coinswitchAmountDue: function() {
                return this.srvModel.coinSwitchAmountMarkupPercentage
                    ? (1 + (this.srvModel.coinSwitchAmountMarkupPercentage / 100))
                    : this.srvModel.btcDue;
            }
        }
    });
    </script>
</body>
</html>
