module.exports = function () {
    var template = '<div class="modal fade confirm-box" tabindex="-1" role="dialog">' +
        '<div class="modal-dialog"><div class="modal-content" style="width: 450px;"><div class="modal-body">' +
        '{BODY}</div><div class="modal-footer">{Buttons} </div></div></div></div>';
    var run = function(options, callback) {
        $(".confirm-box").remove();
        var html = $(renderTemplate(options));

        $(document.body).append(html);
        $(".confirm-box").modal({
            show: true,
            backdrop: 'static',
            keyboard: false
        });
        if (options.buttons) {
            for (var i = 0; i < options.buttons.length; i++) {
                var btnItem = options.buttons[i];
                var btnTag = $(".confirm-box").find(".btn-" + btnItem.type).get(0);
                btnTag.addEventListener('click', event => {
                    var value = event.target.getAttribute("data-value");
                    $(".confirm-box").modal('hide');
                    if (value === "true")
                        callback(true);
                    else if (value === "false")
                        callback(false);
                    else if (value === "undefined")
                        callback(undefined);
                    else
                        callback(value);
                });
            }
        }
    }
    var renderTemplate = function(options) {
        var temp = template;
        if (options.text) {
            temp = temp.replace("{BODY}",'<p style="font-size: 12px;font-weight: 300;">' + options.text  + "</p>");
        }
        if (options.buttons) {
            var btTags ="";
            var signType;
            for (var i = 0; i < options.buttons.length; i++) {
                var btnItem = options.buttons[i];
                btTags += '<button type="button" data-value="'+ btnItem.value +'" class="btn btn-' + btnItem.type + ' btn-sm">'+ btnItem.text  + '</button>';
                if (btnItem.value === true)
                    signType = btnItem.type;
            }
            temp = temp.replace("{Buttons}", btTags);
            return temp;
        } 
    };
    var renderIcon = function(type) {
        switch (type) {
        case "success":
            return '<div class="swal2-icon swal2-success"><div class="swal2-success-circular-line-left"></div><span class="swal2-success-line-tip"></span> <span class="swal2-success-line-long"></span><div class="swal2-success-ring"></div> <div class="swal2-success-fix"></div><div class="swal2-success-circular-line-right"></div></div>';
        case "danger":
            return '<div class="swal2-icon swal2-error"><span class="swal2-x-mark"><span class="swal2-x-mark-line-left"></span><span class="swal2-x-mark-line-right"></span></span></div>';
        case "warning":
            return '<div class="swal2-icon swal2-warning"><div class="swal2-icon-content">!</div></div>';
        case "info":
            return '<div class="swal2-icon swal2-info"><div class="swal2-icon-content">i</div></div>';
        }
    }
    return {
        run: run
    }
}();