var Wick = Wick || {};

Wick.PreHomeForm = function () {

  const preHomeVar = {
    $preHomePage: $('.pre-home-page'),
    $formItemCheck: $('.option-check__list-item .option-check__head'),
    $formItemCheckInput: $('.chb input'),
    $additionalForm: $('.option-check__content'),
    $submitBtn: $('.feedback-form__submit'),
    $feedbackText: $('.feedback-form__textarea textarea'),
    $mainForm: $('.main-form__content'),
    $mainFormContent: $('.main-form__content-form'),
    $mainFormError: $('.main-form__error'),
    $simpleCheckList: $('simple-check__list'),
    $simpleCheckListItem: $('simple-check__list-item input'),
    $mainFormHowID: $('#mainFormHow'),
    $mainFormStyleID: $('#mainFormStyle'),
    $mainFormAppliancesID: $('#mainFormAppliances'),
    $mainFormCompleteID: $('#mainFormComplete'),
    $mainFormCompleteBathroomID: $('#mainFormCompleteBathroom'),
    $mainFormBestStatementID: $('#mainFormBestStatement'),
    $mainFormCompleteID: $('#mainFormComplete'),
    $feedbackFormTextareaError: $('.feedback-form__textarea-error'),
    $iconCheckListIcon: $('.icon-check__list-icon')
    };

  function init() {
    preHomeVar.$formItemCheck.on('change', function () {
      if ($(this).find(preHomeVar.$formItemCheckInput).prop('checked')) {
        $(this).parent().find(preHomeVar.$additionalForm).slideDown();
        $(this).parent().find(preHomeVar.$additionalForm).addClass('d-block');
      } else {
        $(this).parent().find(preHomeVar.$additionalForm).find(preHomeVar.$formItemCheckInput).each(function() {
            $(this).prop('checked', false)
        });
        $(this).parent().find(preHomeVar.$additionalForm).slideUp();
        $(this).parent().find(preHomeVar.$additionalForm).removeClass('d-block');
      }
    })

    function scrollToError() {
      let arrError = preHomeVar.$preHomePage.find(preHomeVar.$mainFormError).filter('.d-block'),
          parentElError = arrError.first().closest(preHomeVar.$mainForm);

        $('html, body').animate({
            scrollTop: parentElError.offset().top - 100
        }, 1000);

    }

    function checkTextValidation() {
      let $feedbackTextValue = preHomeVar.$feedbackText.val(),
          $feedbackTextLength = $feedbackTextValue.length;

      if($feedbackTextLength > 800) {
        Wick.Notification.show({
          text: 'You cannot enter more than 800 characters',
          container: $('.feedback-form__textarea-error'),
          type: 'warning',
          noScrolling: true
        });
        preHomeVar.$feedbackFormTextareaError.addClass('d-block');
        return false;
      }
        else {
          preHomeVar.$feedbackFormTextareaError.removeClass('d-block');
          return true;
      }
    }

    function simpleCheckStep (elID) {
      let count = 0;
      let arrEL = elID.find('input');

      for(var i=0; i < arrEL.length; i++) {
       if(arrEL[i].checked === true) {
        count +=1
       }
      }

      if (count > 0) {
        elID.parent().find(preHomeVar.$mainFormError).removeClass('d-block');
        return true;

      } else {
        elID.parent().find(preHomeVar.$mainFormError).addClass('d-block');
        return false;
      }
    }

    function iconCheckStep ( elID, elBlock ) {
      let arrList = elID.find(elBlock),
          isError = false;

        arrList.each(function() {
          let arrListInput =  $(this).find('input');
          let count = 0;

          for(var i=0; i < arrListInput.length; i++) {
            if(arrListInput[i].checked === true) {
             count +=1;
            }

            if (count > 0) {
              $(this).find(preHomeVar.$mainFormError).removeClass('d-block');
              isError = false;
      
            } else {
              $(this).find(preHomeVar.$mainFormError).addClass('d-block');
              isError = true;
            }
          }

        });
        return !isError;
    }

    function  redirectToThankYouPage() {
      window.location.href = './page_thank_you_kitchen-submit.html';
    }

    preHomeVar.$submitBtn.on('click', function () {
      let $checkTextValidation = checkTextValidation(),
          $simpleCheckStepHowID = simpleCheckStep(preHomeVar.$mainFormHowID),
          $simpleCheckStepStyleID  = simpleCheckStep(preHomeVar.$mainFormStyleID),
          $simpleCheckStepBestStatementID  = simpleCheckStep(preHomeVar.$mainFormBestStatementID),
          $iconCheckStepAppliancesID  = iconCheckStep (preHomeVar.$mainFormAppliancesID, preHomeVar.$additionalForm.filter('.d-block'));

      if(!($checkTextValidation && $simpleCheckStepHowID && $simpleCheckStepStyleID && $simpleCheckStepBestStatementID && $iconCheckStepAppliancesID)) {
        Wick.Notification.show({
          text: 'Sorry, there are some options in the form above that require your input',
          container: $('.submit-error'),
          type: 'error',
          noScrolling: true
        })
      scrollToError();
      } else {
        redirectToThankYouPage();
      }
    })

    preHomeVar.$mainFormContent.find('input[type="checkbox"]').on('click', function () {
      if ($(this).attr('checked') === 'checked') {
        $(this).removeAttr('checked')
      } else {
        $(this).attr('checked', 'checked');
      }
    });

    preHomeVar.$mainFormContent.find('input[type="radio"]').on('click', function () {
      let radio = preHomeVar.$mainFormContent.find('input[type="radio"]');
      $(this).attr('checked', 'checked');
      radio.not($(this)).removeAttr('checked');
    });
  }

  if (!preHomeVar.$preHomePage.length) {
    return;
  }

  init();
}();
