import * as mimeTypes from 'mime-types';
import Blob from '../pages/categories/blob';

const MIME_TYPE_DESCRIPTIONS = {
  'image/jpeg': 'JPEG Image',
  'image/png': 'PNG Image',
  'image/tiff': 'TIFF Image',
  'image/svg+xml': 'SVG Image',
  'audio/wav': 'WAVE Audio',
  'audio/ogg': 'OGG Audio',
  'audio/mpeg': 'MPEG Audio',
  'application/json': 'JSON Data',
  'application/xml': 'XML Data',
  'application/x-bittorrent': 'Torrent File',
  'application/x-zip-compressed': 'Compressed ZIP Archive',
  'application/x-x509-ca-cert': 'Certificate File',
  'application/x-tar': 'Compressed TAR Archive',
  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'Workbook',
  'application/vnd.openxmlformats-officedocument.presentationml.presentation': 'Slideshow Presentation',
  'application/msword': 'Microsoft Word Document',
  'application/pdf': 'PDF Document',
} as const;

const EXTENSION_DESCRIPTIONS = {
  'rar': 'Compressed RAR Archive',
  '7z': 'Compressed 7-Zip Archive',
  'psd': 'Adobe Photoshop Document',
  'dmg': 'Disk Image',
  'fb2k-component': 'foobar2000 Component',
} as const;

export const getMimeTypeDescription = (type?: string, filename?: string) => {
  if (typeof (type as unknown) !== 'string') {
    return '';
  }

  if (type === 'application/octet-stream' || type === '') {
    if (typeof filename === 'string' && filename.includes('.')) {
      const extension = filename.slice(filename.lastIndexOf('.') + '.'.length).toLowerCase();
      const {
        [extension as keyof typeof EXTENSION_DESCRIPTIONS]: extensionDescription = `${extension.toUpperCase()} File`,
      } = EXTENSION_DESCRIPTIONS;

      return extensionDescription;
    }
    return `${type} File`;
  }

  const {
    [type as keyof typeof MIME_TYPE_DESCRIPTIONS]: description = type,
  } = MIME_TYPE_DESCRIPTIONS;

  return description;
}

export enum ContentType {
  TEXT = 'text',
  AUDIO = 'audio',
  VIDEO = 'video',
  IMAGE = 'image',
  BINARY = 'binary',
}

export const getContentType = (mimeType?: string, filename?: string) => {
  let effectiveMimeType: string;
  if (typeof mimeType !== 'string') {
    if (typeof filename !== 'string') {
      return ContentType.BINARY;
    }
    const lookupMimeType = mimeTypes.lookup(filename);

    if (typeof lookupMimeType !== 'string') {
      return ContentType.BINARY;
    }

    effectiveMimeType = lookupMimeType;
  } else {
    effectiveMimeType = mimeType;
  }

  if (
    effectiveMimeType === 'application/json'
    || effectiveMimeType === 'application/xml'
    || effectiveMimeType.startsWith('text/')
  ) {
    return ContentType.TEXT;
  }

  if (effectiveMimeType.startsWith('video/')) {
    return ContentType.VIDEO;
  }

  if (effectiveMimeType.startsWith('audio/')) {
    return ContentType.AUDIO;
  }

  if (effectiveMimeType.startsWith('image/')) {
    return ContentType.IMAGE;
  }

  return ContentType.BINARY;
}

export const readAsText = (blob: Blob) => blob.text();

export const readAsDataURL = (blob: Blob) => new Promise<string>((resolve, reject) => {
  const reader = new FileReader();
  reader.addEventListener('error', () => {
    reject();
  });

  reader.addEventListener('load', (e) => {
    if (!e.target) {
      reject();
      return;
    }
    resolve(e.target.result as string);
  });

  reader.readAsDataURL(blob);
});

export const readAsArrayBuffer = (blob: Blob) => blob.arrayBuffer();
