var Wick = Wick || {};
Wick.ClampContent = {
  CLAMP_BUTTON_SELECTOR: '.clamp_btn.clamp_btn-new',
  CLAMP_BUTTON_READ_MORE_TEXT: 'Read more',
  CLAMP_BUTTON_READ_LESS_TEXT: 'Read less',
  CLAMPED_CLASS: 'clamped-text',
  CLAMP_BUTTON_HTML: '<a class="clamp_btn clamp_btn-new" href="#">Read more</a>',
  MAX_LINES: 3,
  clampContentClass: 'clamp-content',
  clampContentSafariClass: 'clamp-content--safari',
  clampContentSelector: '.clamp-content',
  clampElementsList: [
    '.page-header__intro',
    '.srp-injected__content'
  ],
  calculateLineCount(element) {
    const lineHeight = parseFloat(element.css('line-height'));
    return Math.round(element[0].scrollHeight / lineHeight);
  },
  updateClamping(el) {
    const arrayElement = $(el);
    const linesQuantity = Wick.ClampContent.calculateLineCount(arrayElement);
    const buttonNextToEl = arrayElement.next(Wick.ClampContent.CLAMP_BUTTON_SELECTOR);

    const isExpanded = buttonNextToEl.text() === Wick.ClampContent.CLAMP_BUTTON_READ_LESS_TEXT;

    const shouldElementClamp = linesQuantity > Wick.ClampContent.MAX_LINES;

    if (isExpanded && shouldElementClamp) {
      return;
    }

    Wick.ClampContent.updateClampedClass(arrayElement, shouldElementClamp);
    Wick.ClampContent.updateButtonState(arrayElement, shouldElementClamp);
  },
  updateClampedClass(element, shouldClamp) {
    element.toggleClass(Wick.ClampContent.CLAMPED_CLASS, shouldClamp);
  },
  updateButtonState(element, shouldClamp) {
    const buttonNextToEl = element.next(Wick.ClampContent.CLAMP_BUTTON_SELECTOR);
    buttonNextToEl.remove();

    if (shouldClamp) {
      element.after(Wick.ClampContent.CLAMP_BUTTON_HTML);
    }
  },
  executeClamping() {
    const $clampContentWrapper = $(Wick.ClampContent.clampContentSelector);
    const arrayClampContent = $clampContentWrapper.toArray();

    $.each(arrayClampContent, function (_, el) {
      Wick.ClampContent.updateClamping(el);
    });
  },
  bindEvent() {
    $(document).on('click', Wick.ClampContent.CLAMP_BUTTON_SELECTOR, function (e) {
      e.preventDefault();
      const toggleButton = $(this);
      const relatedContent = toggleButton.prev('.clamp-content');

      relatedContent.toggleClass(Wick.ClampContent.CLAMPED_CLASS);

      toggleButton.text(
        relatedContent.hasClass(Wick.ClampContent.CLAMPED_CLASS)
          ? Wick.ClampContent.CLAMP_BUTTON_READ_MORE_TEXT
          : Wick.ClampContent.CLAMP_BUTTON_READ_LESS_TEXT
      );
    });
  },
  isSafari() {
    return /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
  },
  getElementsForClamping() {
    Wick.ClampContent.clampElementsList.forEach((el) => {
      $(el).addClass(Wick.ClampContent.clampContentClass);

      if (Wick.ClampContent.isSafari()) {
        $(el).addClass(Wick.ClampContent.clampContentSafariClass);
      }
    })
  },
  init() {
    Wick.ClampContent.getElementsForClamping();
    $(Wick.ClampContent.clampContentSelector).length && Wick.ClampContent.executeClamping();
    Wick.ClampContent.bindEvent();
  }
}

$(window).on('resize', _.debounce(function () {
  $(Wick.ClampContent.clampContentSelector).length && Wick.ClampContent.executeClamping();
}, 250));

$(document).ready(function () {
  Wick.ClampContent.init();
});
