import { ref } from 'vue'
import { defineStore } from 'pinia'
import mime from 'mime'

import type { StorageFile } from '@/stores/storage'

export interface MimeMap {
  [key: string]: { icon: string; color: string }
}
export interface IconMap {
  [key: string]: { icon: string; color: string }
}

export const useFileInfoStore = defineStore('info', () => {
  const isFilePreview = ref<Boolean>(false)
  const fileInfo = ref<StorageFile>()
  const fileIcon: IconMap = {
    word: { icon: 'mdi-file-word-outline', color: 'blue-11' },
    excel: { icon: 'mdi-file-excel-outline', color: 'green-4' },
    powerpoint: { icon: 'mdi-file-powerpoint-outline', color: 'orange-4' },
    pdf: { icon: 'mdi-file-pdf-outline', color: 'red-11' },
    txt: { icon: 'mdi-file-document-outline', color: 'blue-11' },
    image: { icon: 'mdi-file-image-outline', color: 'blue-11' },
  }
  const mimeFileMapping: MimeMap = {
    'application/msword': fileIcon.word,
    'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
      fileIcon.word,
    'application/vnd.openxmlformats-officedocument.wordprocessingml.template':
      fileIcon.word,
    'application/vnd.ms-word.document.macroEnabled.12': fileIcon.word,
    'application/vnd.ms-word.template.macroEnabled.12': fileIcon.word,
    'application/vnd.ms-excel': fileIcon.excel,
    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
      fileIcon.excel,
    'application/vnd.openxmlformats-officedocument.spreadsheetml.template':
      fileIcon.excel,
    'application/vnd.ms-excel.sheet.macroEnabled.12': fileIcon.excel,
    'application/vnd.ms-excel.template.macroEnabled.12': fileIcon.excel,
    'application/vnd.ms-excel.addin.macroEnabled.12': fileIcon.excel,
    'application/vnd.ms-excel.sheet.binary.macroEnabled.12': fileIcon.excel,
    'application/vnd.ms-powerpoint': fileIcon.powerpoint,
    'application/vnd.openxmlformats-officedocument.presentationml.presentation':
      fileIcon.powerpoint,
    'application/vnd.openxmlformats-officedocument.presentationml.template':
      fileIcon.powerpoint,
    'application/vnd.openxmlformats-officedocument.presentationml.slideshow':
      fileIcon.powerpoint,
    'application/vnd.ms-powerpoint.addin.macroEnabled.12': fileIcon.powerpoint,
    'application/vnd.ms-powerpoint.presentation.macroEnabled.12':
      fileIcon.powerpoint,
    'application/vnd.ms-powerpoint.template.macroEnabled.12':
      fileIcon.powerpoint,
    'application/vnd.ms-powerpoint.slideshow.macroEnabled.12':
      fileIcon.powerpoint,
    'application/pdf': fileIcon.pdf,
    'text/plain': fileIcon.txt,
    'image/png': fileIcon.image,
    'image/jpeg': fileIcon.image,
  }

  function getType(
    mimeType: string | undefined,
    fileName: string | undefined,
  ): string {
    if (mimeType === undefined) return 'ไม่ทราบประเภท'

    const extension = mime.getExtension(mimeType)

    if (extension) return '.' + extension
    if (fileName && fileName.includes('.')) {
      return fileName.substring(fileName.lastIndexOf('.'))
    }

    return 'ไม่ทราบประเภท'
  }

  function getFormatDate(dateTime: string | undefined): string {
    if (dateTime === undefined) {
      return 'ไม่ทราบวันที่'
    }
    const date = new Date(dateTime)
    return date.toLocaleDateString('th-TH', {
      year: 'numeric',
      month: 'long',
      day: 'numeric',
    })
  }

  function getSize(size: string | undefined): string {
    if (size === undefined) return 'ไม่ทราบขนาด'

    const units = ['B', 'KB', 'MB', 'GB', 'TB']

    let i = 0
    let sizeNumber = parseFloat(size)
    while (sizeNumber >= 1024 && i++ < units.length - 1) {
      sizeNumber /= 1024
    }
    return sizeNumber.toFixed(2) + ' ' + units[i]
  }

  function getFileInfo(data: StorageFile) {
    isFilePreview.value = true
    fileInfo.value = data
  }

  return {
    mimeFileMapping,
    isFilePreview,
    fileInfo,
    getType,
    getFormatDate,
    getSize,
    getFileInfo,
  }
})
