import { createElement, lazy } from 'react'

const FileInfo = lazy(
  () => import('../../components/Dashboard/Management/Files/FileInfo')
)
import DownloadIcon from '../../components/Icons/DownloadIcon'
import FileIcon from '../../components/Icons/FileIcon'
import InfoCircleIcon from '../../components/Icons/InfoCircleIcon'
import EditIcon from '../../components/Icons/EditIcon'
import CheckIcon from '../../components/Icons/CheckIcon'
import XMarkIcon from '../../components/Icons/XMarkIcon'
import BinIcon from '../../components/Icons/BinIcon'

export const File = {
  name: 'file',
  label: 'File',
  labelPlural: 'Files',
  url: '/dashboard/management/files',
  prefix: 'FLE',
  icon: FileIcon,
  actions: [
    {
      name: 'info',
      type: 'page',
      pageName: 'info',
      label: 'Info',
      default: true,
      row: true,
      icon: InfoCircleIcon
    },
    {
      name: 'edit',
      type: 'page',
      pageName: 'info',
      label: 'Edit',
      row: true,
      icon: EditIcon,
      visible: (objectData) => {
        return !(objectData?._isEditing && objectData?._isEditing == true)
      }
    },
    {
      name: 'cancelEdit',
      label: 'Cancel Edits',
      type: 'page',
      pageName: 'info',
      icon: XMarkIcon,
      visible: (objectData) => {
        return objectData?._isEditing && objectData?._isEditing == true
      }
    },
    {
      name: 'finishEdit',
      label: 'Save Edits',
      type: 'page',
      pageName: 'info',
      icon: CheckIcon,
      visible: (objectData) => {
        return objectData?._isEditing && objectData?._isEditing == true
      }
    },
    {
      name: 'download',
      type: 'page',
      pageName: 'info',
      label: 'Download',
      row: true,
      icon: DownloadIcon
    },
    { type: 'divider' },
    {
      name: 'delete',
      type: 'page',
      pageName: 'info',
      label: 'Delete',
      icon: BinIcon,
      danger: true
    }
  ],
  pages: [
    {
      name: 'info',
      content: () => createElement(FileInfo)
    }
  ],
  columns: [
    '_reference',
    'name',
    'type',
    'size',
    'temp',
    'createdAt',
    'updatedAt'
  ],
  filters: [
    '_id',
    'name',
    'type',
    'size',
    'extension',
    'createdAt',
    'updatedAt',
    '_reference'
  ],
  sorters: [
    'name',
    'type',
    'size',
    'createdAt',
    'temp',
    'updatedAt'
  ],
  group: ['type'],
  properties: [
    {
      name: '_id',
      label: 'ID',
      columnFixed: 'left',
      type: 'id',
      objectType: 'file',
      showCopy: true,
      columnWidth: 140
    },
    {
      name: 'createdAt',
      label: 'Created At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: '_reference',
      label: 'Reference',
      type: 'reference',
      columnFixed: 'left',
      objectType: 'file',
      showCopy: true,
      readOnly: true,
      columnWidth: 180
    },
    {
      name: 'name',
      label: 'Name',
      columnFixed: 'left',
      required: true,
      type: 'text',
      columnWidth: 200
    },
    {
      name: 'updatedAt',
      label: 'Updated At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'type',
      label: 'Type',
      type: 'text',
      readOnly: true,
      required: true,
      columnWidth: 190
    },
    {
      name: 'size',
      label: 'Size',
      type: 'number',
      readOnly: true,
      roundNumber: 2,
      required: true,
      suffix: (objectData) => {
        const size = objectData?.size || 0
        if (size === 0) return ' B'
        if (size < 1024) return ' B'
        if (size < 1024 * 1024) return ' KB'
        if (size < 1024 * 1024 * 1024) return ' MB'
        if (size < 1024 * 1024 * 1024 * 1024) return ' GB'
        return ' TB'
      },
      value: (objectData) => {
        const size = objectData?.size || 0
        if (size === 0) return 0
        if (size < 1024) return size
        if (size < 1024 * 1024) return size / 1024
        if (size < 1024 * 1024 * 1024) return size / (1024 * 1024)
        if (size < 1024 * 1024 * 1024 * 1024) return size / (1024 * 1024 * 1024)
        return size / (1024 * 1024 * 1024 * 1024)
      },
      columnWidth: 100
    },
    {
      name: 'metaData',
      label: 'Meta Data',
      columnWidth: 300,
      type: 'data',
      readOnly: true,
      required: false
    },
    {
      name: 'hasThumbnails',
      label: 'Has Thumbnails',
      type: 'bool',
      readOnly: true,
      required: false,
      columnWidth: 100
    }
  ]
}
