import Stack from '@material-hu/mui/Stack';

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

import {
  type EvaluationTask,
  TaskStatus,
} from 'src/pages/dashboard/learning/courses/types';
import { exceedsAttempts } from 'src/pages/dashboard/learning/courses/utils';
import { useLokaliseTranslation } from 'src/utils/i18n';

import TaskEvaluationAnswers from './TaskEvaluationAnswers';
import TaskEvaluationProgress from './TaskEvaluationProgress';
import TaskEvaluationSuccess from './TaskEvaluationSuccess';

type TaskEvaluationResultProps = {
  task: EvaluationTask | undefined;
  onRetry: () => void;
};

const TaskEvaluationResult = ({ task, onRetry }: TaskEvaluationResultProps) => {
  const { t } = useLokaliseTranslation('general');

  const showFailedRetry =
    task && task.status === TaskStatus.FAILED && !exceedsAttempts(task);

  const showProgress = task && !task.onlyTextQuestions;

  return (
    <Stack sx={{ gap: 3, alignItems: 'center' }}>
      {showProgress && <TaskEvaluationProgress task={task} />}
      {!showProgress && <TaskEvaluationSuccess />}
      <TaskEvaluationAnswers task={task} />
      {showFailedRetry && (
        <Button
          sx={{ maxWidth: 'fit-content' }}
          variant="secondary"
          onClick={onRetry}
        >
          {t('retry')}
        </Button>
      )}
    </Stack>
  );
};
export default TaskEvaluationResult;
