import { useContext, useRef, useState } from 'react'
import { Button, Dropdown, Flex, Modal, Space } from 'antd'
import NewPrinterProfile from './PrinterProfiles/NewPrinterProfile'
import ColumnViewButton from '../common/ColumnViewButton'
import ExportListButton from '../common/ExportListButton'
import FilterSidebarButton from '../common/FilterSidebarButton'
import ObjectTable from '../common/ObjectTable'
import ObjectTableViewButton from '../common/ObjectTableViewButton'
import { ApiServerContext } from '../context/ApiServerContext'
import PlusIcon from '../../Icons/PlusIcon'
import ReloadIcon from '../../Icons/ReloadIcon'
import useColumnVisibility from '../hooks/useColumnVisibility'
import useFilterSidebarVisibility from '../hooks/useFilterSidebarVisibility'
import useViewMode from '../hooks/useViewMode'
const PrinterProfiles = () => {
  const { deleteObject } = useContext(ApiServerContext)
  const [newProfileOpen, setNewProfileOpen] = useState(false)
  const tableRef = useRef()
  const [viewMode, setViewMode] = useViewMode('PrinterProfiles')
  const [columnVisibility, setColumnVisibility] =
    useColumnVisibility('printerProfile')
  const [showFilterSidebar, setShowFilterSidebar] =
    useFilterSidebarVisibility('PrinterProfiles')

  const actionItems = {
    items: [
      {
        label: 'New Printer Profile',
        key: 'newPrinterProfile',
        icon: <PlusIcon />
      },
      { type: 'divider' },
      {
        label: 'Reload List',
        key: 'reloadList',
        icon: <ReloadIcon />
      }
    ],
    onClick: ({ key }) => {
      if (key === 'reloadList') {
        tableRef.current?.reload()
      } else if (key === 'newPrinterProfile') {
        setNewProfileOpen(true)
      }
    }
  }

  const handleRowAction = (action, profile) => {
    if (action.name === 'delete') {
      Modal.confirm({
        title: 'Delete printer profile?',
        content: `This will permanently delete ${profile.name || profile._reference}.`,
        okText: 'Delete',
        okType: 'danger',
        onOk: async () => {
          await deleteObject(profile._id, 'printerProfile')
          await tableRef.current?.reload()
        }
      })
      return true
    }

    return false
  }

  return (
    <>
      <Flex vertical={'true'} gap='large' className='h-100'>
        <Flex justify={'space-between'}>
          <Space>
            <Dropdown menu={actionItems}>
              <Button>Actions</Button>
            </Dropdown>
            <ColumnViewButton
              type='printerProfile'
              visibleState={columnVisibility}
              updateVisibleState={setColumnVisibility}
            />
            <ExportListButton objectType='printerProfile' />
          </Space>
          <Space>
            <FilterSidebarButton
              active={showFilterSidebar}
              onClick={() => setShowFilterSidebar(!showFilterSidebar)}
            />
            <ObjectTableViewButton
              viewMode={viewMode}
              setViewMode={setViewMode}
            />
          </Space>
        </Flex>

        <ObjectTable
          ref={tableRef}
          type='printerProfile'
          cards={viewMode === 'cards'}
          visibleColumns={columnVisibility}
          showFilterSidebar={showFilterSidebar}
          expandHeight={true}
          onRowAction={handleRowAction}
        />
      </Flex>

      <Modal
        open={newProfileOpen}
        footer={null}
        width={800}
        onCancel={() => setNewProfileOpen(false)}
        destroyOnHidden
      >
        {newProfileOpen && (
          <NewPrinterProfile
            onOk={() => {
              setNewProfileOpen(false)
              tableRef.current?.reload()
            }}
          />
        )}
      </Modal>
    </>
  )
}

export default PrinterProfiles
