import { 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 SortSidebarButton from '../common/SortSidebarButton'
import ObjectTable from '../common/ObjectTable'
import ObjectTableViewButton from '../common/ObjectTableViewButton'
import PlusIcon from '../../Icons/PlusIcon'
import ReloadIcon from '../../Icons/ReloadIcon'
import useColumnVisibility from '../hooks/useColumnVisibility'
import useFilterSidebarVisibility from '../hooks/useFilterSidebarVisibility'
import useSortSidebarVisibility from '../hooks/useSortSidebarVisibility'
import useViewMode from '../hooks/useViewMode'
const PrinterProfiles = () => {
  const [newProfileOpen, setNewProfileOpen] = useState(false)
  const tableRef = useRef()
  const [viewMode, setViewMode] = useViewMode('PrinterProfiles')
  const [columnVisibility, setColumnVisibility] =
    useColumnVisibility('printerProfile')
  const [showFilterSidebar, setShowFilterSidebar] =
    useFilterSidebarVisibility('PrinterProfiles')

  const [showSortSidebar, setShowSortSidebar] =
    useSortSidebarVisibility('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)
      }
    }
  }

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

        <ObjectTable
          ref={tableRef}
          type='printerProfile'
          cards={viewMode === 'cards'}
          visibleColumns={columnVisibility}
          showFilterSidebar={showFilterSidebar}
          showSortSidebar={showSortSidebar}
          expandHeight={true}
          saveFilterInSession={true}
          saveFilterInUrl={true}
          useFilterInSession={true}
          useFilterInUrl={true}
          saveSortInSession={true}
          saveSortInUrl={true}
          useSortInSession={true}
          useSortInUrl={true}
        />
      </Flex>

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

export default PrinterProfiles
