import { Typography } from '../../Typography'
import { RatePointsOption, RateTermDetails } from '../types'

import { RadioGroup } from 'react-aria-components'
import { IconButton } from '../../IconButton'
import { MaterialIcon } from '../../Icons/MaterialIcon'
import { Radio } from '../../Radio'
import Modal from '../Modal'
import styles from '../rate-card.module.css'
import { variants } from '../variants'

interface PointsRateCardProps {
  rateTitle: string
  paymentTerm: string
  bannerText: string
  rates: RatePointsOption[]
  selectedRate: string | undefined
  onRateSelect: (value: string) => void
  isNotEnoughPoints?: boolean
  notEnoughPointsText?: string
  rateTermDetails: RateTermDetails[]
}

export default function PointsRateCard({
  rateTitle,
  paymentTerm,
  bannerText,
  rates,
  selectedRate,
  isNotEnoughPoints,
  notEnoughPointsText,
  onRateSelect,
  rateTermDetails,
}: PointsRateCardProps) {
  const classNames = variants({
    variant: 'Points',
  })

  return (
    <div className={classNames}>
      <Typography variant="Label/xsBold">
        <p className={styles.banner}>{bannerText}</p>
      </Typography>
      <div className={styles.container}>
        <header>
          <Typography variant="Tag/sm">
            <h3 className={styles.title}>
              <Modal
                title={rateTitle}
                subtitle={paymentTerm}
                trigger={
                  <IconButton theme="Black" style="Muted">
                    <MaterialIcon icon="info" size={20} color="Icon/Default" />
                  </IconButton>
                }
              >
                {rateTermDetails.map((termGroup) => (
                  <div key={termGroup.title} className={styles.terms}>
                    <Typography variant={'Body/Paragraph/mdBold'}>
                      <p>{termGroup.title}</p>
                    </Typography>
                    {termGroup.terms.map((term) => (
                      <Typography key={term}>
                        <p className={styles.term}>
                          <MaterialIcon
                            icon="check"
                            color="Icon/Feedback/Success"
                            size={20}
                            className={styles.termsIcon}
                          />
                          {term}
                        </p>
                      </Typography>
                    ))}
                  </div>
                ))}
              </Modal>
              {rateTitle}
              <span className={styles.textSecondary}>
                {` / ${paymentTerm}`}
              </span>
            </h3>
          </Typography>
        </header>
        <div className={styles.content}>
          <RadioGroup
            aria-label={rateTitle}
            value={selectedRate}
            onChange={onRateSelect}
          >
            {rates.map((rate, index) => (
              <div key={index} className={styles.rateRow}>
                <Radio
                  value={rate.rateCode}
                  isDisabled={rate.isDisabled || isNotEnoughPoints}
                >
                  <div className={styles.pointsRow}>
                    <Typography variant="Title/Subtitle/md">
                      <p>
                        {`${rate.points} `}
                        <Typography variant="Body/Supporting text (caption)/smBold">
                          <span>
                            {`${rate.currency} ${rate.additionalPrice ? ' + ' : ''}`}
                          </span>
                        </Typography>
                      </p>
                    </Typography>
                    {rate.additionalPrice && (
                      <Typography variant="Title/Subtitle/md">
                        <p>
                          {`${rate.additionalPrice.price} `}
                          <Typography variant="Body/Supporting text (caption)/smBold">
                            <span>{rate.additionalPrice.currency}</span>
                          </Typography>
                        </p>
                      </Typography>
                    )}
                  </div>
                </Radio>
              </div>
            ))}
          </RadioGroup>
        </div>
        {isNotEnoughPoints ? (
          <footer className={styles.footer}>
            <Typography variant="Body/Supporting text (caption)/smBold">
              <p className={styles.notEnoughPoints}>
                <span className={styles.filledIcon}>
                  <MaterialIcon icon="info" isFilled size={20} />
                </span>
                {notEnoughPointsText}
              </p>
            </Typography>
          </footer>
        ) : null}
      </div>
    </div>
  )
}
