/* 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 { formatDate } from 'gatsby-core-theme/src/helpers/date-time';
import LazyImage from 'gatsby-core-theme/src/hooks/lazy-image';
import Ratings from 'gatsby-core-theme/src/components/molecules/star-rating/one-star';
import OperatorBanner from 'gatsby-core-theme/src/components/atoms/header-operator-bannner';
import useTranslate from '~hooks/useTranslate/useTranslate';
import getOperatorForSummary from '../../../../helpers/operator';
import Author from '../../../atoms/author/template-one';
import { TrackingKeys } from '~constants/tracking-api'

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

const TemplateOne = ({
  page,
  imgWidth = 128,
  imgHeight = 146,
  operatorImg = true,
  extraField = null,
  showUpdatedDate = true,
  showExperience = false,
  authorImgWidth = 24,
  authorImgHeight = 24,
  moduleName =  TrackingKeys?.OPERATORSUMMARY,
  modulePosition
}) => {
  const operatorDetails = getOperatorForSummary(page);

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

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

  const { logo, name } = operatorData;
  const author = page.author_id ? page?.author : null;
  const img = operatorImg ? logo?.filename : author?.image;
  const title = generatePlaceholderString(
    useTranslate('expert_casino_review_title', 'Expert Casino Review'),
    {},
    operatorData
  );

  return (
    <div className={styles?.summaryLayout || ''}>
      <div className={styles.details}>
        <h2>{title}</h2>
        <div className={styles.img}>
          <LazyImage
            src={imagePrettyUrl(img, imgWidth, imgHeight)}
            width={imgWidth}
            height={imgHeight}
            alt={name}
          />
          {operatorImg && <Ratings rating={operatorData?.rating} />}
        </div>
        {author && (
          <div className={styles.author}>
            <LazyImage
              src={imagePrettyUrl(author?.image, authorImgWidth, authorImgHeight)}
              width={authorImgWidth}
              height={authorImgHeight}
              alt={author?.name}
            />
            <Author label={useTranslate('By', 'By')} authorName={author?.name} authorPath={author?.profile_page_path} />
            <span>
              {author?.pages_count} {useTranslate('reviews', 'Reviews')}
            </span>
            {showUpdatedDate && (
              <p>
                {useTranslate('updated_on', 'Updated on')} {formatDate(page.updated_at)}
              </p>
            )}
            {showExperience && author?.experience && (
              <div className={styles.experience}>{author?.experience}</div>
            )}
          </div>
        )}
        {operatorSummary && (
          <div
            className={styles.description}
            dangerouslySetInnerHTML={{ __html: operatorSummary }}
          />
        )}
      </div>
      <OperatorBanner
        logo={logo}
        operator={operatorData}
        template={page?.template}
        moduleName={moduleName}
        modulePosition={modulePosition}
      />
      {extraField && (
        <div className={styles.extraField} dangerouslySetInnerHTML={{ __html: extraField }} />
      )}
    </div>
  );
};
TemplateOne.propTypes = {
  moduleName:  PropTypes.string,
  imgWidth: PropTypes.string,
  imgHeight: PropTypes.string,
  extraField: PropTypes.string,
  showExperience: PropTypes.bool,
  showUpdatedDate: PropTypes.bool,
  operatorImg: PropTypes.bool,
  page: PropTypes.shape({
    author: PropTypes.shape({}),
    author_id: PropTypes.number,
    relation_type: PropTypes.string,
    extra_fields: PropTypes.shape({}),
    template: PropTypes.string,
    updated_at: PropTypes.string,
    relation: PropTypes.shape({
      name: PropTypes.string,
      logo_url: PropTypes.string,
    }),
  }),
  authorImgWidth: PropTypes.number,
  authorImgHeight: PropTypes.number,
  modulePosition: PropTypes.number,
};
export default TemplateOne;
