@model BTCPayServer.Models.StoreViewModels.RatesViewModel
@{
    Layout = "../Shared/_NavLayout.cshtml";
    ViewData["Title"] = "Rates";
    ViewData.AddActivePage(BTCPayServer.Views.Stores.StoreNavPages.Rates);
}

<h4>@ViewData["Title"]</h4>
<partial name="_StatusMessage" for="@TempData["TempDataProperty-StatusMessage"]" />

<div class="row">
    <div class="col-md-8">
        <div asp-validation-summary="All" class="text-danger"></div>
    </div>
</div>
<div class="row">
    <div class="col-md-8">
        <form method="post">
            @if(Model.ShowScripting)
            {
                <div class="form-group">
                    <h5>Scripting</h5>
                    <span>Rate script allows you to express precisely how you want to calculate rates for currency pairs.</span>
                    <p class="text-muted">
                        <b>Supported exchanges are</b>:
                        @for(int i = 0; i < Model.AvailableExchanges.Length; i++)
                        {
                            <a href="@Model.AvailableExchanges[i].Url">@Model.AvailableExchanges[i].Name</a><span>@(i == Model.AvailableExchanges.Length - 1 ? "" : ",")</span>
                        }
                    </p>
                    <p><a href="#help" data-toggle="collapse"><b>Click here for more information</b></a></p>
                </div>
            }
            @if(Model.TestRateRules != null)
            {
                <div class="form-group">
                    <h5>Test results:</h5>
                    <table class="table table-sm table-responsive-md">
                        <tbody>
                            @foreach(var result in Model.TestRateRules)
                            {
                                <tr>
                                    @if(result.Error)
                                    {
                                        <th class="small"><span class="fa fa-times" style="color:red;"></span> @result.CurrencyPair</th>
                                    }
                                    else
                                    {
                                        <th class="small"><span class="fa fa-check" style="color:green;"></span> @result.CurrencyPair</th>
                                    }
                                    <td class="small">@result.Rule</td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            }
            @if(Model.ShowScripting)
            {
                <div id="help" class="collapse text-left">
                    <p>
                        The script language is composed of several rules composed of a currency pair and a mathematic expression.
                        The example below will use <code>gdax</code> for both <code>LTC_USD</code> and <code>BTC_USD</code> pairs.
                    </p>
                    <pre>
                    <code>
                            LTC_USD = gdax(LTC_USD);
                            BTC_USD = gdax(BTC_USD);
                        </code>
                    </pre>
                    <p>However, explicitely setting specific pairs like this can be a bit difficult. Instead, you can define a rule <code>X_X</code> which will match any currency pair. The following example will use <code>gdax</code> for getting the rate of any currency pair.</p>
                    <pre>
                    <code>
                            X_X = gdax(X_X);
                        </code>
                    </pre>
                    <p>However, <code>gdax</code> does not support the <code>BTC_CAD</code> pair. For this reason you can add a rule mapping all <code>X_CAD</code> to <code>quadrigacx</code>, a Canadian exchange.</p>
                    <pre>
                    <code>
                            X_CAD = quadrigacx(X_CAD);
                            X_X = gdax(X_X);
                        </code>
                    </pre>
                    <p>A given currency pair match the most specific rule. If two rules are matching and are as specific, the first rule will be chosen.</p>
                    <p>
                        But now, what if you want to support <code>DOGE</code>? The problem with <code>DOGE</code> is that most exchange do not have any pair for it. But <code>bittrex</code> has a <code>DOGE_BTC</code> pair. <br />
                        Luckily, the rule engine allow you to reference rules:
                    </p>
                    <pre>
                    <code>
                            DOGE_X = bittrex(DOGE_BTC) * BTC_X
                            X_CAD = quadrigacx(X_CAD);
                            X_X = gdax(X_X);
                        </code>
                    </pre>
                    <p>With <code>DOGE_USD</code> will be expanded to <code>bittrex(DOGE_BTC) * gdax(BTC_USD)</code>. And <code>DOGE_CAD</code> will be expanded to <code>bittrex(DOGE_BTC) * quadrigacx(BTC_CAD)</code>. <br />
                    However, we advise you to write it that way to increase coverage so that <code>DOGE_BTC</code> is also supported:</p>
                    <pre>
                        <code>
                            DOGE_X = DOGE_BTC * BTC_X
                            DOGE_BTC = bittrex(DOGE_BTC)
                            X_CAD = quadrigacx(X_CAD);
                            X_X = gdax(X_X);
                        </code>
                    </pre>
                    <p>It is worth noting that the inverses of those pairs are automatically supported as well.<br />
                    It means that the rule <code>USD_DOGE = 1 / DOGE_USD</code> implicitely exists.</p>

                </div>
                <div class="form-group">
                    <label asp-for="Script"></label>
                    <textarea asp-for="Script" rows="20" cols="80" class="form-control"></textarea>
                    <span asp-validation-for="Script" class="text-danger"></span>
                    <a href="#" onclick="$('#Script').val(defaultScript); return false;">Set to default settings</a>
                </div>
                <div class="form-group">
                    <a asp-action="ShowRateRules" asp-route-scripting="false">Turn off advanced rate rule scripting</a>
                </div>
            }
            else
            {
                <div class="form-group">
                    <label asp-for="PreferredExchange"></label>
                    <select asp-for="PreferredExchange" asp-items="Model.Exchanges" class="form-control"></select>
                    <span asp-validation-for="PreferredExchange" class="text-danger"></span>
                    <p id="PreferredExchangeHelpBlock" class="form-text text-muted">
                        Current price source is <a href="@Model.RateSource" target="_blank">@Model.PreferredExchange</a>.
                    </p>
                </div>
                <div class="form-group">
                    <a asp-action="ShowRateRules" asp-route-scripting="true">Turn on advanced rate rule scripting</a>
                </div>
            }
            <div class="form-group">
                <label asp-for="RateMultiplier"></label>
                <input asp-for="RateMultiplier" class="form-control" />
                <span asp-validation-for="RateMultiplier" class="text-danger"></span>
            </div>
            <div class="form-group">
                <h5>Testing</h5>
                <span>Enter currency pairs which you want to test against your rule (eg. <code>DOGE_USD,DOGE_CAD,BTC_CAD,BTC_USD</code>)</span>
                <div class="input-group">
                    <input placeholder="BTC_USD, BTC_CAD" asp-for="ScriptTest" class="form-control" />
                    <span class="input-group-btn">
                        <button name="command" value="Test" type="submit" class="btn btn-primary" title="Test">
                            <span class="fa fa-vial"></span> Test
                        </button>
                    </span>
                </div>
                <span asp-validation-for="ScriptTest" class="text-danger"></span>
            </div>
            <button name="command" type="submit" class="btn btn-primary" value="Save">Save</button>
            <input type="hidden" asp-for="ShowScripting" />
        </form>
    </div>
</div>

@section Scripts {
    <script type="text/javascript">var defaultScript = @Html.Raw(Json.Serialize(Model.DefaultScript));</script>
    @await Html.PartialAsync("_ValidationScriptsPartial")
}
