/*
 * 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 bsalert(title, message, okbtn, cancelbtn, callback) {
    var html = '<div class="modal fade" id="bsalert">'
            + '  <div class="modal-dialog">'
            + '    <div class="modal-content">'
            + '      <div class="modal-header">'
            + '        <h5 class="modal-title" id="bsalert-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="bsalert-body">'
            + '        <div id="bsalert-message"></div>'
            + '      </div>'
            + '      <div class="modal-footer">'
            + '        <button type="button" class="btn btn-secondary" data-dismiss="modal" id="bsalert-cancel">Cancel</button>'
            + '        <button type="button" class="btn btn-primary" id="bsalert-ok">OK</button>'
            + '      </div>'
            + '    </div>'
            + '  </div>'
            + '</div>';
    $("body").append(html);
    $("#bsalert-title").text(title);
    $("#bsalert-message").html(message);
    if (typeof okbtn != "string") {
        okbtn = "OK";
    }
    $("#bsalert-ok").text(okbtn);
    if (typeof cancelbtn != "string") {
        cancelbtn = "Cancel";
    }
    $("#bsalert-cancel").text(cancelbtn);
    $("#bsalert-ok").on("click", function () {
        if (typeof callback != 'undefined') {
            callback();
        }
        $("#bsalert").modal("hide");
    });
    $("#bsalert").on("hidden.bs.modal", function () {
        $("#bsalert").remove();
    });
    $("#bsalert").modal("show");
}

/**
 * Prompt the user for input with a modal dialog.
 * @param {type} title Modal title
 * @param {type} message Message content
 * @param {type} okbtn Text for the OK button
 * @param {type} cancelbtn Text for the cancel button
 * @param {type} type Input box type (text, number, etc)
 * @param {type} callback Function to pass the user input to
 * @returns {undefined}
 */
function bsprompt(title, message, okbtn, cancelbtn, type, callback) {
    var html = '<div class="modal fade" id="bsprompt">'
            + '  <div class="modal-dialog">'
            + '    <div class="modal-content">'
            + '      <div class="modal-header">'
            + '        <h5 class="modal-title" id="bsprompt-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="bsprompt-body">'
            + '        <div id="bsprompt-message"></div>'
            + '        <input id="bsprompt-input" class="form-control" />'
            + '      </div>'
            + '      <div class="modal-footer">'
            + '        <button type="button" class="btn btn-secondary" data-dismiss="modal" id="bsprompt-cancel">Cancel</button>'
            + '        <button type="button" class="btn btn-primary" id="bsprompt-ok">OK</button>'
            + '      </div>'
            + '    </div>'
            + '  </div>'
            + '</div>';
    $("body").append(html);
    $("#bsprompt-title").text(title);
    $("#bsprompt-message").text(message);
    $("#bsprompt-ok").text(okbtn);
    $("#bsprompt-cancel").text(cancelbtn);
    $("#bsprompt-input").attr("type", type);
    $("#bsprompt-input").on("keypress", function (e) {
        if (e.which === 13) {
            callback($("#bsprompt-input").val());
            $("#bsprompt").modal("hide");
        }
    });
    $("#bsprompt-ok").on("click", function () {
        callback($("#bsprompt-input").val());
        $("#bsprompt").modal("hide");
    });
    $("#bsprompt").on("shown.bs.modal", function () {
        $("#bsprompt-input").focus();
    });
    $("#bsprompt").on("hidden.bs.modal", function () {
        $("#bsprompt").remove();
    });
    $("#bsprompt").modal("show");
}

function bschoices(title, message, options, cancelbtn, callback) {
    var html = '<div class="modal fade" id="bschoices">'
            + '  <div class="modal-dialog">'
            + '    <div class="modal-content">'
            + '      <div class="modal-header">'
            + '        <h5 class="modal-title" id="bschoices-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="bschoices-body">'
            + '        <div id="bschoices-message" class="mb-2"></div>'
            + '        <div class="list-group" id="bschoices-list"></div>'
            + '      </div>'
            + '      <div class="modal-footer">'
            + '        <button type="button" class="btn btn-secondary" data-dismiss="modal" id="bschoices-cancel">Cancel</button>'
            + '      </div>'
            + '    </div>'
            + '  </div>'
            + '</div>';
    $("body").append(html);
    $("#bschoices-title").text(title);
    $("#bschoices-message").text(message);
    $("#bschoices-cancel").text(cancelbtn);
    for (var i = 0; i < options.length; i++) {
        var text = options[i]["text"];
        var val = options[i]["val"];
        $("#bschoices-list").append('<div class="list-group-item bschoices-option d-flex justify-content-between" data-value="' + val + '">' + text + '</div>');
    }
    $(".bschoices-option").css("cursor", "pointer");
    $(".bschoices-option").on("click", function () {
        callback($(this).data("value"));
        $("#bschoices").modal("hide");
    });
    $("#bschoices").on("hidden.bs.modal", function () {
        $("#bschoices").remove();
    });
    $("#bschoices").modal("show");
}