/* eslint-disable camelcase */
import React, { useState, useContext } from 'react';
import PropTypes from 'prop-types';
import { FaArrowRight } from '@react-icons/all-files/fa/FaArrowRight';
import { IoMdClose } from '@react-icons/all-files/io/IoMdClose';
import { imagePrettyUrl, translate } from '~helpers/getters';
import LazyImage from '~hooks/lazy-image';
import isSticky from '~hooks/stickyOnScroll';
import BonusBox from '~molecules/bonus-box/template-four';
import OperatorCta from '~atoms/button/operator-cta';
import Rating from '~molecules/star-rating/one-star';
import Tnc from '~molecules/tnc';
import PrettyLink from '~atoms/pretty-link';
import { Context } from '~context/MainProvider';
import styles from './bonus.module.scss';

export default function TemplateOne({
  operator,
  stickyOffset = 0,
  pageTemplate,
  module = {
    name: 'bonus',
  },
  tncEnable = true,
  tncFixed = false,
  showOnLoad = false,
  showWelcomeText = true,
  floating = false,
}) {
  const [show, setShow] = useState(showOnLoad);
  const [closed, setClosed] = useState(false);
  const { translations } = useContext(Context) || {};
  const { standardised_logo_url, logo_url, bonus, name } = operator || {};
  const logo = standardised_logo_url || logo_url;

  const showTncFixed = tncEnable && tncFixed;

  const translationsObj = {
    active: {
      translationKey: translations?.play_now_banner ? 'play_now_banner' : 'play_now',
      defaultValue: 'Play',
    },
  };

  isSticky(stickyOffset, setShow);

  const clickHandler = () => {
    setShow(false);
    setClosed(true);
  };

  return (
    !closed && (
      <div
        className={`${styles.operatorBanner || ''} ${show && styles.show} ${
          tncEnable ? styles.tncEnable : ''
        } ${showTncFixed ? styles.showTncFixed || '' : ''} ${floating ? styles.floating : ''}
        `}
      >
        <div className={styles.container}>
          <div>
            <PrettyLink
              operator={operator}
              pageTemplate={pageTemplate}
              module={module.name}
              tracker={module?.tracking_link_name || 'main'}
              clickedElement="oneliner"
              className={`${styles.logo || ''} operator-banner-gtm logo-cta`}
            >
              <LazyImage
                alt={operator?.name}
                src={imagePrettyUrl(logo, 120, 120)}
                loading="eager"
                height={120}
                width={100}
              />
              <div className={styles.details}>
                {name && <span>{name}</span>}
                {bonus?.rating && <Rating rating={bonus?.rating} showDecimal />}
              </div>
            </PrettyLink>
            <div className={styles.bonus || ''}>
              {showWelcomeText && (
                <p className={styles.welcomeBonus}>
                  {translate(translations, 'welcome_bonus', 'Welcome Bonus')}
                </p>
              )}

              <BonusBox item={operator} tracker={module?.type || 'main'} />
              {tncEnable && !tncFixed && <Tnc hasCollapse={false} operator={operator} />}
            </div>
          </div>

          <OperatorCta
            operator={operator}
            module={module.name}
            pageTemplate={pageTemplate}
            tracker={module?.tracking_link_name || 'main'}
            icon={<FaArrowRight fontSize={20} title="Right-pointing Arrow Icon" />}
            translationsObj={translationsObj}
          />
          {showTncFixed && <Tnc hasCollapse={false} operator={operator} />}
          <IoMdClose
            className={`${styles.closeBtn || ''}`}
            onClick={clickHandler}
            fontSize={16}
            color="#ffffff"
            title="Close Icon"
          />
        </div>
      </div>
    )
  );
}

TemplateOne.propTypes = {
  showWelcomeText: PropTypes.bool,
  showOnLoad: PropTypes.bool,
  operator: PropTypes.shape({
    standardised_logo_url: PropTypes.string,
    logo: PropTypes.string,
    logo_url: PropTypes.string,
    bonus: PropTypes.shape({
      rating: PropTypes.string,
    }),
    name: PropTypes.string,
    rating: PropTypes.string,
    url: PropTypes.string,
    extra_fields: PropTypes.shape({
      terms_and_conditions_text_enabled: PropTypes.string,
    }),
    status: PropTypes.string,
  }),
  tncFixed: PropTypes.bool,
  tncEnable: PropTypes.bool,
  stickyOffset: PropTypes.number,
  floating: PropTypes.bool,
  pageTemplate: PropTypes.string,
  module: PropTypes.shape({
    tracking_link_name: PropTypes.string,
    type: PropTypes.string,
    name: PropTypes.string,
  }),
};
