/* eslint-disable react/no-danger */
/* eslint-disable camelcase */
import React, { useContext } from 'react';
import PropTypes from 'prop-types';
import { imagePrettyUrl, translate } from 'gatsby-core-theme/src/helpers/getters';
import { generatePlaceholderString } from 'gatsby-core-theme/src/helpers/generators.mjs';
import { formatDate } from 'gatsby-core-theme/src/helpers/date-time';
import LazyImage from 'gatsby-core-theme/src/hooks/lazy-image';
import Tnc from 'gatsby-core-theme/src/components/molecules/tnc';
import { Context } from 'gatsby-core-theme/src/context/MainProvider';
import Ratings from 'gatsby-core-theme/src/components/molecules/star-rating/index';
import getOperatorForSummary from '../../../../helpers/operator';
import Author from '../../../atoms/author/template-one';
import OperatorBanner from '../../../atoms/header-operator-bannner';
import styles from './template-two.module.scss';

const TemplateTwo = ({
  page,
  imgWidth = 128,
  imgHeight = 146,
  tnc = false,
  operatorImg = true,
}) => {
  const { translations } = useContext(Context) || {};

  const operatorDetails = getOperatorForSummary(page);

  if (!operatorDetails) {
    return <></>;
  }

  const operatorData = operatorDetails[0];
  const operatorSummary = operatorDetails[1];

  const { name } = operatorData;
  const imageObject = operatorData?.standardised_logo_url_object || operatorData?.logo_url_object;
  const author = page.reviewer_id ? page?.reviewer : null;
  const img = operatorImg ? imageObject : author?.image;

  const title = generatePlaceholderString(
    translate(translations, 'expert_casino_review_title', 'Expert Casino Review'),
    translations,
    operatorData
  );

  return (
    <div className={styles?.summaryLayout || ''}>
      <div className={styles.details}>
        <div className={styles.img}>
          <LazyImage
            src={imagePrettyUrl(img?.filename, imgWidth, imgHeight)}
            width={imgWidth}
            height={imgHeight}
            alt={name}
          />
        </div>
        <div className={styles.title}>
          <h2>{title}</h2>
          <Ratings halfStars={false} rating={operatorData?.rating} numOfStars={5} />
          <p>
            {translate(translations, 'published_on', 'Published on')} {formatDate(page.updated_at)}
          </p>
        </div>
        {operatorSummary && (
          <div
            className={styles.description}
            dangerouslySetInnerHTML={{ __html: operatorSummary }}
          />
        )}

        {author && (
          <div className={styles.author}>
            <Author
              label="By"
              authorName={author?.name}
              verify={imagePrettyUrl(author?.image)}
              authorPath={author?.profile_page_path}
            />
            <span>
              {author?.pages_count} {translate(translations, 'reviews', 'Reviews')}
            </span>
          </div>
        )}
      </div>

      <OperatorBanner
        module="operator_summary"
        logo={imageObject}
        operator={operatorData}
        template={page?.template}
      />
      {tnc && <Tnc hasCollapse={false} />}
    </div>
  );
};

TemplateTwo.propTypes = {
  imgWidth: PropTypes.string,
  imgHeight: PropTypes.string,
  tnc: PropTypes.bool,
  operatorImg: PropTypes.bool,
  page: PropTypes.shape({
    relation_type: PropTypes.string,
    reviewer_id: PropTypes.number,
    reviewer: PropTypes.shape({}),
    extra_fields: PropTypes.shape({}),
    template: PropTypes.string,
    updated_at: PropTypes.string,
    relation: PropTypes.shape({
      name: PropTypes.string,
      logo_url: PropTypes.string,
    }),
  }),
};

export default TemplateTwo;
