/* eslint-disable react/no-danger */
import React, { useState, useRef, useContext } from 'react';
import PropTypes from 'prop-types';
import {
  imagePrettyUrl,
  getAltText,
  translate,
  textWordsLimit,
} from 'gatsby-core-theme/src/helpers/getters';
import Tnc from 'gatsby-core-theme/src/components/molecules/tnc';
import LazyImage from 'gatsby-core-theme/src/hooks/lazy-image';
import { Context } from 'gatsby-core-theme/src/context/MainProvider';
import Rating from '../../../../../atoms/ratings';
import OperatorBanner from '../../../../../atoms/header-operator-bannner';

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

const TemplateOneThree = ({
  relation,
  type,
  name,
  image,
  sections = {},
  pageTemplate,
  icon,
  description,
  numOfStars,
  CostumeComponent = null,
}) => {
  const [isReadMore, setIsReadMore] = useState(true);
  const { translations } = useContext(Context) || {};
  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, 179, 179)}
          width={179}
          height={179}
          alt={getAltText(image, relation?.name)}
        />
        <div className={`${styles.topContent} `}>
          <div className={styles.name}>
            <h1>{name}</h1>
            <LazyImage
              src={icon || '../../../../../../images/verify.svg'}
              loading="eager"
              alt="Verified"
              width={24}
              height={24}
            />
          </div>

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

          <Rating item={relation} type={type} numOfStars={numOfStars} />
        </div>
      </div>
      {description && (
        <div
          ref={descriptionRef}
          className={`${styles.desc} ${description.split(' ').length > 60 && styles.showReadMore}`}
          role="button"
          onClick={toggleReadMore}
          onKeyDown={toggleReadMore}
          aria-label="continue reading"
          tabIndex={0}
          dangerouslySetInnerHTML={{
            __html:
              description.split(' ').length > 60 && isReadMore
                ? `${textWordsLimit(description, 60)} <span class='${
                    styles.contReadText
                  }'>${translate(translations, 'cont_read', 'Continue reading')}</span>`
                : description,
          }}
        />
      )}
      {headerBonus && (
        <div className={styles.templateFiveCta}>
          <OperatorBanner
            logo={
              headerBonus?.value?.standardised_logo_url_object ||
              headerBonus?.value?.logo_url_object
            }
            operator={headerBonus?.value}
            template={pageTemplate}
          />
        </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.string,
    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.node,
  CostumeComponent: PropTypes.node,
};

export default TemplateOneThree;
