@using BTCPayServer.Views.Stores
@using BTCPayServer.Abstractions.Models
@model UILNURLController.EditLightningAddressVM
@{
    ViewData.SetActivePage("LightningAddress", nameof(StoreNavPages), StringLocalizer["Lightning Address"], Context.GetStoreData().Id);
}

@section PageHeadContent {
    <style>
        .settings-holder span:not(:last-child):after{
            content: " / ";
        }
    </style>
}

@section PageFootContent {
    <script>
        delegate('click', '.remove', event => { 
            event.preventDefault() 
            const { name, value } = event.target
            const confirmButton = document.getElementById('ConfirmContinue')
            confirmButton.setAttribute('name', name)
            confirmButton.setAttribute('value', value)
        })
    </script>
}

<div class="sticky-header">
    <h2 text-translate="true">@ViewData["Title"]</h2>
	<a id="page-primary" data-bs-toggle="collapse" data-bs-target="#AddAddress" class="btn btn-primary" role="button">
        Add Address
    </a>
</div>

<partial name="_StatusMessage" />

@if (Context.Request.PathBase.ToString() != string.Empty)
{
    <div class="alert alert-warning" role="alert">
        Your BTCPay Server installation is using the root path <span class="fw-bold">@Context.Request.PathBase</span>.<br /><br />
        This is incompatible with wallets attempting to resolve <span class="fw-bold">@Context.Request.GetAbsoluteUriNoPathBase(new Uri("/.well-known/lnurlp/{username}", UriKind.Relative))</span> rather than <span class="fw-bold">@Context.Request.GetAbsoluteUri("/.well-known/lnurlp/{username}")</span>.<br /><br />
        If the LN Address doesn't work, ask your integrator to redirect queries from <span class="fw-bold">@Context.Request.GetAbsoluteUriNoPathBase(new Uri("/.well-known/lnurlp/{username}", UriKind.Relative))</span> to <span class="fw-bold">@Context.Request.GetAbsoluteUri("/.well-known/lnurlp/{username}")</span>.
    </div>
}

<form asp-action="EditLightningAddress" method="post">
    @{
        var showAddForm = !ViewContext.ViewData.ModelState.IsValid || !string.IsNullOrEmpty(Model.Add?.Username) || Model.Add?.Max != null || Model.Add?.Min != null || !string.IsNullOrEmpty(Model.Add?.CurrencyCode);
        var showAdvancedOptions = !string.IsNullOrEmpty(Model.Add?.CurrencyCode) || !string.IsNullOrEmpty(Model.Add?.InvoiceMetadata) || Model.Add?.Min != null || Model.Add?.Max != null;
     }
    
    <div class="collapse @(showAddForm ? "show": "")" id="AddAddress">
        <div class="form-group">
            <label asp-for="Add.Username" class="form-label"></label>
            <div class="input-group">
                <input asp-for="Add.Username" class="form-control"/>
                <span class="input-group-text">@@@Context.Request.Host.ToUriComponent()@Context.Request.PathBase</span>
            </div>
            <span asp-validation-for="Add.Username" class="text-danger"></span>
        </div>
        <button class="d-inline-flex align-items-center btn btn-link text-primary fw-semibold p-0 mb-3" type="button" id="AdvancedSettingsButton" data-bs-toggle="collapse" data-bs-target="#AdvancedSettings" aria-expanded="false" aria-controls="AdvancedSettings">
            <vc:icon symbol="caret-down"/>
            <span class="ms-1" text-translate="true">Advanced settings</span>
        </button>
        <div id="AdvancedSettings" class="collapse @(showAdvancedOptions ? "show" : "")">
            <div class="row">
                <div class="col-12 col-sm-auto">
                    <div class="form-group" title="@StringLocalizer["The currency to generate the invoice in when generated through this lightning address"]">
                        <label asp-for="Add.CurrencyCode" class="form-label"></label>
                        <input asp-for="Add.CurrencyCode" class="form-control w-auto" currency-selection style="max-width:16ch;"/>
                        <span asp-validation-for="Add.CurrencyCode" class="text-danger"></span>
                    </div>
                </div>
                <div class="col-12 col-sm-auto">
                    <div class="form-group" title="@StringLocalizer["Minimum amount of sats to allow to be sent to this ln address"]">
                        <label asp-for="Add.Min" class="form-label"></label>
                        <input asp-for="Add.Min" class="form-control" type="number" inputmode="numeric" min="1" style="max-width:16ch;"/>
                        <span asp-validation-for="Add.Min" class="text-danger"></span>
                    </div>
                </div>
                <div class="col-12 col-sm-auto">
                    <div class="form-group" title="@StringLocalizer["Maximum amount of sats to allow to be sent to this ln address"]">
                        <label asp-for="Add.Max" class="form-label"></label>
                        <input asp-for="Add.Max" class="form-control" type="number" inputmode="numeric" min="1" max="@int.MaxValue" style="max-width:16ch;"/>
                        <span asp-validation-for="Add.Max" class="text-danger"></span>
                    </div>
                </div>
                <div class="col-12 col-sm-auto">
                    <div class="form-group" title="Metadata (in JSON) to add to the invoice when created through this lightning address.">
                        <label asp-for="Add.InvoiceMetadata" class="form-label"></label>
						<a href="https://docs.btcpayserver.org/Development/InvoiceMetadata/" target="_blank" rel="noreferrer noopener">
							<vc:icon symbol="info" />
						</a>
                        <textarea asp-for="Add.InvoiceMetadata" class="form-control"></textarea>
                        <span asp-validation-for="Add.InvoiceMetadata" class="text-danger"></span>
                    </div>
                </div>
            </div>
        </div>
    
        <div class="form-group">
            <button type="submit" name="command" value="add" class="btn btn-primary">Save</button>
        </div>
    </div>
    
    @if (Model.Items.Any())
    {
        <table class="table table-hover">
            <thead>
            <tr>
                <th text-translate="true">Address</th>
                <th text-translate="true">Settings</th>
                <th class="text-end" text-translate="true">Actions</th>
            </tr>
            </thead>
            <tbody>
            @for (var index = 0; index < Model.Items.Count; index++)
            {
                <input asp-for="Items[index].CurrencyCode" type="hidden"/>
                <input asp-for="Items[index].Min" type="hidden"/>
                <input asp-for="Items[index].Max" type="hidden"/>
                <input asp-for="Items[index].Username" type="hidden"/>
                <input asp-for="Items[index].InvoiceMetadata" type="hidden"/>
                var address = $"{Model.Items[index].Username}@{Context.Request.Host.ToUriComponent()}";
                <tr>
                    <td>
                        <div class="input-group" data-clipboard="@address">
                            <input type="text" class="form-control copy-cursor lightning-address-value" readonly="readonly" value="@address"/>
                            <button type="button" class="btn btn-outline-secondary px-3">
                                <vc:icon symbol="actions-copy" />
                            </button>
                        </div>

                    </td>
                    <td class="settings-holder align-middle">
                        @if (Model.Items[index].Min.HasValue)
                        {
                            <span>@Model.Items[index].Min min sats</span>
                        }
                        @if (Model.Items[index].Max.HasValue)
                        {
                            <span>@Model.Items[index].Max max sats</span>
                        }
                        @if (!string.IsNullOrEmpty(Model.Items[index].CurrencyCode))
                        {
                            <span>tracked in @Model.Items[index].CurrencyCode</span>
                        }
                        @if (!string.IsNullOrEmpty(Model.Items[index].InvoiceMetadata))
                        {
                            <span>with invoice metadata @Model.Items[index].InvoiceMetadata</span>
                        }
                    </td>
                    <td class="text-end">
                        <button type="submit" title="Remove" name="command" value="@($"remove:{Model.Items[index].Username}")" class="btn btn-link px-0 remove" data-bs-toggle="modal" data-bs-target="#ConfirmModal" data-description="The Lightning Address <strong>@Html.Encode(address)</strong> will be removed." data-confirm-input="REMOVE" text-translate="true">
                            Remove
                        </button>
                    </td>
                </tr>
            }
            </tbody>
        </table>
    }
    else
    {
        <p class="text-secondary" text-translate="true">
            There are no Lightning Addresses yet.
        </p>
    }
</form>

<partial name="_Confirm" model="@(new ConfirmModel("Remove Lightning Address", "This Lightning Address will be removed.", "Remove"))" />