/*
 * ============================== - Async Help
 *                                  _    _      _
 *     /\                          | |  | |    | |
 *    /  \   ___ _   _ _ __   ___  | |__| | ___| |_ __
 *   / /\ \ / __| | | | '_ \ / __| |  __  |/ _ \ | '_ \
 *  / ____ \\__ \ |_| | | | | (__  | |  | |  __/ | |_) |
 * /_/    \_\___/\__, |_| |_|\___| |_|  |_|\___|_| .__/
 *                __/ |                          | |
 *               |___/                           |_|
 *
*/

function asyncRedirect(target, method) {
    $('#progress').removeClass('hidden');
    let request = fetch(target, {
        method: method,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'X-Requested-With': 'fetch'
        }
    }).then(function (response) {
        return response.text();
    }).then(function (data) {
        $('body #content').html(data).trigger('reloaded');
        history.replaceState({}, '', target);
    }).catch(function (error) {

    }).finally(function () {
        $('#progress').addClass('hidden');
    });
}

/*
 * Fill a given element with content on a
 * remote location
 *
 */
function asyncFill(target, method, location, callback = function () { }, params = {}, payload = {}) {
    fetch(target, {
        method: method,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'X-Requested-With': 'fetch'
        }
    }).then(function (response) {
        return response.text();
    }).then(function (data) {
        $(location).html(data).trigger('filled');
    }).catch(function (error) {
    }).finally(function () {
        callback(params);
    });
}

/**
 * Search any type of content on a remote location
 *
 **/
async function asyncContent(target, method, type, callback = function () { }) {
    return fetch(target, {
        method: method,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'X-Requested-With': 'fetch'
        }
    }).then(function (response) {
        var content = "";

        if (type == 'text') {
            content = response.text();
        } else {
            content = response.json();
        }

        return {
            "status": response.ok,
            "content": content
        };

    }).then(function (data) {
        callback(data.content);
        return data;
    }).finally(function (content) {

    });

}


/*
 * ============================== Modal
 *   _  __           _       _
 * |  \/  |         | |     | |
 * | \  / | ___   __| | __ _| |
 * | |\/| |/ _ \ / _` |/ _` | |
 * | |  | | (_) | (_| | (_| | |
 * |_|  |_|\___/ \__,_|\__,_|_|
 *
*/

var scan;

var modalTarget = {}; // Store field where the modal will place the selected stuff
let modal = new bootstrap.Modal($('#modal'), { keyboard: true });
var modalClose;

$('#modal').on('hidden.bs.modal', function (evt) {
    $(this).find('.modal-body').html('');
    $(this).find('.modal-title').html('');
    if (typeof modalClose === "function") {
        modalClose();
    }
});

async function openModal(params) {
    modalTarget = params.target;
    modalClose = params.close;
    modal.toggle();
}