import React, { useContext } from 'react';
import PropTypes from 'prop-types';
// eslint-disable-next-line import/no-extraneous-dependencies
import loadable from '@loadable/component';
import { translate, imagePrettyUrl, getAltText } 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 Rating from '../../../../../atoms/ratings';
import OperatorBanner from '../../../../../atoms/header-operator-bannner';
import styles from './template-four.module.scss';
import PrettyLink from '~atoms/pretty-link';

const TemplateFour = ({
  relation,
  type,
  image,
  name,
  ribbons,
  pageTemplate,
  icon,
  numOfStars = 5,
  hasVariableComponent = false,
}) => {
  const { translations } = useContext(Context) || {};
  const ribbon = ribbons?.length > 0 ? ribbons[0] : null;

  const VariableComponent = hasVariableComponent ? loadable(() => import('../variables')) : null;

  const logo = () => (
    <PrettyLink
      operator={relation}
      pageTemplate={pageTemplate}
      module="header_operator"
      tracker="main"
      clickedElement="logo"
      className="header-gtm logo-cta "
    >
      <LazyImage
        loading="eager"
        src={imagePrettyUrl(image?.filename, 300, 300)}
        width={300}
        height={300}
        alt={getAltText(image, relation?.name)}
      />
    </PrettyLink>
  );

  return (
    <div
      className={`${styles.container} ${hasVariableComponent ? styles.hasVariableComponent : ''}`}
    >
      <div className={styles.topSection}>
        {logo()}

        {!['coming_soon', 'inactive'].includes(relation?.status) ? (
          <StarRating
            numOfStars={numOfStars}
            rating={numOfStars < 6 ? relation?.rating : Math.round(relation?.rating * 2)}
            icon={<FaStar title="Star Icon" />}
            showDot={false}
          />
        ) : (
          <div className={styles.ratingInactive}>
            <FaStar title="Star Icon" />
            {translate(translations, 'tba', 'TBA')}
          </div>
        )}
      </div>
      <div className={styles.bottomSection}>
        <div className={styles.name}>
          <h1>{name}</h1>
          <LazyImage
            src={icon || '../../../../../../images/verify.svg'}
            loading="eager"
            alt="Verified"
            width={24}
            height={24}
          />
        </div>
        {ribbon && <p className={styles.ribbon}>{ribbon}</p>}
        <div className={styles.ratings}>
          <Rating item={relation} type={type} numOfStars={numOfStars} />
        </div>
        <OperatorBanner logo={image} operator={relation} template={pageTemplate} />

        {VariableComponent && (
          <VariableComponent
            relation={relation}
            pageTemplate={pageTemplate}
            className={styles.variable}
          />
        )}
      </div>
    </div>
  );
};

TemplateFour.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,
  }),
  ribbons: PropTypes.PropTypes.arrayOf(PropTypes.shape({})),
  pageTemplate: PropTypes.string,
  hasVariableComponent: PropTypes.bool,
  numOfStars: PropTypes.number,
  icon: PropTypes.node,
};

export default TemplateFour;
