// testCaseVersionData contains the variants and versions of the test case
var testObjectVersionData = {};

function fillVariants(variants, selectorID) {
    $('#inputTestCaseSUTVariants').empty();
    $.each(variants, function (key, variant) {
        addVariant(variant, selectorID);
    });
}


function addVariant(variant, selectorID) {
    var selector = $(selectorID);
    var selectedValue = selector.val();
    selector.append($("<option></option>")
        .attr("value", variant.Name)
        .text(variant.Name));
    selector.value = selectedValue;
}


// Update the list with versions when selecting another variant
function setVariantOnChangeListener() {
    $('#inputTestCaseSUTVariants').on('change', function () {
        updateVersionShowList()
    });
}

// Update the list with versions when selecting another variant
function setVariantDropdownOnChangeListener(isSequence) {
    $('#inputTestObjectSUTVariants').on('change', function () {
        updateVersionDropdown(testObjectVersionData, "#inputTestObjectSUTVariants", isSequence);
    });
}

/*
 * Updates the list with the versions
 */
function updateVersionShowList() {

    var variantKey = $('#inputTestCaseSUTVariants').val();
    if (typeof variantKey == 'undefined'){
        return;
    }
    var list = $('#inputTestCaseSUTVersions');
    // remove all previously shown elements
    list.empty();
    // add versions of selected variant to list
    $.each(testObjectVersionData[variantKey].Versions, function (key, version) {
        var listElement = ($("<li></li>")
            .attr("class", "list-item")
            .html('<span>' + version.Name + '</span>'));
        list.append(listElement)
    });


}

/*
 * Updates the list with the versions
 */
function updateVersionDropdown(data, selectorID, isSequence) {
    var variantKey = $('#inputTestObjectSUTVariants').val();
    var list = $('#inputTestObjectSUTVersions');
    // remove all previously shown elements
    list.empty();
    // add versions of selected variant to list
    var versions;
    if(data != null) {
        versions = data[variantKey].Versions;
    }

    $.each(versions, function (key, version) {
        list.append($("<option></option>")
            .attr("value", version.Name)
            .text(version.Name));
    });


}

