
@using NBitcoin
@using BTCPayServer.Abstractions.Contracts
@using BTCPayServer.Plugins.Wabisabi
@using WalletWasabi.Blockchain.Analysis
@model BTCPayServer.Models.WalletViewModels.WalletSendModel

@inject WalletProvider  _walletProvider
@inject IScopeProvider ScopeProvider
@{
    var storeId = ScopeProvider.GetCurrentStoreId();
    var w = await _walletProvider.GetWalletAsync(storeId);
}

@if (w is not null)
{
    
    <div style="display: none">
        <div id="wabisabitemplate">
            <button type="button"
                    id="privatesend"
                    class="btn btn-primary"
                    title="Optimzie coin selection for privacy">
                Private coin selection
                <i class="fa fa-user-secret"></i>
            </button>
        </div>

    </div>
    <datalist id="wabisabidenominations">
        @foreach (var stdDenom in BlockchainAnalyzer.StdDenoms)
        {
            var num = new Money(stdDenom).ToDecimal(MoneyUnit.BTC);
        <option value="@num">Privacy suggestion (used in coinjoins)</option>
        }
    </datalist>
    

<script type="text/javascript">
 document.addEventListener("DOMContentLoaded", function () {
     
                const amountElements = document.querySelectorAll("[name^='Outputs'][name$='Amount']");
     amountElements.forEach(value => {
                         value.setAttribute("list","wabisabidenominations" );
                     });
     document.querySelector("#SignTransaction").insertAdjacentElement("beforeBegin", document.getElementById("wabisabitemplate"))
     
     document.getElementById("privatesend").addEventListener("click", async ev => {
         document.getElementById("InputSelection").value = "True";
                const amountElements = document.querySelectorAll("[name^='Outputs'][name$='Amount']");
                const labelElements = document.querySelectorAll("[name^='Outputs'][name$='Labels'] option:checked");
                let amount = 0;
                amountElements.forEach(value => {
                    try {
                      
                    amount+= parseFloat(value.value);
                    }catch{}
                });
                if (!amount){
                    return;
                }
                let labels = [];
                labelElements.forEach(value => {
                    labels.push(value.value);
                });
                let url =@Safe.Json(@Url.Action("ComputeCoinSelection", "WabisabiStore", new { storeId }));
                url += `?amount=${amount}`;
                url += labels.length >0?  "&" + labels.map(x => "labels=" + encodeURIComponent(x)).join("&"): "";
                const response = await fetch(url);
                const coins = await response.json();
               let selectedInputsElement =  document.getElementById("SelectedInputs");
               if (!selectedInputsElement){
                selectedInputsElement = document.createElement("select");
                selectedInputsElement.setAttribute("name", "SelectedInputs")
                selectedInputsElement.setAttribute("id", "SelectedInputs")
                selectedInputsElement.setAttribute("multiple", "multiple")
                selectedInputsElement.style.display = "none";
                ev.target.insertAdjacentElement("beforeBegin",selectedInputsElement);
               }else{
                    while (selectedInputsElement.options.length > 0) {
                       selectedInputsElement.remove(0);
                   }
               }
               for (const coin of coins) {
                 selectedInputsElement.add(new Option(coin, coin, true, true),undefined);
               }
            document.getElementsByTagName("form")[0].submit();  
     });
        });
 
</script>
}