import SubmissionItem from './SubmissionItem';
import { FC } from 'react';
import { Attempt } from '../../../redux/slices/contests';

interface SubmissionsBlockProps {
    attempt: Attempt;
}

const SubmissionsBlock: FC<SubmissionsBlockProps> = ({ attempt }) => {
    const submissions = attempt?.submissions;
    const isFinished = new Date(attempt.expiresAt) < new Date();

    const checkStatus = (status: string) => {
        if (status == 'IncorrectAnswer') return 'wronganswer';
        if (status == 'TimeLimitError') return 'timelimit';
        return undefined;
    };

    return (
        <div className="mb-[50px]">
            <div className="flex items-center justify-center text-liquid-white font-bold text-[20px]">{`Попытка #${attempt.attemptId}`}</div>
            {!submissions || submissions.length == 0 ? (
                <></>
            ) : (
                <div className="grid grid-cols-7 text-center items-center h-[43px] mb-[10px] text-[16px] font-bold text-liquid-white">
                    <div>Посылка</div>
                    <div>Когда</div>
                    <div>Задача</div>
                    <div>Язык</div>
                    <div>Вердикт</div>
                    <div>Время</div>
                    <div>Память</div>
                </div>
            )}

            {!submissions || submissions.length == 0 ? (
                <div className="text-liquid-brightmain text-[16px] font-medium text-center">
                    {isFinished
                        ? 'Вы ничего не посылали в этот сеанс'
                        : 'Вы еще ничего не отсылали'}
                </div>
            ) : (
                <>
                    {submissions.map((v, i) => (
                        <SubmissionItem
                            key={i}
                            id={v.id ?? 0}
                            datetime={v.solution.time}
                            missionId={v.solution.missionId}
                            language={v.solution.language}
                            verdict={
                                v.solution.testerMessage?.includes(
                                    'Compilation failed',
                                )
                                    ? 'Compilation failed'
                                    : v.solution.testerMessage
                            }
                            duration={1000}
                            memory={256 * 1024 * 1024}
                            type={i % 2 ? 'second' : 'first'}
                            status={
                                v.solution.testerMessage == 'All tests passed'
                                    ? 'success'
                                    : checkStatus(v.solution.testerErrorCode)
                            }
                        />
                    ))}
                </>
            )}
            <div className="h-[1px] bg-liquid-lighter mt-[50px]"></div>
        </div>
    );
};

export default SubmissionsBlock;
