import React, { memo } from 'react';

import { Icons } from '@/components/icons';

const DOCUMENT_FORMATS = new Set(['.doc', 'doc', '.docx', 'docx']);
const PDF_FORMAT = new Set(['.pdf', 'pdf']);
const SHEET_FORMATS = new Set([
  '.xls',
  'xls',
  '.xlt',
  'xlt',
  '.xlsx',
  'xlsx',
  '.xltx',
  'xltx',
  '.xml',
  'xml',
  '.csv',
  'csv',
]);
const POWERPOINT_FORMATS = new Set(['.ppt', 'ppt', '.pptx', 'pptx']);
const TXT_FORMATS = new Set(['.txt', 'txt']);
const IMAGE_FORMATS = new Set([
  '.jpg',
  'jpg',
  '.jpeg',
  'jpeg',
  '.png',
  'png',
  '.gif',
  'gif',
  '.bmp',
  'bmp',
  '.svg',
  'svg',
]);
const VIDEO_FORMATS = new Set([
  '.mp4',
  'mp4',
  '.avi',
  'avi',
  '.mkv',
  'mkv',
  '.mov',
  'mov',
  '.wmv',
  'wmv',
  '.flv',
  'flv',
  '.webm',
  'webm',
]);
const AUDIO_FORMATS = new Set([
  '.mp3',
  'mp3',
  '.wav',
  'wav',
  '.aac',
  'aac',
  '.flac',
  'flac',
  '.ogg',
  'ogg',
]);
const ZIP_FORMATS = new Set([
  '.rar',
  'rar',
  '.zip',
  'zip',
  '.jar',
  'jar',
  '.gz',
  'gz',
]);

const generateIcon = (type: string, className?: string) => {
  if (DOCUMENT_FORMATS.has(type))
    return <Icons.wordTwoTone className={className} />;
  if (PDF_FORMAT.has(type)) return <Icons.pdfTwoTone className={className} />;
  if (SHEET_FORMATS.has(type))
    return <Icons.excelTwoTone className={className} />;
  if (POWERPOINT_FORMATS.has(type))
    return <Icons.pptTwoTone className={className} />;
  if (TXT_FORMATS.has(type)) return <Icons.txtTwoTone className={className} />;
  if (IMAGE_FORMATS.has(type))
    return <Icons.pictureTwoTone className={className} />;
  if (VIDEO_FORMATS.has(type))
    return <Icons.videoTwoTone className={className} />;
  if (AUDIO_FORMATS.has(type))
    return <Icons.audioTwoTone className={className} />;
  if (ZIP_FORMATS.has(type))
    return <Icons.fileZipTwoTone className={className} />;
  return <Icons.unknownFileTwoTone className={className} />;
};

export interface IFileIconProps {
  type: string;
  className?: string;
}
export const AttachmentIcon = memo(
  ({ type, className = 'h-5 w-5' }: IFileIconProps) => {
    const icon = generateIcon(type, className);
    return icon;
  }
);

AttachmentIcon.displayName = 'AttachmentIcon';
