/* eslint-disable import/no-extraneous-dependencies */
import React from 'react';
import PropTypes from 'prop-types';
import { imagePrettyUrl } from 'gatsby-core-theme/src/helpers/getters';
import Link from 'gatsby-core-theme/src/hooks/link';
import BonusTemplateDefault from 'gatsby-core-theme/src/components/molecules/bonus-box/template-three';
import LazyImage from 'gatsby-core-theme/src/hooks/lazy-image';
import ReviewLink from 'gatsby-core-theme/src/components/atoms/review-link';
import Tnc from 'gatsby-core-theme/src/components/molecules/tnc';
import VariableComponent from '../variables';
import OperatorDetails from '../../../../molecules/operator-details';
import styles from './template-two.module.scss';

const TemplateTwo = ({
  showRibbon = false,
  item,
  useOneStarRating = true,
  page,
  tncCollapse = false,
  tncFixed = false,
  showTnc = false,
  showPaymentMethods = true,
  showAuthor = true,
  showRatingLabel = false,
  showRatingDecimal = false,
  authorTemplateTwo = true,
  contentRibbon = false,
  hasVariableComponent = false,
  verifyIcon = '../../../images/verify.svg',
  showBonusForAll = false,
  BonusTemplate = BonusTemplateDefault,
}) => {
  const ribbons = item?.relation?.ribbons || [];
  const { relation, path } = item;
  const { name, status, bonus } = relation || {};
  const paymentMethods = bonus?.deposit_methods;
  const hasTnc = showTnc && relation?.extra_fields?.terms_and_conditions_text_enabled;
  const reviewPath = path || relation.path;
  const isInactive = status === 'inactive';

  return (
    <div
      className={`${styles?.operatorCard || ''} ${
        ribbons?.length > 0 ? styles?.withRibbon || '' : ''
      }`}
    >
      <OperatorDetails
        item={item}
        showReviewLink={false}
        costumeStyle={styles.operatorDetails}
        imageWidth={88}
        imageHeight={88}
        ribbons={ribbons}
        showRibbon={showRibbon}
        contentRibbon={contentRibbon}
        icon={verifyIcon}
        template={page?.template}
        showAuthor={showAuthor}
        authorTemplateTwo={authorTemplateTwo}
        useOneStarRating={useOneStarRating}
        showRatingLabel={showRatingLabel}
        showRatingDecimal={showRatingDecimal}
        module="cards"
      />

      <div className={styles?.cardContent || ''}>
        {showBonusForAll ? (
          <BonusTemplate
            module="cards"
            welcomeBonus={false}
            ctaBtn
            item={relation}
            tracker="main"
          />
        ) : (
          !isInactive && (
            <BonusTemplate
              module="cards"
              welcomeBonus={false}
              ctaBtn
              item={relation}
              tracker="main"
            />
          )
        )}

        {showPaymentMethods && !!paymentMethods?.length && (
          <ul
            className={`${
              paymentMethods?.length > 4
                ? `${styles?.paymentMethods || ''} ${styles?.showMore || ''}`
                : styles?.paymentMethods || ''
            }`}
          >
            {paymentMethods?.slice(0, 4).map((deposit) => {
              const logo =
                deposit?.standardised_logo_filename_object?.filename ||
                deposit?.standardised_logo_filename ||
                deposit?.logo_filename;
              if (!logo) return;
              return (
                <li>
                  <Link to={deposit?.path}>
                    <LazyImage
                      key={`${deposit.id}-${deposit.name}`}
                      src={imagePrettyUrl(logo, 54, 36)}
                      width={54}
                      height={36}
                      alt={deposit.name}
                    />
                  </Link>
                </li>
              );
            })}
            {paymentMethods?.length > 4 && <li>{`+${paymentMethods?.length - 4}`}</li>}
          </ul>
        )}
        <ReviewLink
          className={`${styles.reviewLink} operator-card-gtm`}
          template={page?.template}
          operatorName={name}
          reviewPath={reviewPath}
        />
        {hasTnc === '1' && (
          <div
            className={`${styles.tnc || ''} ${
              tncCollapse && styles.tncCollapse ? styles.tncCollapse || '' : ''
            } ${tncFixed && styles.tncFixed ? styles.tncFixed || '' : ''}`}
          >
            <Tnc hasCollapse={tncCollapse} operator={relation} isFixed={tncFixed} />
          </div>
        )}
      </div>

      {hasVariableComponent && (
        <VariableComponent item={item} page={page} featured={false} template="templateTwo" />
      )}
    </div>
  );
};

TemplateTwo.propTypes = {
  item: PropTypes.shape({
    relation: PropTypes.shape({
      ribbons: PropTypes.shape([]),
      bonus: PropTypes.shape({
        deposit_methods: PropTypes.shape([]),
      }),
      rating: PropTypes.string,
      name: PropTypes.string,
      path: PropTypes.string,
      extra_fields: PropTypes.shape({
        terms_and_conditions_text_enabled: PropTypes.string,
      }),
      status: PropTypes.string,
      standardised_logo_url_object: PropTypes.shape({
        filename: PropTypes.string,
      }),
    }),
    path: PropTypes.string,
    author: PropTypes.shape({
      name: PropTypes.string,
      image: PropTypes.string,
    }),
  }).isRequired,
  page: PropTypes.shape({
    template: PropTypes.string,
  }),
  authorTemplateTwo: PropTypes.bool,
  showTnc: PropTypes.bool,
  tncCollapse: PropTypes.bool,
  tncFixed: PropTypes.bool,
  showPaymentMethods: PropTypes.bool,
  showAuthor: PropTypes.bool,
  verifyIcon: PropTypes.string,
  showRibbon: PropTypes.bool,
  useOneStarRating: PropTypes.bool,
  showRatingLabel: PropTypes.bool,
  showRatingDecimal: PropTypes.bool,
  hasVariableComponent: PropTypes.bool,
  contentRibbon: PropTypes.bool,
  showBonusForAll: PropTypes.bool,
  BonusTemplate: PropTypes.string,
};

export default TemplateTwo;
