// --------------------------------------------------------
//                      Autostart
// --------------------------------------------------------

$(function(){
    $(".toggle-switch").bootstrapSwitch();

    $(".timestamp").relTimestamp();
});

// --------------------------------------------------------
// JQuery helper functions
// --------------------------------------------------------

// $.attr() # returns all attributes of an element
(function(old) {
    $.fn.attr = function() {
        if(arguments.length === 0) {
            if(this.length === 0) {
                return null;
            }

            var obj = {};
            $.each(this[0].attributes, function() {
                if(this.specified) {
                    obj[this.name] = this.value;
                }
            });
            return obj;
        }

        return old.apply(this, arguments);
    };
})($.fn.attr);

// --------------------------------------------------------
// Timestamps
// --------------------------------------------------------

// Converts all timestamps to human readable time and date

$.fn.relTimestamp = function() {
    return this.each(function() {
        var timestamp = parseInt($(this).text());
        var timestampNow = Date.now();
        var timeDiff = timestampNow - timestamp;

        if(timeDiff < 10 * 60 * 1000) // less than 10 min
            $(this).text(moment(timestamp).fromNow());
        else if(timeDiff < 24 * 60 * 60 * 1000) // less than 24 hours
            $(this).text(moment(timestamp).fromNow() + " ("+moment(timestamp).format("HH:mm")+")");
        else
            $(this).text(moment(timestamp).format("YYYY-MM-DD HH:mm"));
        return this;
    });
};

// --------------------------------------------------------
// Dynamic forms
// --------------------------------------------------------

var dynamicConf = {};

function initDynamicModalForm(modalId, formTemplateId = null, templateID = null){
    // read in all configurations into global variable (to skip naming issues)
    if (formTemplateId != null) {
        dynamicConf[formTemplateId] = [];
        $("#" + templateID + " div").each(function(){
            dynamicConf[formTemplateId][$(this).attr("id")] = $(this).html();
        });
        // Update dynamic inputs
        $("#" + modalId + " select[name=type]").change(function(){
            $("#" + modalId + " #"+formTemplateId).html(dynamicConf[formTemplateId][$(this).val()]);
        });
    }

    // Listen to click event

    $("#" + modalId).on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget);
        var modal = $(this);

        // Reset all inputs
        if (formTemplateId != null)
            modal.find("#" + formTemplateId).empty(); // clear form div

        // select dynamic form
        var selector = modal.find("select[name=type]");
        selector.val(button.data("type"));
        selector.change(); // Update dynamic inputs
        // set dynamic form data
        $.each(button.attr(), function(fieldName, value) {
            if(fieldName.startsWith("data-")) {
                fieldName = fieldName.substring(5);
                // case insensitive search
                input = modal.find("input").filter(function() {
                       return this.name.toLowerCase() == fieldName;
                });
                if (input.attr("type") == "checkbox") { // special handling for checkboxes
                    input.attr("value", "true");
                    if (value=="true") input.attr("checked", "true");
                    else               input.removeAttr("checked");
                    // Add default false value as a unchecked checkbox is not included in the post
                    input.parent().prepend("<input type='hidden' name='" + input.attr("name") + "' value='false' />");
                } else {
                    input.val(value);
                }
            }
        });
    });
}