// Disable the Visualforce row highlighting.
// https://developer.salesforce.com/forums/?id=906F00000008r1wIAA
window.hiOff = undefined;

// Constants
var PLUS_CIRCLE_CLASS = 'fa-plus-circle';
var MINUS_CIRCLE_CLASS = 'fa-minus-circle';

/**
 * A click event handler for radio buttons related to the import process.
 * Handles switching between file upload and paste.
 */
function importRadioButtonHandler() {
    // Hide all import containers for convenience sake.
    jQuery('.import-containers').hide();

    var clickedVal = jQuery(this).val();

    // Show the relevant import container.
    if (clickedVal === 'paste') {
        jQuery('.import-paste').show();
        jQuery('.import-upload .m-import-file').val('');
    } else {
        jQuery('.import-upload').show();
        jQuery('.import-paste .m-import-export-box').val('');
    }
}

/**
 * A click event handler for radio buttons related to the export process.
 * Handles switching between file download and copy.
 */
function exportRadioButtonHandler() {
    var clickedVal = jQuery(this).val();

    // Show the relevant import container.
    if (clickedVal === 'download') {
        jQuery('.export-to-file').val(true);
    } else {
        jQuery('.export-to-file').val(false);
    }
}

/**
 * A keypress handler for the inputs, triggers button click on enter.
 * @param {object} event - The event to intercept keypresses from.
 */
function exportEnterKeyupHandler(event) {
    if (event.which === 13) {
        event.preventDefault();

        jQuery('.m-export-button').click();
    }
}

/**
 * A click handler for filtering the table of records by "conflict type" dropdown selection.
 */
function filterByConflictTypeHandler() {
    var selectedValue = $(this).val();

    $("[id$='ConflictsHeader']").each(function() {
        var cellValue = $(this).text();
        if (selectedValue !== 'All') {
            if (selectedValue === cellValue) {
                $(this).parent().css('display', '');
            } else {
                $(this).parent().css('display', 'none');
            }
        } else {
            $(this).parent().css('display', '');
        }
    });
}

/**
 * A handler for performing "select all" style operations for sections of the record table.
 */
function selectAllCheckboxHandler() {
    var clickedInput = jQuery(this);
    var isChecked = clickedInput.is(':checked');
    var recordType = clickedInput.data('record-type');

    jQuery('.record-row-checkbox[data-record-type="' + recordType + '"]').prop('checked', isChecked);
}

/**
 * A handler for looking at the parent row's "select all" box, marking it checked if all sibling rows are checked,
 * or "partially checked" if only some of them are.
 */
function selectRowCheckboxHandler() {
    var clickedInput = jQuery(this);
    var recordType = clickedInput.data('record-type');
    var allCheckboxesForRecordType = jQuery('.record-row-checkbox[data-record-type="' + recordType + '"]');

    var oneChecked = false;
    var oneUnchecked = false;

    // Look at each sibling checkbox in this group to see what is checked / unchecked.
    jQuery.each(allCheckboxesForRecordType, function(key, item) {
        var isItemChecked = jQuery(item).is(':checked');

        if (!oneChecked && isItemChecked === true) {
            oneChecked = true;
        }

        if (!oneUnchecked && isItemChecked === false) {
            oneUnchecked = true;
        }
    });

    var rowParentCheckbox = jQuery('.select-all-records[data-record-type="' + recordType + '"]');

    // Propagate events to the parent row checkbox which shares this record type.
    if (oneChecked && !oneUnchecked) {
        // All checked.
        rowParentCheckbox.prop('indeterminate', false);
        rowParentCheckbox.prop('checked', true);
    } else if (!oneChecked && oneUnchecked) {
        // All unchecked.
        rowParentCheckbox.prop('indeterminate', false);
        rowParentCheckbox.prop('checked', false);
    } else {
        // Some checked.
        rowParentCheckbox.prop('checked', false);
        rowParentCheckbox.prop('indeterminate', true);
    }
}

/**
 * A keypress handler for showing and hiding rows in the table with +/- icons.
 * @param {object} e - The event to intercept clicks from.
 */
function rowShowHideHandler(e) {
    e.preventDefault();

    var target = $(this).data('target');

    $('[id="' + target + '"]').toggle();

    var childIcon = $(this).children('i');

    if (childIcon.hasClass(PLUS_CIRCLE_CLASS)) {
        childIcon.removeClass(PLUS_CIRCLE_CLASS);
        childIcon.addClass(MINUS_CIRCLE_CLASS);
    } else {
        childIcon.addClass(PLUS_CIRCLE_CLASS);
        childIcon.removeClass(MINUS_CIRCLE_CLASS);
    }
}

jQuery(function() {
    // Add a listener for click events on inputs with the name group of "import-type".
    jQuery('input[name=import-type]').on('click', importRadioButtonHandler);

    // Add a listener for click events on inputs with the name group of "export-type".
    jQuery('input[name=export-type]').on('click', exportRadioButtonHandler);

    // Add a listener for key press events on the tag name.
    jQuery('.m-tag-name').on('keyup', exportEnterKeyupHandler);

    // Add a listener for key press events on the namespace prefix.
    jQuery('.m-namespace-prefix').on('keyup', exportEnterKeyupHandler);

    // Add a listener for changes to the ConflictsFilter select list.
    jQuery("[id$='ConflictsFilter']").on('change', filterByConflictTypeHandler);

    // Add a listener for the select all button checkbox.
    jQuery('.select-all-records').on('click', selectAllCheckboxHandler);

    // Add a listener for each row's select checkbox.
    jQuery('.record-row-checkbox').on('click', selectRowCheckboxHandler);

    // Plus minus on record list.
    jQuery('.m-show-hide-link').on('click', rowShowHideHandler);
});