function mgBytesToSize(bytes) {
    var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
    if (bytes <= 1) {
        if (bytes !== 0) {
            var bytes = Number(bytes).toFixed(1);
        }
        return bytes + ' Byte';
    }
    var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
    var total = bytes / Math.pow(1024, i) ;
    if(sizes[i] == 'GB' || sizes[i] == 'TB' ){
        return total.toFixed(2)+ ' ' + sizes[i];
    }
    return Math.round( total, 2) + ' ' + sizes[i];
}

function mgTooltipCpu(tooltipItem, data) {
    var used = Number(tooltipItem.yLabel).toFixed(2);
    return used + "%";
}

function mgTooltipServerLoad(tooltipItem, data) {
    var used = Number(tooltipItem.yLabel).toFixed(2);
    return used;
}

function mgTooltipCallbackForMemory(tooltipItem, data) {
    return mgBytesToSize(tooltipItem.yLabel) + "/s";
}

function mgTooltipCallbackForNet(tooltipItem, data) {
    return mgBytesToSize(tooltipItem.yLabel) + "/s";
}

function mgTooltipCallbackForDisk(tooltipItem, data) {
    return mgBytesToSize(tooltipItem.yLabel) + "/s";
}

/*Disk support*/
function pmToggleDiskButton(data) {
    let button = $(".pmCreateDiskButton");
    if(data.htmlData.createButtonStatus)
    {
        button.removeClass('hidden');
    }
    else
    {
        button.addClass('hidden');
    }
}
/*Snapshot support*/
function pmToggleSnapshotButton(data) {
    let button = $(".pmCreateSnapshotButton");
    if(data.htmlData.createButtonStatus)
    {
        button.removeClass('hidden');
    }
    else
    {
        button.addClass('hidden');
    }
}

/*Backup*/
function pmToggleBackupButton(data) {
    let button = $(".pmCreateBackupButton");
    if(data.htmlData.createButtonStatus)
    {
        button.removeClass('hidden');
    }
    else
    {
        button.addClass('hidden');
    }
}

/*Firewall*/
function pmToggleFirewallButton(data) {
    let button = $(".pmCreateFirewallButton");
    if(data.htmlData.createButtonStatus)
    {
        button.removeClass('hidden');
    }
    else
    {
        button.addClass('hidden');
    }
}

function mgLocationReload(data) {
    window.location.reload();
}

/* VM delete done*/
function pcVmDeltedAjaxDone(data) {
    if(data.htmlData.redirectUrl)
    {
        window.location.href = data.htmlData.redirectUrl;
    }
}

$(document).ready(function () {
    //virtual network
    var selectizedInstance = $('.pm-virtual-network-content-default .selectized').selectize();
    selectizedInstance.each(function(){
        this.selectize.destroy();
    });
    //create
    $("#pm-add-new-virtual-network-button").on("click",function (e) {
        e.preventDefault();
        var content = $('.pm-virtual-network-content-default').clone();
        content.removeClass("hidden");
        content.removeClass("pm-virtual-network-content-default");
        var index = $("#pm-add-new-virtual-network-button").closest('.lu-widget').find('.lu-widget').size();
        content.find('select[name="virtualNetwork[id]"]').prop("name","virtualNetwork"+index+"[id]");
        content.find('select[name="virtualNetwork[ip]"]').prop("name","virtualNetwork"+index+"[ip]");
        $(this).closest('.lu-widget').find('.lu-widget').last().after(content);
        $(this).closest('.lu-widget').find('.pm-vn-select').trigger("change");
        $(this).closest('.lu-widget').find('.lu-alert').hide();
        $(".pm-virtual-network-content-default").parent().find(".lu-alert").hide();
    });
    //delete
    $(".mg-wrapper").delegate(".pm-delete-virtual-network-button","click",function (e) {
        e.preventDefault();
        $(this).closest('.lu-widget').remove();
        if($(".pm-virtual-network-content-default").parent().find(".lu-widget").length <= 1)
        {
            $(".pm-virtual-network-content-default").parent().find(".lu-alert").show();
        }
    });
    //change
    $(".mg-wrapper").delegate(".pm-vn-select","change",function (e) {
        var show = '.vn-ip-'+$(this).val();
        $(this).closest('.lu-widget__content').find('.pm-vn-ip-select option').hide();
        $(this).closest('.lu-widget__content').find(show).show();
        var firstOption =  $(this).closest('.lu-widget__content').find('.pm-vn-ip-select '+show+':first').val();
        $(this).closest('.lu-widget__content').find('.pm-vn-ip-select').val(firstOption);
    });
    //disk
    var selectizedInstance = $('.pm-disk-content-default .selectized').selectize();
    selectizedInstance.each(function(){
        this.selectize.destroy();
    });

    //create
    $("#pm-add-disk-button").on("click",function (e) {
        e.preventDefault();
        var content = $('.pm-disk-content-default').clone();
        content.removeClass("hidden");
        content.removeClass("pm-disk-content-default");
        var index = $("#pm-add-disk-button").closest('.lu-widget').find('.lu-widget').size();
        content.find('input[name="additionalDiskSize"]').prop("name","additionalDiskSize"+index);
        content.find('select[name="additionalDiskBus"]').prop("name","additionalDiskBus"+index);
        content.find('select[name="additionalDiskFormat"]').prop("name","additionalDiskFormat"+index);
        content.find('select[name="additionalDiskBackup"]').prop("name","additionalDiskBackup"+index);
        content.find('input[name="additionalDiskMp"]').prop("name","additionalDiskMp"+index);
        $(this).closest('.lu-widget').find('.lu-widget').last().after(content);
        $(".pm-disk-content-default ").parent().find(".lu-alert").hide();
    });
    //delete
    $(".mg-wrapper").delegate(".pm-delete-disk-button","click",function (e) {
        e.preventDefault();
        $(this).closest('.lu-widget').remove();
        if($(".pm-disk-content-default ").parent().find(".lu-widget").length <= 1)
        {
            $(".pm-disk-content-default ").parent().find(".lu-alert").show();
        }
    });
    //cpuPriority change
    $(".mg-wrapper").delegate("#cpuPriority","input",function (e) {
        e.preventDefault();
        var label = $(this).data("label"+$(this).val());
        $("#outputcpuPriority").html(label);
    });
});

$(".mg-wrapper").on("change", "select[name='macro']", function () {
    var disable = $(this).val()!='0';
    if(disable){
        $("input[name='sport']").closest(".lu-form-group").addClass("disabled");
        $("input[name='dport']").closest(".lu-form-group").addClass("disabled");
        $("select[name='proto']").closest(".lu-form-group").addClass("disabled");
    }else{
        $("input[name='sport']").closest(".lu-form-group").removeClass("disabled");
        $("input[name='dport']").closest(".lu-form-group").removeClass("disabled");
        $("select[name='proto']").closest(".lu-form-group").removeClass("disabled");
    }
});

mgEventHandler.on('ModalLoaded', 'updateButton', function(id, params){
    if(id != 'updateButton'){
        return;
    }
    $(".mg-wrapper select[name='macro']").trigger('change');

}, 1000);
