import { createElement, lazy } from 'react'

const PrinterInfo = lazy(
  () => import('../../components/Dashboard/Production/Printers/PrinterInfo')
)
const NewPrinterProfile = lazy(
  () =>
    import('../../components/Dashboard/Production/PrinterProfiles/NewPrinterProfile')
)
const LoadFilamentStock = lazy(
  () =>
    import('../../components/Dashboard/Inventory/FilamentStocks/LoadFilamentStock')
)
const UnloadFilamentStock = lazy(
  () =>
    import('../../components/Dashboard/Inventory/FilamentStocks/UnloadFilamentStock')
)
const RestartPrinter = lazy(
  () => import('../../components/Dashboard/Production/Printers/RestartPrinter')
)
const RestartPrinterFirmware = lazy(
  () =>
    import('../../components/Dashboard/Production/Printers/RestartPrinterFirmware')
)
const RestartMoonraker = lazy(
  () =>
    import('../../components/Dashboard/Production/Printers/RestartMoonraker')
)
import PrinterIcon from '../../components/Icons/PrinterIcon'
import InfoCircleIcon from '../../components/Icons/InfoCircleIcon'
import ReloadIcon from '../../components/Icons/ReloadIcon'
import EditIcon from '../../components/Icons/EditIcon'
import CheckIcon from '../../components/Icons/CheckIcon'
import XMarkIcon from '../../components/Icons/XMarkIcon'
import PlayCircleIcon from '../../components/Icons/PlayCircleIcon'
import PauseCircleIcon from '../../components/Icons/PauseCircleIcon'
import StopCircleIcon from '../../components/Icons/StopCircleIcon'
import FilamentStockIcon from '../../components/Icons/FilamentStockIcon'
import ControlIcon from '../../components/Icons/ControlIcon'
import JobIcon from '../../components/Icons/JobIcon'
import PlusIcon from '../../components/Icons/PlusIcon'

export const Printer = {
  name: 'printer',
  label: 'Printer',
  labelPlural: 'Printers',
  url: '/dashboard/production/printers',
  prefix: 'PRN',
  icon: PrinterIcon,
  actions: [
    {
      name: 'info',
      type: 'page',
      pageName: 'info',
      label: 'Info',
      default: true,
      row: true,
      icon: InfoCircleIcon
    },
    {
      name: 'control',
      label: 'Control',
      row: true,
      icon: ControlIcon,
      url: (_id) => `/dashboard/production/printers/control?printerId=${_id}`
    },
    {
      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
      }
    },
    { type: 'divider' },
    {
      name: 'newPrinterProfile',
      type: 'modal',
      modalWidth: 700,
      label: 'New Printer Profile',
      icon: PlusIcon,
      content: (objectData, { onOk } = {}) => {
        return createElement(NewPrinterProfile, {
          defaultValues: { printer: objectData },
          onOk,
          reset: true
        })
      }
    },
    { type: 'divider' },
    {
      name: 'restartSubmenu',
      label: 'Restart',
      icon: ReloadIcon,
      disabled: (objectData) => {
        return objectData?.online == false
      },
      children: [
        {
          name: 'restart',
          type: 'modal',
          modalWidth: 520,
          modalCentered: true,
          label: 'Restart',
          icon: ReloadIcon,
          disabled: (objectData) => {
            return objectData?.online == false
          },
          content: (objectData, { onOk } = {}) => {
            return createElement(RestartPrinter, { objectData, onOk })
          }
        },
        {
          name: 'restartFirmware',
          type: 'modal',
          modalWidth: 520,
          modalCentered: true,
          label: 'Restart Firmware',
          icon: ReloadIcon,
          disabled: (objectData) => {
            return objectData?.online == false
          },
          content: (objectData, { onOk } = {}) => {
            return createElement(RestartPrinterFirmware, { objectData, onOk })
          }
        },
        { type: 'divider' },
        {
          name: 'restartMoonraker',
          type: 'modal',
          modalWidth: 520,
          modalCentered: true,
          label: 'Restart Moonraker',
          icon: ReloadIcon,
          disabled: (objectData) => {
            return objectData?.online == false
          },
          content: (objectData, { onOk } = {}) => {
            return createElement(RestartMoonraker, { objectData, onOk })
          }
        }
      ]
    },
    {
      name: 'job',
      label: 'Job',
      icon: JobIcon,
      disabled: (objectData) => {
        return objectData?.online == false
      },
      children: [
        {
          name: 'startQueue',
          label: 'Start Queue',
          icon: PlayCircleIcon,
          disabled: (objectData) => {
            return (
              objectData?.state?.type == 'error' ||
              objectData?.state?.type == 'printing' ||
              objectData?.state?.type == 'paused' ||
              objectData?.queue?.length == 0 ||
              objectData?.queue?.length == undefined
            )
          },
          url: (_id) =>
            `/dashboard/production/printers/control?printerId=${_id}&action=startQueue`
        },
        { type: 'divider' },
        {
          name: 'pauseJob',
          label: 'Pause Job',
          icon: PauseCircleIcon,
          disabled: (objectData) => {
            return objectData?.state?.type != 'printing'
          },
          url: (_id) =>
            `/dashboard/production/printers/control?printerId=${_id}&action=pauseJob`
        },
        {
          name: 'resumeJob',
          label: 'Resume Job',
          icon: PlayCircleIcon,
          disabled: (objectData) => {
            return objectData?.state?.type != 'paused'
          },
          url: (_id) =>
            `/dashboard/production/printers/control?printerId=${_id}&action=resumeJob`
        },
        {
          name: 'cancelJob',
          label: 'Cancel Job',
          icon: StopCircleIcon,
          disabled: (objectData) => {
            return (
              objectData?.state?.type != 'printing' &&
              objectData?.state?.type != 'paused'
            )
          },
          url: (_id) =>
            `/dashboard/production/printers/control?printerId=${_id}&action=cancelJob`
        }
      ]
    },
    {
      name: 'filamentStock',
      label: 'Filament Stock',
      icon: FilamentStockIcon,
      disabled: (objectData) => {
        return objectData?.online == false
      },
      children: [
        {
          name: 'loadFilamentStock',
          type: 'modal',
          modalWidth: 700,
          label: 'Load Filament Stock',
          icon: FilamentStockIcon,
          disabled: (objectData) => {
            return (
              objectData?.state?.type == 'printing' ||
              objectData?.state?.type == 'error' ||
              objectData?.currentFilamentStock != null
            )
          },
          content: (objectData, { onOk } = {}) => {
            return createElement(LoadFilamentStock, {
              printer: objectData,
              onOk,
              reset: false,
              filamentStockLoaded: false
            })
          }
        },
        {
          name: 'unloadFilamentStock',
          type: 'modal',
          modalWidth: 700,
          label: 'Unload Filament Stock',
          icon: FilamentStockIcon,
          disabled: (objectData) => {
            return (
              objectData?.state?.type == 'printing' ||
              objectData?.state?.type == 'error' ||
              objectData?.currentFilamentStock == null
            )
          },
          content: (objectData, { onOk } = {}) => {
            return createElement(UnloadFilamentStock, {
              printer: objectData,
              onOk,
              reset: false,
              filamentStockLoaded: false
            })
          }
        }
      ]
    }
  ],
  pages: [
    {
      name: 'info',
      content: () => createElement(PrinterInfo)
    }
  ],
  columns: [
    '_reference',
    'name',
    'state',
    'host',
    'tags',
    'connectedAt',
    'createdAt',
    'updatedAt'
  ],
  filters: [
    'tags',
    'host',
    'active',
    'online',
    'name',
    '_id',
    '_reference',
    'state',
    'createdAt',
    'updatedAt'
  ],
  sorters: ['name', 'state', 'connectedAt', 'createdAt', 'updatedAt', 'host'],
  group: ['tags'],
  properties: [
    {
      name: '_id',
      label: 'ID',
      type: 'id',
      objectType: 'printer',
      showCopy: true,
      columnWidth: 140
    },
    {
      name: 'createdAt',
      label: 'Created At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: '_reference',
      label: 'Reference',
      type: 'reference',
      columnFixed: 'left',
      objectType: 'printer',
      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: 150
    },
    {
      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',
      type: 'object',
      objectType: 'host',
      required: true,
      showHyperlink: true,
      columnWidth: 200
    },
    {
      name: 'moonraker.host',
      label: 'Hostname',
      type: 'text',
      required: true,
      columnWidth: 180
    },
    {
      name: 'moonraker.port',
      label: 'Port',
      type: 'number',
      required: true,
      columnWidth: 85
    },
    {
      name: 'moonraker.apiKey',
      label: 'API Key',
      type: 'secret',
      reveal: true,
      required: false,
      columnWidth: 140
    },
    {
      name: 'moonraker.protocol',
      label: 'Protocol',
      type: 'wsprotocol',
      required: true,
      columnWidth: 110
    },
    {
      name: 'tags',
      label: 'Tags',
      type: 'tags',
      required: false,
      columnWidth: 200
    },
    {
      name: 'firmware',
      label: 'Firmware Version',
      type: 'text',
      required: false,
      readOnly: true,
      columnWidth: 150
    },
    {
      name: 'currentFilamentStock',
      label: 'Filament Stock',
      type: 'object',
      objectType: 'filamentStock',
      required: false,
      showHyperlink: true,
      columnWidth: 200
    },
    {
      name: 'currentJob',
      label: 'Current Job',
      type: 'object',
      objectType: 'job',
      required: false,
      showHyperlink: true,
      columnWidth: 200
    },
    {
      name: 'currentSubJob',
      label: 'Current Sub Job',
      type: 'object',
      objectType: 'subJob',
      required: false,
      showHyperlink: true,
      columnWidth: 200
    },
    {
      name: 'alerts',
      label: 'Alerts',
      type: 'alerts',
      required: false,
      columnWidth: 200
    },
    {
      name: 'queue',
      label: 'Queue',
      type: 'objectList',
      objectType: 'subJob',
      required: false,
      readOnly: true,
      columnWidth: 200
    },
    {
      name: 'pendingSlicerUploads',
      type: 'objectChildren',
      label: 'Pending Slicer Uploads',
      properties: [
        {
          name: 'file',
          label: 'File',
          type: 'object',
          objectType: 'file',
          required: true,
          showHyperlink: true
        },
        {
          name: 'shouldPrint',
          label: 'Should Print',
          type: 'bool',
          required: true,
          columnWidth: 125
        },
        {
          name: 'job',
          label: 'Job',
          type: 'object',
          objectType: 'job',
          required: false,
          showHyperlink: true
        }
      ]
    }
  ],
  stats: [
    {
      name: 'standby.count',
      label: 'Ready',
      type: 'number',
      sum: ['standby.count', 'complete.count'],
      color: 'success'
    },
    {
      name: 'printing.count',
      label: 'Printing',
      type: 'number',
      color: 'processing'
    },
    {
      name: 'error.count',
      label: 'Error',
      type: 'number',
      color: 'error'
    },
    {
      name: 'offline.count',
      label: 'Offline',
      type: 'number',
      color: 'default'
    }
  ]
}
