import { createElement, lazy } from 'react'

const DocumentPrinterInfo = lazy(
  () => import('../../components/Dashboard/Management/DocumentPrinters/DocumentPrinterInfo')
)
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 DocumentPrinterIcon from '../../components/Icons/DocumentPrinterIcon'

export const DocumentPrinter = {
  name: 'documentPrinter',
  label: 'Document Printer',
  labelPlural: 'Document Printers',
  url: '/dashboard/management/documentprinters',
  prefix: 'DPR',
  icon: DocumentPrinterIcon,
  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
      }
    },
  ],
  pages: [
    {
      name: 'info',
      content: () => createElement(DocumentPrinterInfo)
    }
  ],
  columns: [
    '_reference',
    'name',
    'state',
    'host',
    'tags',
    'connectedAt',
    'createdAt',
    'updatedAt'
  ],
  filters: [
    'name',
    'tags',
    'active',
    'isGlobal',
    'state',
    'createdAt',
    'updatedAt',
    '_reference'
  ],
  sorters: [
    'name',
    'documentSize',
    'connectedAt',
    'updatedAt',
    'state',
    'createdAt'
  ],
  properties: [
    {
      name: '_id',
      label: 'ID',
      type: 'id',
      objectType: 'documentPrinter',
      showCopy: true,
      columnWidth: 140
    },
    {
      name: 'createdAt',
      label: 'Created At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: '_reference',
      label: 'Reference',
      type: 'reference',
      columnFixed: 'left',
      objectType: 'documentPrinter',
      showCopy: true,
      readOnly: true,
      columnWidth: 180
    },
    {
      name: 'updatedAt',
      label: 'Updated At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'name',
      label: 'Name',
      required: true,
      type: 'text',
      columnWidth: 200,
      columnFixed: 'left'
    },

    {
      name: 'connectedAt',
      label: 'Connected At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },

    {
      name: 'state',
      label: 'Status',
      type: 'state',
      objectType: 'printer',
      showName: false,
      readOnly: true,
      columnWidth: 260
    },

    {
      name: 'online',
      label: 'Online',
      type: 'bool',
      readOnly: true,
      columnWidth: 85
    },
    {
      name: 'active',
      label: 'Active',
      type: 'bool',
      required: true,
      columnWidth: 125
    },
    {
      name: 'vendor',
      label: 'Vendor',
      type: 'object',
      objectType: 'vendor',
      required: false,
      showHyperlink: true,
      columnWidth: 200
    },
    {
      name: 'host',
      label: 'Host',
      required: true,
      type: 'object',
      objectType: 'host',
      showHyperlink: true,
      columnWidth: 200
    },
    {
      name: 'connection.interface',
      label: 'Interface',
      type: 'select',
      options: [
        { label: 'CUPS', value: 'cups' },
        { label: 'Epson Receipt', value: 'epsonReceipt' },
        { label: 'Star Receipt', value: 'starReceipt' }
      ],
      required: true,
      columnWidth: 150
    },
    {
      name: 'connection.protocol',
      label: 'Protocol',
      type: 'select',
      options: (objectData) => {
        if (objectData?.connection?.interface == 'cups') {
          return [
            { label: 'IPP', value: 'ipp' },
            { label: 'HTTP', value: 'http' }
          ]
        }
        return [
          { label: 'TCP', value: 'tcp' },
          { label: 'Serial', value: 'serial' },
          { label: 'System', value: 'system' }
        ]
      },
      required: true,
      columnWidth: 130
    },
    {
      name: 'connection.host',
      label: 'Host Name',
      type: 'text',
      required: true,
      columnWidth: 180
    },
    {
      name: 'connection.port',
      label: 'Port',
      type: 'number',
      required: false,
      disabled: (objectData) => {
        return (
          objectData?.connection?.protocol == 'system' ||
          objectData?.connection?.protocol == 'serial'
        )
      },
      columnWidth: 85
    },
    {
      name: 'currentDocumentSize',
      label: 'Document Size',
      required: false,
      type: 'object',
      objectType: 'documentSize',
      showHyperlink: true,
      columnWidth: 200
    },
    {
      name: 'tags',
      label: 'Tags',
      required: false,
      type: 'tags',
      columnWidth: 200
    }
  ]
}
