@using Breez.Sdk
@using BTCPayServer
@using BTCPayServer.Client
@using BTCPayServer.Components.QRCode
@using BTCPayServer.Components.TruncateCenter
@using BTCPayServer.Models.StoreViewModels
@using BTCPayServer.Payments
@using BTCPayServer.Plugins.Breez
@using BTCPayServer.Security
@using BTCPayServer.Services
@using BTCPayServer.Services.Invoices
@using Microsoft.AspNetCore.Mvc.TagHelpers
@using NBitcoin
@inject BreezService BreezService
@inject TransactionLinkProviders TransactionLinkProviders
@inject PaymentMethodHandlerDictionary PaymentMethodHandlerDictionary
@{
    ViewData.SetActivePage("Breez", "Swap In", "SwapIn");
    var pmi = PaymentTypes.CHAIN.GetPaymentMethodId("BTC");
    string storeId = Model switch
    {
        string s => s,
        StoreDashboardViewModel dashboardModel => dashboardModel.StoreId,
        _ => Context.GetImplicitStoreId()
    };
    var sdk = BreezService.GetClient(storeId)?.Sdk;
    if (sdk is null)
        return;

    SwapInfo inProgressSwap = null;
    try
    {
        inProgressSwap = sdk.InProgressSwap();
        inProgressSwap ??= sdk.ReceiveOnchain(new ReceiveOnchainRequest());
    }
    catch (Exception e)
    {
    }

    var refundables = sdk.ListRefundables();
    var deriv = Context.GetStoreData().GetDerivationSchemeSettings(PaymentMethodHandlerDictionary, "BTC");
    var ni = sdk.NodeInfo();
    var f = sdk.RecommendedFees();
}

<datalist id="fees">
    <option value="@f.fastestFee">Fastest fee (@f.fastestFee sat/vB)</option>
    <option value="@f.halfHourFee">Half hour fee (@f.halfHourFee sat/vB)</option>
    <option value="@f.hourFee">Hour fee (@f.hourFee sat/vB)</option>
    <option value="@f.economyFee">Economic fee (@f.economyFee sat/vB)</option>
    <option value="@f.minimumFee">Minimum fee (@f.minimumFee sat/vB)</option>
</datalist>

<div class="row mb-4 mt-4">
    <div class="col-12">

        @if (inProgressSwap is not null)
        {
            <div class="payment-box">
                <div class="qr-container" data-clipboard="@inProgressSwap.bitcoinAddress">
                    <vc:qr-code data="@inProgressSwap.bitcoinAddress"/>
                </div>
                <div class="input-group mt-3">
                    <div class="form-floating">
                        <vc:truncate-center text="@inProgressSwap.bitcoinAddress" padding="15" elastic="true" classes="form-control-plaintext" id="Address"/>
                        <label for="Address">Address</label>
                    </div>
                    <div class="w-100">
                        <span class="text-muted">Please send an amount between <br/> @Money.Satoshis(inProgressSwap.minAllowedDeposit).ToDecimal(MoneyUnit.BTC) and @Money.Satoshis(inProgressSwap.maxAllowedDeposit).ToDecimal(MoneyUnit.BTC)BTC </span>
                        @if (deriv is not null)
                        {
                            var wallet = new WalletId(storeId, "BTC");
                            <a class="btn btn-link w-100" permission="@Policies.CanModifyStoreSettings" asp-controller="UIWallets" asp-action="WalletSend" asp-route-walletId="@wallet" asp-route-defaultDestination="@inProgressSwap.bitcoinAddress">Send using BTCPay Wallet</a>
                        }
                        @{
                            var onChainSats = ni.onchainBalanceMsat / 1000;
                            if (inProgressSwap.minAllowedDeposit <= (long) onChainSats)
                            {
                                <div class="w-100">
                                    <form asp-action="Sweep" asp-route-storeId="@storeId">
                                        <input type="hidden" value="@inProgressSwap.bitcoinAddress" name="address"/>
                                        <button class="btn btn-link w-100" type="submit"> Sweep onchain funds to swap in</button>
                                    </form>
                                </div>
                            }
                        }
                    </div>

                </div>

                @if (inProgressSwap.unconfirmedSats + inProgressSwap.confirmedSats + (inProgressSwap.paidMsat * 1000) > 0)
                {
                    <div class="card truncate-center-id">
                        <span class="text-nowrap">@inProgressSwap.unconfirmedSats sats unconfirmed</span>
                        <span class="text-nowrap">@inProgressSwap.confirmedSats sats confirmed</span>
                        <span class="text-nowrap">@(inProgressSwap.paidMsat * 1000) sats paid</span>
                    </div>
                }
                @if (inProgressSwap.unconfirmedTxIds.Any())
                {
                    <div class="card truncate-center-id">

                        <span class="text-nowrap">Unconfirmed transactions</span>
                        @foreach (var txId in inProgressSwap.unconfirmedTxIds)
                        {
                            <vc:truncate-center text="@txId" link="@TransactionLinkProviders.GetTransactionLink(pmi, txId)" classes="truncate-center-id"/>
                        }
                    </div>
                }
                @if (inProgressSwap.confirmedTxIds.Any())
                {
                    <div class="card truncate-center-id">

                        <span class="text-nowrap">Confirmed transactions</span>
                        @foreach (var txId in inProgressSwap.confirmedTxIds)
                        {
                            <vc:truncate-center text="@txId" link="@TransactionLinkProviders.GetTransactionLink(pmi, txId)" classes="truncate-center-id"/>
                        }
                    </div>
                }
            </div>
        }
        @if (refundables?.Any() is true)
        {
            <table class="table">
                <thead>
                <tr>
                    <th>Status</th>
                    <th>Created</th>
                    <th>Deposit Address</th>
                    <th>Payment</th>
                    <th>Actions</th>
                </tr>
                </thead>
                <tbody>
                @foreach (var refund in refundables)
                {
                    <tr>
                        <td>@refund.status</td>
                        <td>@DateTimeOffset.FromUnixTimeSeconds(refund.createdAt)</td>
                        <td>@refund.bitcoinAddress</td>
                        <td>
                            @if (refund.unconfirmedSats + refund.confirmedSats + refund.paidMsat > 0)
                            {
                                <div class="card truncate-center-id">
                                    <span class="text-nowrap">@refund.unconfirmedSats sats unconfirmed</span>
                                    <span class="text-nowrap">@refund.confirmedSats sats confirmed</span>
                                    <span class="text-nowrap">@(refund.paidMsat * 1000) sats paid</span>
                                </div>
                            }
                            @if (refund.unconfirmedTxIds.Any())
                            {
                                <div class="card truncate-center-id">

                                    <span class="text-nowrap">Unconfirmed transactions</span>
                                    @foreach (var txId in refund.unconfirmedTxIds)
                                    {
                                        <vc:truncate-center text="@txId" link="@TransactionLinkProviders.GetTransactionLink(pmi, txId)" classes="truncate-center-id"/>
                                    }
                                </div>
                            }
                            @if (refund.confirmedTxIds.Any())
                            {
                                <div class="card truncate-center-id">

                                    <span class="text-nowrap">Confirmed transactions</span>
                                    @foreach (var txId in refund.confirmedTxIds)
                                    {
                                        <vc:truncate-center text="@txId" link="@TransactionLinkProviders.GetTransactionLink(pmi, txId)" classes="truncate-center-id"/>
                                    }
                                </div>
                            }
                            @if (refund.refundTxIds.Any())
                            {
                                <div class="card truncate-center-id">

                                    <span class="text-nowrap">Refund transactions</span>
                                    @foreach (var txId in refund.refundTxIds)
                                    {
                                        <vc:truncate-center text="@txId" link="@TransactionLinkProviders.GetTransactionLink(pmi, txId)" classes="truncate-center-id"/>
                                    }
                                </div>
                            }

                        </td>
                        <td>
                            <a class="btn btn-link" asp-controller="Breez" asp-action="SwapInRefund" asp-route-storeId="@storeId" asp-route-address="@refund.bitcoinAddress">Start Refund</a>
                        </td>
                    </tr>
                }
                </tbody>
            </table>
        }
    </div>
</div>