/* eslint-disable react/no-danger */
import React, { useState, useRef, lazy, Suspense } from 'react';
import PropTypes from 'prop-types';
import { imagePrettyUrl, textWordsLimit } from '~helpers/getters';
import { getAltText } from '~helpers/image';
import Tnc from '~molecules/tnc';
import LazyImage from '~hooks/lazy-image';
import OperatorBanner from '~atoms/header-operator-bannner';
import Rating from '~atoms/ratings';
import useTranslate from '~hooks/useTranslate/useTranslate';
import { TrackingKeys } from '~constants/tracking-api';

import styles from './template-five.module.scss';

const VerifyIcon = lazy(() => import('~images/icons/verify'));

const TemplateOneThree = ({
  relation,
  type,
  name,
  image,
  sections = {},
  pageTemplate,
  icon = <Suspense fallback={<></>}><VerifyIcon /></Suspense>,
  description,
  numOfStars,
  CostumeComponent = null,
  showRoundedRating = false,
  width = 179,
  height = 179,
  moduleName = TrackingKeys?.HEADERPAYMENT,
}) => {
  const [isReadMore, setIsReadMore] = useState(true);

  const descriptionRef = useRef(null);
  const headerBonus = sections?.header?.modules?.find((bonus) => bonus?.name === 'bonus');

  const toggleReadMore = () => {
    descriptionRef.current.classList.add(styles.open);
    setIsReadMore(false);
  };

  return (
    <div className={`${styles.container}`}>
      <div className={`${styles.topSection} `}>
        <LazyImage
          loading="eager"
          src={imagePrettyUrl(image?.filename, width, height)}
          width={width}
          height={height}
          alt={getAltText(image, relation?.name)}
        />
        <div className={`${styles.topContent} `}>
          <div className={styles.name}>
            <h1>{name}</h1>
            {icon}
          </div>

          {CostumeComponent && <div className={styles.costumeComponent}>{CostumeComponent}</div>}

          <Rating
            item={relation}
            type={type}
            numOfStars={numOfStars}
            showRoundedRating={showRoundedRating}
          />
        </div>
      </div>
      {description && (
        <div
          ref={descriptionRef}
          className={`${styles.desc} ${description.split(' ').length > 60 && styles.showReadMore}`}
          role="button"
          onClick={toggleReadMore}
          onKeyDown={toggleReadMore}
          aria-label={useTranslate('ariaLabel-continueReading', 'continue reading')}
          tabIndex={0}
          dangerouslySetInnerHTML={{
            __html:
              description.split(' ').length > 60 && isReadMore
                ? `${textWordsLimit(description, 60)} <span class='${
                    styles.contReadText
                  }'>${useTranslate('cont_read', 'Continue reading')}</span>`
                : description,
          }}
        />
      )}
      {headerBonus && (
        <div className={styles.templateFiveCta}>
          <OperatorBanner
            logo={headerBonus?.value?.logo}
            operator={headerBonus?.value}
            template={pageTemplate}
            clickedElement="header_operator_banner"
            moduleName={moduleName}
          />
        </div>
      )}
      <Tnc operator={headerBonus?.value} hasCollapse={false} isFixed />
    </div>
  );
};

TemplateOneThree.propTypes = {
  relation: PropTypes.shape({
    type: PropTypes.string,
    template: PropTypes.string,
    name: PropTypes.string,
    rating: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
    status: PropTypes.string,
  }),
  name: PropTypes.string,
  type: PropTypes.string,
  image: PropTypes.shape({
    filename: PropTypes.string,
  }),
  sections: PropTypes.shape({}),
  pageTemplate: PropTypes.string,
  numOfStars: PropTypes.number,
  description: PropTypes.string,
  icon: PropTypes.element,
  CostumeComponent: PropTypes.node,
  showRoundedRating: PropTypes.bool,
  width: PropTypes.number,
  height: PropTypes.number,
  moduleName: PropTypes.string,
};

export default TemplateOneThree;
