import { type FunctionComponent, memo } from 'react';

import { Badge, Modal } from '@/components';
import { LOCALE_FEATURES } from '@/i18n';
import { getTileSizes } from '@/lib';
import { REMAINING_TILES_TILE_SIZE } from '@/parameters';
import { selectLocale, useTranslate, useTypedSelector } from '@/state';

import { Character } from './components';
import styles from './RemainingTilesModal.module.scss';
import { selectRemainingTilesGroups } from './selectors';

interface Props {
  className?: string;
  isOpen: boolean;
  onClose: () => void;
}

const RemainingTilesModalBase: FunctionComponent<Props> = ({ className, isOpen, onClose }) => {
  const translate = useTranslate();
  const locale = useTypedSelector(selectLocale);
  const groups = useTypedSelector(selectRemainingTilesGroups);
  const { tileFontSize } = getTileSizes(REMAINING_TILES_TILE_SIZE);
  const { direction } = LOCALE_FEATURES[locale];

  return (
    <Modal className={className} isOpen={isOpen} title={translate('remaining-tiles')} onClose={onClose}>
      {groups.map(({ remainingCount, tiles, translationKey, totalCount }) => {
        const current = direction === 'ltr' ? remainingCount : totalCount;
        const total = direction === 'ltr' ? totalCount : remainingCount;

        return (
          <Modal.Section
            key={translationKey}
            label={translate(translationKey)}
            title={
              <span className={styles.title}>
                <span>{translate(translationKey)}</span>
                <Badge className={styles.badge}>
                  {current.toLocaleString(locale)} / {total.toLocaleString(locale)}
                </Badge>
              </span>
            }
          >
            <div className={styles.content} style={{ fontSize: tileFontSize }}>
              {tiles.map((tile) => {
                return (
                  <div className={styles.character} key={tile.character}>
                    <Character tile={tile} />
                  </div>
                );
              })}
            </div>
          </Modal.Section>
        );
      })}
    </Modal>
  );
};

export const RemainingTilesModal = memo(RemainingTilesModalBase);
