import AJS from 'aui';
import $ from 'jquery';

function initMergeStrategies() {
    const $mergeStrategies = $('.merge-strategies');
    const mergeStrategy = '.merge-strategy';
    const defaultMergeStrategy = '.default-merge-strategy';
    const defaultMergeChecked = 'default-merge-checked';
    const defaultMergeStrategyLabel = '.default-merge-strategy-label';
    const mergeSettingsError = 'merge-settings-error';

    $mergeStrategies.on('change', mergeStrategy, evt => {
        const $defaultRadio = $(evt.target).siblings(defaultMergeStrategy);

        // toggle disable attr if the strategy is/not checked
        $defaultRadio.attr('disabled', !evt.target.checked);

        // uncheck the default radio if the strategy is unselected
        if (!evt.target.checked) {
            $defaultRadio.prop('checked', false);
        }

        const $checkedStrategies = $mergeStrategies.find(mergeStrategy + ':checked');
        // if there's just one checked strategy, set it as the default
        if ($checkedStrategies.length === 1) {
            $checkedStrategies.siblings(defaultMergeStrategy).click();
        }
    });
    $mergeStrategies.on('change', defaultMergeStrategy, evt => {
        // reset all default labels
        $mergeStrategies
            .find(defaultMergeStrategyLabel)
            .removeClass(defaultMergeChecked)
            .text(AJS.I18n.getText('bitbucket.web.repository.settings.pullrequest.mergestrategy.set.default'));

        // update this label as the default
        $(evt.target)
            .next(defaultMergeStrategyLabel)
            .addClass(defaultMergeChecked)
            .text(`(${AJS.I18n.getText('bitbucket.web.repository.settings.pullrequest.mergestrategy.default')})`);
    });

    function printErrorMsg($errorsContainer, test){
        if (!$errorsContainer.find('#' + test.id).length) {
            $errorsContainer.append(`<p id="${test.id}" class="${mergeSettingsError}">${test.message}</p>`);
        }
        return false;
    }

    function removeErrorMsg($errorsContainer, test){
        $errorsContainer.find('#' + test.id).remove();
        return true;
    }

    function validate($errorsContainer, test){
        return (test.condition) ? removeErrorMsg($errorsContainer, test) : printErrorMsg($errorsContainer, test);
    }

    $('form.pull-request-settings').on('submit', evt => {
        const $errorsContainer = $('#merge-strategy-errors');
        const testStrategy = {
            id: 'enabledUnset',
            message: AJS.I18n.getText('bitbucket.web.repository.settings.pullrequest.mergestrategy.invalid.selection'),
            condition: $mergeStrategies.find(mergeStrategy + ':checked').length
        };
        const testDefault = {
            id: 'defaultUnset',
            message: AJS.I18n.getText('bitbucket.web.repository.settings.pullrequest.mergestrategy.invalid.default'),
            condition: $mergeStrategies.find(defaultMergeStrategy + ':checked').length
        };

        $errorsContainer.empty();

        const valid = [testStrategy, testDefault].every(test => {
            return validate($errorsContainer, test);
        });

        if (!valid) {
            evt.preventDefault();
        }
    });
}

function onReady({ showMergeStrategies }) {
    if (showMergeStrategies) {
        initMergeStrategies();
    }
}

export default {
    onReady: onReady
};
