/* eslint-disable react/no-danger */
import React, { useState, useRef, useContext } from 'react';
import PropTypes from 'prop-types';
import {
  translate,
  imagePrettyUrl,
  getAltText,
  textWordsLimit,
} from 'gatsby-core-theme/src/helpers/getters';
import LazyImage from 'gatsby-core-theme/src/hooks/lazy-image';
import { Context } from 'gatsby-core-theme/src/context/MainProvider';
import StarRating from 'gatsby-core-theme/src/components/molecules/star-rating/one-star';
import { FaStar } from '@react-icons/all-files/fa/FaStar';
import OperatorBanner from '../../../../../atoms/header-operator-bannner';

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

const TemplateTwo = ({
  relation,
  name,
  image,
  sections = {},
  pageTemplate,
  icon,
  description,
  numOfStars,
}) => {
  const [isReadMore, setIsReadMore] = useState(true);
  const descriptionRef = useRef(null);
  const { translations } = useContext(Context) || {};
  const headerBonus = sections?.header?.modules?.find((bonus) => bonus?.name === 'bonus');

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

  return (
    <div className={styles.background}>
      <div className={styles.container}>
        <div className={styles.topSection}>
          <LazyImage
            loading="eager"
            src={imagePrettyUrl(image?.filename, 288, 288)}
            width={288}
            height={288}
            alt={getAltText(image, relation?.name)}
          />
          <div className={styles.topheader}>
            <div className={styles.name}>
              <h1>{name}</h1>
              <LazyImage
                src={icon || '../../../../../../images/verify.svg'}
                loading="eager"
                alt="Verified"
                width={24}
                height={24}
              />
            </div>
            {!['coming_soon', 'inactive'].includes(relation?.status) ? (
              <StarRating
                numOfStars={numOfStars}
                rating={relation?.rating}
                icon={<FaStar title="Star Icon" />}
              />
            ) : (
              <div className={styles.ratingInactive}>
                <FaStar title="Star Icon" />
                {translate(translations, 'tba', 'TBA')}
              </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,
              }}
            />
          )}
        </div>
        {headerBonus && (
          <OperatorBanner
            logo={
              headerBonus?.value?.standardised_logo_url_object ||
              headerBonus?.value?.logo_url_object
            }
            operator={headerBonus?.value}
            template={pageTemplate}
          />
        )}
      </div>
    </div>
  );
};

TemplateTwo.propTypes = {
  relation: PropTypes.shape({
    type: PropTypes.string,
    template: PropTypes.string,
    name: PropTypes.string,
    rating: PropTypes.string,
    status: PropTypes.string,
  }),
  name: PropTypes.string,
  image: PropTypes.shape({
    filename: PropTypes.string,
  }),
  sections: PropTypes.shape({}),
  pageTemplate: PropTypes.string,
  numOfStars: PropTypes.number,
  description: PropTypes.string,
  icon: PropTypes.node,
};
export default TemplateTwo;
