var Wick = Wick || {};
Wick.YourDetails = (function () {

    let reasonSelect = $("#reason"),
      helpRadioBlock = $('.your-details__help-wrap input'),
      attachmentsFile = $(".your-details__attachments-file"),
      notificationForAttachments = $(".your-details__attachments-notification"),
      isHidden = "d-none",
      tooltipMessage = "This is needed in order for us to help you with your query";

    function addTooltipToLabel (elementSelector, messageText) {
        if($(elementSelector).find('.form-row__label .icon.icon-info-component').length === 0){
            $(elementSelector).find('.form-row__label').append('<span class="icon icon-info-component">'+
                '<span class="fas fa-info-circle"></span><span class="far fa-info-circle"></span></span>');
            $(elementSelector).find('.form-row__label .icon.icon-info-component').tooltip({title: messageText, placement: "top"});
            }
    }

    function changeOrderFields(status) {
        console.log('changeOrderFields');
        const orderInput = $('#order');
        const orderLabel = orderInput.parent().siblings('.form-row__label');
        const orderWrapper = orderInput.closest('.form-row');

        const postcodeInput = $('#postcode');
        const postcodeLabel = postcodeInput.parent().siblings('.form-row__label');
        const postcodeWrapper = postcodeInput.closest('.form-row');
        if (status) {
            orderLabel.addClass('form-row__label_required');
            orderWrapper.attr('data-row-required', 'true');
            postcodeLabel.addClass('form-row__label_required');
            postcodeWrapper.attr('data-row-required', 'true');
            attachmentsFile.removeClass(isHidden);
            notificationForAttachments.removeClass(isHidden);
            addTooltipToLabel('.form-row.row-postcode', tooltipMessage);
            addTooltipToLabel('.form-row.row-ordernumber', tooltipMessage);
        } else {
            orderLabel.removeClass('form-row__label_required');
            orderWrapper.removeAttr('data-row-required');
            postcodeLabel.removeClass('form-row__label_required');
            postcodeWrapper.removeAttr('data-row-required');
            attachmentsFile.addClass(isHidden);
            notificationForAttachments.addClass(isHidden);
            $('.form-row.row-postcode .form-row__label .icon.icon-info-component').remove();
            $('.form-row.row-ordernumber .form-row__label .icon.icon-info-component').remove();
        }
    }

    function checkRadioButtonsStatus() {
        if ($('#reason :selected').attr('data-val') === 'wickes-installed') {
            const selectedRadioIDs = $('.wickes-installed input:checked').map(function() {
                return $(this).attr('id');
            }).get();

            if (selectedRadioIDs.includes('2_guarantee-claim_') || selectedRadioIDs.includes('3_workmanship-guarantee-claim_')) {
                changeOrderFields(true);
            } else {
                changeOrderFields(false);
            }
        } else if ($('#reason :selected').attr('data-val') === 'kitchen-bathroom') {
            const selectedRadioIDs = $('.kitchen-bathroom input:checked').map(function() {
                return $(this).attr('id');
            }).get();

            if (selectedRadioIDs.includes('2_guarantee-claim-kitchen_')) {
                changeOrderFields(true);
            } else {
                changeOrderFields(false);
            }
        } else {
            changeOrderFields(false);
        }
    }

    reasonSelect.on('change', function() {
        checkRadioButtonsStatus();
    });

    $(helpRadioBlock).change(function () {
        checkRadioButtonsStatus();
    });

})();
