/* eslint-disable import/no-extraneous-dependencies */
import React from 'react';
import PropTypes from 'prop-types';
import loadable from '@loadable/component';
import { FaCheck } from '@react-icons/all-files/fa/FaCheck';
import { FaArrowRight } from '@react-icons/all-files/fa/FaArrowRight';
import { imagePrettyUrl } from 'gatsby-core-theme/src/helpers/getters';
import Link from 'gatsby-core-theme/src/hooks/link';
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 OperatorCta from 'gatsby-core-theme/src/components/atoms/button/operator-cta';
import SellingPoints from 'gatsby-core-theme/src/components/atoms/selling-points';
import OperatorDetails from '../../../../molecules/operator-details';
import styles from './template-one.module.scss';

const TemplateOne = ({
  showRibbon = false,
  item,
  useOneStarRating = true,
  page,
  tncCollapse = false,
  tncFixed = false,
  showTnc = true,
  showPaymentMethods = true,
  showSellingPoints = true,
  showAuthor = true,
  hasVariableComponent = false,
  showRatingLabel = false,
  showRatingDecimal = false,
  authorTemplateTwo = true,
  sellingPointsIcon = <FaCheck title="Check Icon" />,
  showBonusForAll = false,
  verifyIcon = '../../../images/verify.svg',
  showOperatorCta = true,
  BonusTemplate = loadable(() =>
    import(`gatsby-core-theme/src/components/molecules/bonus-box/template-one`)
  ),
}) => {
  const ribbons = item?.relation?.ribbons || [];
  const { relation, path } = item;
  const { name, status, selling_points: sellingPoints, 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';

  const VariableComponent = hasVariableComponent ? loadable(() => import('../variables')) : null;

  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}
        icon={verifyIcon}
        template={page?.template}
        showAuthor={showAuthor}
        authorTemplateTwo={authorTemplateTwo}
        useOneStarRating={useOneStarRating}
        showRatingLabel={showRatingLabel}
        showRatingDecimal={showRatingDecimal}
        module="cards"
      />

      <div className={styles?.cardContent || ''}>
        <div className={styles?.cardContentContainer || ''}>
          <ReviewLink
            className={`${styles?.reviewLink || ''} operator-card-gtm`}
            template={page?.template}
            operatorName={name}
            reviewPath={reviewPath}
          />
          {showSellingPoints && !!sellingPoints?.length && (
            <SellingPoints sellingPoints={sellingPoints} limit={3} icon={sellingPointsIcon} />
          )}
          {showBonusForAll ? (
            <BonusTemplate module="cards" item={relation} tracker="main" />
          ) : (
            !isInactive && <BonusTemplate item={relation} tracker="main" />
          )}
        </div>
        <div className={styles?.cardFooter || ''}>
          {VariableComponent && <VariableComponent item={item} page={page} featured={false} />}
          {relation?.status !== 'not_recommended' && showOperatorCta && (
            <OperatorCta
              tracker="main"
              module="cards_v2"
              operator={relation}
              buttonSize="m"
              translationsObj={{
                active: { translationKey: 'play_now', defaultValue: 'Play Now' },
                not_recommended: {
                  translationKey: 'not_recommended',
                  defaultValue: 'Not Recommended',
                },
                coming_soon: { translationKey: 'coming_soon', defaultValue: 'Soon Available' },
                inactive: {
                  translationKey: 'inactive',
                  defaultValue: 'Not Accepting New Players',
                },
                blacklisted: { translationKey: 'blacklisted', defaultValue: 'Blacklisted' },
              }}
              gtmClass="operator-card-gtm btn-cta"
              pageTemplate={page?.template}
              icon={<FaArrowRight title="Right-pointing Arrow Icon" />}
            />
          )}
          {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>
          )}
        </div>
        {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>
    </div>
  );
};

TemplateOne.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,
      }),
      selling_points: PropTypes.shape([]),
    }),
    path: PropTypes.string,
    author: PropTypes.shape({
      name: PropTypes.string,
      image: PropTypes.string,
    }),
  }).isRequired,
  page: PropTypes.shape({
    template: PropTypes.string,
  }),
  showTnc: PropTypes.bool,
  tncCollapse: PropTypes.bool,
  tncFixed: PropTypes.bool,
  authorTemplateTwo: PropTypes.bool,
  showPaymentMethods: PropTypes.bool,
  showSellingPoints: PropTypes.bool,
  showAuthor: PropTypes.bool,
  hasVariableComponent: PropTypes.bool,
  sellingPointsIcon: PropTypes.element,
  verifyIcon: PropTypes.string,
  showRibbon: PropTypes.bool,
  useOneStarRating: PropTypes.bool,
  showRatingLabel: PropTypes.bool,
  showBonusForAll: PropTypes.bool,
  showRatingDecimal: PropTypes.bool,
  BonusTemplate: PropTypes.string,
  showOperatorCta: PropTypes.bool,
};

export default TemplateOne;
