/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */


function bsmoneypad(title, amount, okbtn, cancelbtn, denominations, callback) {
    var html = '<div class="modal fade" id="bsmoneypad">'
            + '  <div class="modal-dialog">'
            + '    <div class="modal-content">'
            + '      <div class="modal-header">'
            + '        <h5 class="modal-title" id="bsmoneypad-title"></h5>'
            + '        <button type="button" class="close" data-dismiss="modal" aria-label="Close">'
            + '          <span aria-hidden="true">&times;</span>'
            + '        </button>'
            + '      </div>'
            + '      <div class="modal-body" id="bsmoneypad-body">'
            + '        <div class="d-flex justify-content-between align-content-center m-2">'
            + '          <div class="btn btn-outline-success" id="bsmoneypad-plusminus" data-direction="plus"><i class="fas fa-plus"></i></div>'
            + '          <div class="h2" id="bsmoneypad-amount" data-amount="0">$0.00</div>'
            + '          <div class="btn btn-outline-primary" id="bsmoneypad-reset"><i class="fas fa-eraser"></i></div>'
            + '        </div>'
            + '        <div id="bsmoneypad-pad" class="d-flex flex-wrap"></div>'
            + '      </div>'
            + '      <div class="modal-footer">'
            + '        <button type="button" class="btn btn-secondary" data-dismiss="modal" id="bsmoneypad-cancel">Cancel</button>'
            + '        <button type="button" class="btn btn-primary" id="bsmoneypad-ok">OK</button>'
            + '      </div>'
            + '    </div>'
            + '  </div>'
            + '</div>';
    $("body").append(html);
    if (denominations == null) {
        denominations = [1.00, 2.00, 5.00, 10.00, 20.00, 50.00, 100.00, null, 0.01, 0.05, 0.10, 0.25, 0.50];
    }
    function addButton(val, label) {
        $("#bsmoneypad-pad").append('<div class="btn btn-default moneypadbtn m-1" data-amount="' + val + '"><h5>' + label + '</h5></div>');
    }
    for (var i = 0; i < denominations.length; i++) {
        if (denominations[i] === null) {
            $("#bsmoneypad-pad").append('<div class="w-100 my-0 py-0"><hr /></div>');
            continue;
        }
        var lbl = "";
        if (denominations[i] < 1) {
            lbl = denominations[i] * 100.0 + "¢";
        } else if (denominations[i] % 1 === 0) {
            lbl = "$" + denominations[i];
        } else {
            lbl = "$" + denominations[i].toFixed(2);
        }
        addButton(denominations[i], lbl);
    }
    $("#bsmoneypad-title").html(title);
    $("#bsmoneypad-amount").text("$" + (amount * 1.0).toFixed(2));
    $("#bsmoneypad-amount").data('amount', (amount * 1.0));
    $("#bsmoneypad-ok").html(okbtn);
    $("#bsmoneypad-cancel").html(cancelbtn);
    $("#bsmoneypad-pad").on("click", ".moneypadbtn", function () {
        var total = $("#bsmoneypad-amount").data('amount') * 1.0;
        var amount = $(this).data('amount');
        if ($("#bsmoneypad-plusminus").data("direction") == "plus") {
            total += amount;
        } else {
            total -= amount;
        }
        $("#bsmoneypad-amount").text("$" + (total * 1.0).toFixed(2));
        $("#bsmoneypad-amount").data('amount', (total * 1.0));
    });
    $("#bsmoneypad-plusminus").on("click", function () {
        if ($(this).data("direction") == "plus") {
            $(this).data("direction", "minus");
            $(this).removeClass("btn-outline-success");
            $(this).addClass("btn-outline-danger");
            $(this).html('<i class="fas fa-minus"></i>');
        } else {
            $(this).data("direction", "plus");
            $(this).removeClass("btn-outline-danger");
            $(this).addClass("btn-outline-success");
            $(this).html('<i class="fas fa-plus"></i>');
        }
    });
    $("#bsmoneypad-reset").on("click", function () {
        $("#bsmoneypad-amount").text("$0.00");
        $("#bsmoneypad-amount").data('amount', 0.0);
    });
    $("#bsmoneypad-ok").on("click", function () {
        callback($("#bsmoneypad-amount").data('amount').toFixed(2) * 1.0);
        $("#bsmoneypad").modal("hide");
    });
    $("#bsmoneypad").on("shown.bs.modal", function () {
        $("#bsmoneypad-input").focus();
    });
    $("#bsmoneypad").on("hidden.bs.modal", function () {
        $("#bsmoneypad").remove();
    });
    $("#bsmoneypad").modal("show");
}