var Wick = Wick || {};
Wick.YourDetails = (function () {
    const reasonSelect = $('input[type="radio"][name="reasonCode"]'),
      wickesInstalled = $(".wickes-installed"),
      kitchenBlock = $(".kitchen-bathroom"),
      otherEnquiryBlock = $(".other-enquiries"),
      storeEnquiryBlock = $(".store-enquiry"),
      $subcategoryRadios = $('input[type="radio"][name="helpPurpose"], input[type="radio"][name="help-kitchen"]'),
      existingOrder = $(".ex-order"),
      bookDeliveryDate = $(".book-delivery-date"),
      guaranteeClaim = $(".guarantee-claim"),
      workmanshipGuaranteeClaim = $(".workmanship-guarantee-claim"),
      existingOrderKitchen = $(".ex-order-kitchen"),
      bookDeliveryDateKitchen = $(".book-delivery-date-kitchen"),
      guaranteeClaimKitchen = $(".guarantee-claim-kitchen"),
      contactChoise = $(".your-details__add"),
      placingOrder = $(".placing-order-on"),
      cancellingOrderOn = $(".cancelling-order-on"),
      availabilityInfo = $(".availability-info-on"),
      onlineOrder = $(".order-deliv-online"),
      quaranteeClaim = $(".product-guarantee-claim"),
      enquiryNotRelatedToOrder = $(".enquiry-not-related-to-order"),
      complaintRaise = $(".complaint-raise"),
      unresolvedComplaint = $(".unresolved-complaint"),
      attachmentsFile = $(".your-details__attachments-file"),
      notificationForAttachments = $(".your-details__attachments-notification"),
      titleField = $(".title-field"),
      isVisible = "d-block",
      isHidden = "d-none",
      $linkTo = $('.follow-link'),
      $dest = $('.your-details__wrap'),
      $warning = $('.notification_warning');

    $(document).ready(function () {
        reasonSelect.first().prop('checked', true).trigger('change');
        otherEnquiryBlock.addClass(isVisible);

        reasonSelect.on('change', function () {
            $('.radio-option-wrapper').removeClass('selected');
            $('.radio-expanded-content').slideUp();

            $subcategoryRadios.prop('checked', false);
            $subcategoryRadios.closest('.radio-option-wrapper').removeClass('selected');
            $('.sub-radio-expanded-content').slideUp();

            resetSubCategories();
            resetMainCategories();

            const $wrapper = $(this).closest('.radio-option-wrapper');
            $wrapper.addClass('selected');
            $wrapper.find('.radio-expanded-content').slideDown();

            const mainCategories = [wickesInstalled, kitchenBlock, otherEnquiryBlock, storeEnquiryBlock];

            const selectedValue = $(this).val();

            mainCategories.forEach(function (block) {
                block.removeClass(isVisible);
            });

            switch (selectedValue) {
                case 'wickes-installed':
                    wickesInstalled.addClass(isVisible);
                    break;
                case 'kitchen-bathroom':
                    kitchenBlock.addClass(isVisible);
                    break;
                case 'other-enquiries':
                    otherEnquiryBlock.addClass(isVisible);
                    break;
                case 'store-enquiry':
                    storeEnquiryBlock.addClass(isVisible);
                    break;
            }

            if (selectedValue === 'wickes-installed' || selectedValue === 'kitchen-bathroom') {
                titleField.addClass(isHidden);
                contactChoise.addClass(isHidden);
            } else if (selectedValue === 'other-enquiries') {
                titleField.removeClass(isHidden);
                contactChoise.removeClass(isHidden);
                contactChoise.find('.form-row__label').addClass('form-row__label_required');
            } else {
                titleField.removeClass(isHidden);
                contactChoise.removeClass(isHidden);
                contactChoise.find('.form-row__label').removeClass('form-row__label_required');
            }
        });

        $subcategoryRadios.on('change', function () {
            const $parentWrapper = $(this).closest('.radio-option-wrapper');
            const $siblings = $parentWrapper.siblings('.radio-option-wrapper');

            $siblings.removeClass('selected');
            $siblings.find('.sub-radio-expanded-content').slideUp();

            $parentWrapper.addClass('selected');
            $parentWrapper.find('.sub-radio-expanded-content').slideDown();

            attachmentsFile.addClass(isHidden);
            notificationForAttachments.addClass(isHidden);
            $warning.removeClass(isVisible);

            let selectedBlock;

            const subcategoryId = $(this).attr("id");
            switch (subcategoryId) {
                case '0_ex-order_':
                    selectedBlock = existingOrder;
                    break;
                case '1_book-delivery-date_':
                    selectedBlock = bookDeliveryDate;
                    break;
                case '2_guarantee-claim_':
                    selectedBlock = guaranteeClaim;
                    attachmentsFile.removeClass(isHidden);
                    notificationForAttachments.removeClass(isHidden);
                    break;
                case '3_workmanship-guarantee-claim_':
                    selectedBlock = workmanshipGuaranteeClaim;
                    attachmentsFile.removeClass(isHidden);
                    notificationForAttachments.removeClass(isHidden);
                    break;
                case '0_ex-order-kitchen_':
                    selectedBlock = existingOrderKitchen;
                    break;
                case '1_book-delivery-date-kitchen_':
                    selectedBlock = bookDeliveryDateKitchen;
                    break;
                case '2_guarantee-claim-kitchen_':
                    selectedBlock = guaranteeClaimKitchen;
                    attachmentsFile.removeClass(isHidden);
                    notificationForAttachments.removeClass(isHidden);
                    break;
                case '0_placing-order-on_':
                    selectedBlock = placingOrder;
                    break;
                case '1_cancelling-order-on_':
                    selectedBlock = cancellingOrderOn;
                    break;
                case '2_availability-info-on_':
                    selectedBlock = availabilityInfo;
                    break;
                case '3_online-order-on_':
                    selectedBlock = onlineOrder;
                    $warning.addClass(isVisible);
                    break;
                case '4_product-guarantee-claim_':
                    selectedBlock = quaranteeClaim;
                    attachmentsFile.removeClass(isHidden);
                    notificationForAttachments.removeClass(isHidden);
                    break;
                case '5_enquiry-not-related-to-order_':
                    selectedBlock = enquiryNotRelatedToOrder;
                    break;
                case '6_complaint-raise_':
                    selectedBlock = complaintRaise;
                    break;
                case '4_complaint-raise_':
                    selectedBlock = complaintRaise;
                    break;
                case '3_complaint-raise_':
                    selectedBlock = complaintRaise;
                    break;
                case '7_unresolved-complaint_':
                    selectedBlock = unresolvedComplaint;
                    break;
            }
            resetSubcategoryBlocks(selectedBlock);

            if (selectedBlock) {
                selectedBlock.addClass(isVisible);
            }
        });

        $('form.form-wrapper').on('submit', function (event) {
            event.preventDefault();
            $('.error-msg').css('display', 'block');
        });

        $linkTo.on('click', function (event) {
            event.preventDefault();
            let dn = $dest.offset().top - 10;
            $('html, body').animate({ scrollTop: dn }, 1000);
        });

        function resetMainCategories() {
            const mainCategories = [wickesInstalled, kitchenBlock, otherEnquiryBlock, storeEnquiryBlock];
            mainCategories.forEach(function (block) {
                block.removeClass(isVisible);
                block.find('input, textarea, select').val('');
                block.find('input[type="checkbox"], input[type="radio"]').prop('checked', false);
            });
        }

        function resetSubCategories() {
            const subcategoryBlocks = [
                existingOrder, bookDeliveryDate, guaranteeClaim, workmanshipGuaranteeClaim,
                existingOrderKitchen, bookDeliveryDateKitchen, guaranteeClaimKitchen,
                placingOrder, cancellingOrderOn, availabilityInfo, onlineOrder,
                quaranteeClaim, enquiryNotRelatedToOrder, complaintRaise, unresolvedComplaint
            ];

            subcategoryBlocks.forEach(function (block) {
                block.find('input, textarea, select').val('');
                block.find('input[type="checkbox"], input[type="radio"]').prop('checked', false);
                block.removeClass(isVisible);
            });
        }

        function resetSubcategoryBlocks(excludeBlock) {
            const subcategoryBlocks = [
                existingOrder, bookDeliveryDate, guaranteeClaim, workmanshipGuaranteeClaim,
                existingOrderKitchen, bookDeliveryDateKitchen, guaranteeClaimKitchen,
                placingOrder, cancellingOrderOn, availabilityInfo, onlineOrder,
                quaranteeClaim, enquiryNotRelatedToOrder, complaintRaise, unresolvedComplaint
            ];

            subcategoryBlocks.forEach(function (block) {
                if (block !== excludeBlock) {
                    block.removeClass(isVisible);
                    block.find('input, textarea, select').val('');
                    block.find('input[type="checkbox"], input[type="radio"]').prop('checked', false);
                }
            });
        }
    });
})();
