import React, { lazy, Suspense } from 'react';
import PropTypes from 'prop-types';
import { imagePrettyUrl } from '~helpers/getters';
import { getAltText } from '~helpers/image';
import LazyImage from '~hooks/lazy-image';
import Rating from '~atoms/ratings';
import OperatorBanner from '~atoms/header-operator-bannner';
import { TrackingKeys } from '~constants/tracking-api'
import styles from './template-four.module.scss';

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

const TemplateFour = ({
  relation,
  type,
  image,
  name,
  sections = {},
  pageTemplate,
  icon = <Suspense fallback={<></>}><VerifyIcon /></Suspense>,
  numOfStars = 5,
  showRoundedRating = false,
  width = 300,
  height = 300,
  moduleName = TrackingKeys?.HEADERPAYMENT
}) => {
  const headerBonus = sections?.header?.modules?.find((bonus) => bonus?.name === 'bonus');

  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>
      <div className={styles.bottomSection}>
        <div className={styles.name}>
          <h1>{name}</h1>
          {icon}
        </div>
        <div className={styles.ratings}>
          <Rating
            item={relation}
            type={type}
            numOfStars={numOfStars}
            showRoundedRating={showRoundedRating}
          />
        </div>
        {headerBonus && (
          <OperatorBanner
            logo={headerBonus?.value?.logo}
            operator={headerBonus?.value}
            template={pageTemplate}
            moduleName={moduleName}
            clickedElement="header_operator_banner"
          />
        )}
      </div>
    </div>
  );
};

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

export default TemplateFour;
