import { useEffect } from 'react';
import { Helmet } from 'react-helmet-async';
import { useNavigate } from 'react-router-dom';

import { IconPlus } from '@material-hu/icons/tabler';
import Stack from '@material-hu/mui/Stack';

import Breadcrumbs from '@material-hu/components/design-system/Breadcrumbs';
import Button from '@material-hu/components/design-system/Buttons/Button';
import Title from '@material-hu/components/design-system/Title';

import { formatTitle } from 'src/utils/helmetUtils';
import { useLokaliseTranslation } from 'src/utils/i18n';
import { LogEvents, logEvent } from 'src/utils/logging';

import { LearningContainer } from '../../../common/components/LearningContainer';
import useNavigateToSettings from '../../hooks/useNavigateToSettings';
import useSettingsTitle from '../../hooks/useSettingsTitle';
import { settingsRoutes } from '../../routes';
import { CERTIFICATES_PAGE_ID } from '../constants';
import useCertificatesNewFeatureDialog from '../hooks/useCertificatesNewFeatureDialog';

import { CertificatesTable } from './components/CertificatesTable';

const CertificatesSettings = () => {
  const { t } = useLokaliseTranslation('learning');
  const navigate = useNavigate();
  const settingsTitle = useSettingsTitle();
  const navigateToSettings = useNavigateToSettings();

  useCertificatesNewFeatureDialog();

  const handleCreateCertificate = () => {
    logEvent(LogEvents.NEW_CERTIFICATE_INITIATED);
    navigate(settingsRoutes.certificates.new());
  };

  useEffect(() => {
    logEvent(LogEvents.CERTIFICATES_ENTERED);
  }, []);

  const breadcrumbs = [
    {
      href: settingsRoutes.base(),
      onClick: navigateToSettings,
      title: settingsTitle,
    },
    {
      title: t('settings.certificates.title'),
    },
  ];

  return (
    <>
      <Helmet>
        <title>{formatTitle(t('settings.certificates.title'))}</title>
      </Helmet>
      <LearningContainer id={CERTIFICATES_PAGE_ID}>
        <Breadcrumbs links={breadcrumbs} />
        <Stack
          sx={{
            justifyContent: 'space-between',
            alignItems: 'center',
            flexDirection: 'row',
          }}
        >
          <Title
            variant="L"
            title={t('settings.certificates.title')}
            description={t('settings.certificates.description')}
          />
          <Button
            variant="primary"
            onClick={handleCreateCertificate}
            startIcon={<IconPlus size={16} />}
          >
            {t('settings.certificates.create')}
          </Button>
        </Stack>
        <CertificatesTable onCreate={handleCreateCertificate} />
      </LearningContainer>
    </>
  );
};

export default CertificatesSettings;
