import { createElement, lazy } from 'react'

const FilamentInfo = lazy(
  () => import('../../components/Dashboard/Management/Filaments/FilamentInfo')
)
const NewFilamentSku = lazy(
  () => import('../../components/Dashboard/Management/FilamentSkus/NewFilamentSku')
)
import EditIcon from '../../components/Icons/EditIcon'
import CheckIcon from '../../components/Icons/CheckIcon'
import XMarkIcon from '../../components/Icons/XMarkIcon'
import FilamentIcon from '../../components/Icons/FilamentIcon'
import InfoCircleIcon from '../../components/Icons/InfoCircleIcon'
import PlusIcon from '../../components/Icons/PlusIcon'
import BinIcon from '../../components/Icons/BinIcon'

export const Filament = {
  name: 'filament',
  label: 'Filament',
  labelPlural: 'Filaments',
  url: '/dashboard/management/filaments',
  prefix: 'FIL',
  icon: FilamentIcon,
  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
      }
    },
    { type: 'divider' },
    {
      name: 'newFilamentSku',
      type: 'modal',
      modalWidth: 700,
      label: 'New Filament SKU',
      icon: PlusIcon,
      visible: (objectData) => {
        return !(objectData?._isEditing && objectData?._isEditing == true)
      },
      content: (objectData, { onOk } = {}) => {
        return createElement(NewFilamentSku, { defaultValues: { filament: objectData }, onOk, reset: true })
      }
    },
    { type: 'divider' },
    {
      name: 'delete',
      type: 'page',
      pageName: 'info',
      label: 'Delete',
      icon: BinIcon,
      danger: true,
      visible: (objectData) => {
        return !(objectData?._isEditing && objectData?._isEditing == true)
      }
    }
  ],
  pages: [
    {
      name: 'info',
      content: () => createElement(FilamentInfo)
    }
  ],
  columns: [
    '_reference',
    'name',
    'vendor',
    'material',
    'density',
    'diameter',
    'cost',
    'costWithTax',
    'createdAt',
    'updatedAt'
  ],
  filters: [
    '_id',
    'material',
    'material._id',
    'diameter',
    'name',
    'cost',
    'createdAt',
    'updatedAt',
    '_reference'
  ],
  sorters: [
    'name',
    'createdAt',
    'vendor',
    'material',
    'cost',
    'costWithTax',
    'updatedAt'
  ],
  group: ['diameter', 'material'],
  properties: [
    {
      name: '_id',
      label: 'ID',
      columnFixed: 'left',
      type: 'id',
      objectType: 'filament',
      showCopy: true,
      columnWidth: 140
    },
    {
      name: 'createdAt',
      label: 'Created At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: '_reference',
      label: 'Reference',
      type: 'reference',
      columnFixed: 'left',
      objectType: 'filament',
      showCopy: true,
      readOnly: true,
      columnWidth: 180
    },
    {
      name: 'updatedAt',
      label: 'Updated At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'name',
      label: 'Name',
      columnFixed: 'left',
      required: true,
      type: 'text',
      columnWidth: 200
    },
    {
      name: 'vendor',
      label: 'Vendor',
      type: 'object',
      objectType: 'vendor',
      required: false,
      showHyperlink: true,
      columnWidth: 200
    },
    {
      name: 'material',
      label: 'Material',
      required: true,
      columnWidth: 150,
      type: 'object',
      objectType: 'material',
      showHyperlink: true
    },
    {
      name: 'diameter',
      label: 'Diameter',
      columnWidth: 150,
      required: true,
      type: 'number',
      suffix: 'mm'
    },
    {
      name: 'density',
      label: 'Density',
      columnWidth: 150,
      required: true,
      type: 'number',
      suffix: 'g/cm³'
    },
    {
      name: 'emptySpoolWeight',
      label: 'Spool Weight',
      required: true,
      columnWidth: 150,
      type: 'number',
      suffix: 'g'
    },
    {
      name: 'cost',
      label: 'Cost',
      required: true,
      columnWidth: 100,
      type: 'number',
      prefix: '£',
      min: 0,
      step: 0.01
    },
    {
      name: 'costWithTax',
      label: 'Cost w/ Tax',
      required: true,
      readOnly: true,
      type: 'number',
      prefix: '£',
      min: 0,
      step: 0.01,
      columnWidth: 150,
      value: (objectData) => {
        const cost = objectData?.cost
        if (!cost) return 0
        if (objectData?.costTaxRate?.rateType == 'percentage') {
          return (
            (cost * (1 + objectData?.costTaxRate?.rate / 100)).toFixed(2) || 0
          )
        } else if (objectData?.costTaxRate?.rateType == 'amount') {
          return (cost + objectData?.costTaxRate?.rate).toFixed(2) || 0
        }
        return cost
      }
    },
    {
      name: 'costTaxRate',
      label: 'Cost Tax Rate',
      required: true,
      type: 'object',
      objectType: 'taxRate',
      showHyperlink: true,
      columnWidth: 150
    },
    {
      name: 'url',
      label: 'Link',
      required: false,
      type: 'url',
      columnWidth: 300
    },
    {
      name: 'barcode',
      label: 'Barcode',
      required: false,
      type: 'text',
      columnWidth: 150
    }
  ]
}
