import { createElement, lazy } from 'react'

const StockTransferInfo = lazy(
  () => import('../../components/Dashboard/Inventory/StockTransfers/StockTransferInfo')
)
const PostStockTransfer = lazy(
  () => import('../../components/Dashboard/Inventory/StockTransfers/PostStockTransfer')
)
import StockTransferIcon from '../../components/Icons/StockTransferIcon'
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 StockTransfer = {
  name: 'stockTransfer',
  label: 'Stock Transfer',
  labelPlural: 'Stock Transfers',
  url: '/dashboard/inventory/stocktransfers',
  prefix: 'STT',
  icon: StockTransferIcon,
  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)
      },
      disabled: (objectData) => {
        return objectData?.state?.type != 'draft'
      }
    },
    {
      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: 'delete',
      type: 'page',
      pageName: 'info',
      label: 'Delete',
      icon: BinIcon,
      danger: true,
      visible: (objectData) => {
        return !(objectData?._isEditing && objectData?._isEditing == true)
      },
      disabled: (objectData) => {
        return objectData?.state?.type != 'draft'
      }
    },
    { type: 'divider' },
    {
      name: 'post',
      type: 'modal',
      modalWidth: 520,
      label: 'Post',
      icon: CheckIcon,
      visible: (objectData) => {
        return objectData?.state?.type == 'draft'
      },
      content: (objectData, { onOk } = {}) => {
        return createElement(PostStockTransfer, { objectData, onOk })
      }
    }
  ],
  pages: [
    {
      name: 'info',
      content: () => createElement(StockTransferInfo)
    }
  ],
  filters: [
    'state',
    'state.type',
    'postedAt',
    'name',
    'createdAt',
    'updatedAt',
    '_reference'
  ],
  sorters: [
    'name',
    'createdAt',
    'postedAt',
    'state',
    'updatedAt'
  ],
  columns: [
    '_reference',
    'name',
    'state',
    'postedAt',
    'createdAt',
    'updatedAt'
  ],
  properties: [
    {
      name: '_id',
      label: 'ID',
      type: 'id',
      objectType: 'stockTransfer',
      showCopy: true,
      readOnly: true,
      columnWidth: 140
    },
    {
      name: 'createdAt',
      label: 'Created At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: '_reference',
      label: 'Reference',
      type: 'reference',
      columnFixed: 'left',
      objectType: 'stockTransfer',
      showCopy: true,
      readOnly: true,
      columnWidth: 180
    },
    {
      name: 'name',
      label: 'Name',
      type: 'text',
      required: true,
      columnWidth: 220,
      columnFixed: 'left'
    },
    {
      name: 'state',
      label: 'State',
      type: 'state',
      readOnly: true,
      columnWidth: 260
    },
    {
      name: 'postedAt',
      label: 'Posted At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'updatedAt',
      label: 'Updated At',
      type: 'dateTime',
      readOnly: true,
      columnWidth: 175
    },
    {
      name: 'lines',
      label: 'Lines',
      type: 'objectChildren',
      required: false,
      canAddRemove: true,
      columns: [
        'fromStockType',
        'fromStock',
        'quantity',
        'toStockLocation',
        'toStockType',
        'toStock'
      ],
      properties: [
        {
          name: 'fromStockType',
          label: 'From type',
          type: 'objectType',
          required: true,
          columnWidth: 180,
          masterFilter: ['filamentStock', 'partStock', 'productStock']
        },
        {
          name: 'fromStock',
          label: 'From stock',
          type: 'object',
          objectType: (row) => row?.fromStockType,
          required: true,
          showHyperlink: true,
          columnWidth: 230
        },
        {
          name: 'quantity',
          label: 'Quantity',
          type: 'number',
          required: true,
          min: 0,
          columnWidth: 140,
          suffix: (row) =>
            row?.fromStockType === 'filamentStock' ? 'g net' : null
        },
        {
          name: 'available',
          label: 'Available',
          type: 'number',
          readOnly: true,
          columnWidth: 140,
          value: (row) => {
            if (row?.fromStockType === 'filamentStock') {
              return row?.fromStock?.currentWeight?.net ?? 0
            } else {
              return row?.fromStock?.currentQuantity ?? 0
            }
          },
          suffix: (row) =>
            row?.fromStockType === 'filamentStock' ? 'g net' : null
        },
        {
          name: 'remaining',
          label: 'Remaining',
          type: 'number',
          readOnly: true,
          columnWidth: 140,
          value: (row) => {
            const quantity = row?.quantity ?? 0
            if (row?.fromStockType === 'filamentStock') {
              return (row?.fromStock?.currentWeight?.net ?? 0) - quantity
            }
            return (row?.fromStock?.currentQuantity ?? 0) - quantity
          },
          suffix: (row) =>
            row?.fromStockType === 'filamentStock' ? 'g net' : null
        },
        {
          name: 'toStockLocation',
          label: 'To location',
          type: 'object',
          objectType: 'stockLocation',
          required: true,
          showHyperlink: true,
          columnWidth: 230
        },
        {
          name: 'toStockType',
          label: 'To type',
          type: 'objectType',
          readOnly: true,
          columnWidth: 180,
          visible: (row) => Boolean(row?.toStockType)
        },
        {
          name: 'toStock',
          label: 'To stock',
          type: 'object',
          objectType: (row) => row?.toStockType,
          readOnly: true,
          showHyperlink: true,
          columnWidth: 230,
          visible: (row) => Boolean(row?.toStock)
        }
      ]
    }
  ],
  stats: [
    {
      name: 'draft.count',
      label: 'Draft',
      type: 'number',
      color: 'default'
    },
    {
      name: 'posted.count',
      label: 'Posted',
      type: 'number',
      color: 'success'
    }
  ]
}
