import { CurrentUserFile } from '../../types/file.ts'
import styles from './style.module.scss'
import { Button } from '@mui/material'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faCheck } from '@fortawesome/free-solid-svg-icons'

interface FileListProps {
  files: CurrentUserFile[]
  currentFile: CurrentUserFile
  setCurrentFile: (file: CurrentUserFile) => void
  handleDownload: () => void
  downloadLabel?: string
}

const FileList = ({
  files,
  currentFile,
  setCurrentFile,
  handleDownload,
  downloadLabel
}: FileListProps) => {
  const isActive = (file: CurrentUserFile) => file.id === currentFile.id
  return (
    <div className={styles.wrap}>
      <ul className={styles.files}>
        {files.map((currentUserFile: CurrentUserFile) => (
          <li
            key={currentUserFile.id}
            className={`${styles.fileItem} ${isActive(currentUserFile) && styles.active}`}
            onClick={() => setCurrentFile(currentUserFile)}
          >
            <div className={styles.fileNumber}>{currentUserFile.id}</div>
            <div className={styles.fileInfo}>
              <div className={styles.fileName}>{currentUserFile.file.name}</div>
            </div>

            <div className={styles.fileVisual}>
              {currentUserFile.isHashValid && (
                <FontAwesomeIcon icon={faCheck} />
              )}
            </div>
          </li>
        ))}
      </ul>
      <Button variant="contained" fullWidth onClick={handleDownload}>
        {downloadLabel || 'Download Files'}
      </Button>
    </div>
  )
}

export default FileList
