/* eslint-disable react/no-danger */
/* eslint-disable camelcase */
import React from 'react';
import PropTypes from 'prop-types';
import { imagePrettyUrl } from 'gatsby-core-theme/src/helpers/getters';
import { generatePlaceholderString } from 'gatsby-core-theme/src/helpers/generators.mjs';
import Bonus from 'gatsby-core-theme/src/components/molecules/bonus/template-one';
import LazyImage from 'gatsby-core-theme/src/hooks/lazy-image';
import useTranslate from '~hooks/useTranslate/useTranslate';
import getOperatorForSummary from '../../../../helpers/operator';
import AuthorName from '../../../atoms/author/template-one';
import VerifiedAuthor from '../../../../images/icons/verified-author';
import { TrackingKeys } from '~constants/tracking-api'
import styles from './template-three.module.scss';

const TemplateThree = ({ page,
  imgWidth = 128,
  imgHeight = 146,
  verify = <VerifiedAuthor />,
  moduleName = TrackingKeys?.OPERATORSUMMARY,
  modulePosition }) => {
  const operatorDetails = getOperatorForSummary(page);

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

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

  const author = page.author_id ? page?.author : null;
  const title = generatePlaceholderString(
    useTranslate('expert_casino_review_title', 'Expert Casino Review [operator_name]'),
    {},
    operatorData
  );

  return (
    <div className={`${styles?.summaryLayout || ''} ${styles.tncContainer}`}>
      <div className={styles.details}>
        <h2>{title}</h2>

        {operatorSummary && (
          <div
            className={styles.description}
            dangerouslySetInnerHTML={{ __html: operatorSummary }}
          />
        )}
        {author && (
          <div className={styles.author}>
            <LazyImage
              src={imagePrettyUrl(author?.image, imgWidth, imgHeight)}
              width={imgWidth}
              height={imgHeight}
              alt={author?.name}
            />
            <div>
              <AuthorName verify={verify} authorName={author?.name} />
              <span>{author?.author_title}</span>
            </div>
          </div>
        )}
      </div>
      <Bonus
        operator={operatorData}
        template={page?.template}
        moduleName={moduleName}
        modulePosition={modulePosition}
      />
    </div>
  );
};

TemplateThree.propTypes = {
  moduleName:  PropTypes.string,
  imgWidth: PropTypes.string,
  imgHeight: PropTypes.string,
  verify: PropTypes.element,
  page: PropTypes.shape({
    relation_type: PropTypes.string,
    author: PropTypes.shape({}),
    author_id: PropTypes.number,
    extra_fields: PropTypes.shape({}),
    template: PropTypes.string,
    updated_at: PropTypes.string,
    relation: PropTypes.shape({
      name: PropTypes.string,
      logo_url: PropTypes.string,
    }),
  }),
  modulePosition: PropTypes.number,
};

export default TemplateThree;
