// src/Jobs.js

import { useState, useRef } from 'react'
import { Button, Flex, Space, Modal, Dropdown } from 'antd'
import NewJob from './Jobs/NewJob.jsx'
import useColumnVisibility from '../hooks/useColumnVisibility.jsx'
import PlusIcon from '../../Icons/PlusIcon.jsx'
import ReloadIcon from '../../Icons/ReloadIcon.jsx'
import ObjectTable from '../common/ObjectTable.jsx'
import useViewMode from '../hooks/useViewMode.jsx'
import ObjectTableViewButton from '../common/ObjectTableViewButton'
import FilterSidebarButton from '../common/FilterSidebarButton'
import SortSidebarButton from '../common/SortSidebarButton'
import ColumnViewButton from '../common/ColumnViewButton.jsx'
import useFilterSidebarVisibility from '../hooks/useFilterSidebarVisibility.jsx'
import useSortSidebarVisibility from '../hooks/useSortSidebarVisibility'
import ExportListButton from '../common/ExportListButton.jsx'

const Jobs = () => {
  const [newJobOpen, setNewJobOpen] = useState(false)
  const tableRef = useRef()
  const [viewMode, setViewMode] = useViewMode('job')

  const [columnVisibility, setColumnVisibility] = useColumnVisibility('job')

  const [showFilterSidebar, setShowFilterSidebar] =
    useFilterSidebarVisibility('Jobs')

  const [showSortSidebar, setShowSortSidebar] = useSortSidebarVisibility('Jobs')

  const actionItems = {
    items: [
      {
        label: 'New Print Job',
        key: 'newJob',
        icon: <PlusIcon />
      },
      { type: 'divider' },
      {
        label: 'Reload List',
        key: 'reloadList',
        icon: <ReloadIcon />
      }
    ],
    onClick: ({ key }) => {
      if (key === 'newJob') {
        showNewJobModal()
      } else if (key === 'reloadList') {
        tableRef.current?.reload()
      }
    }
  }

  const showNewJobModal = () => {
    setNewJobOpen(true)
  }

  return (
    <>
      <Flex vertical={'true'} gap='large' className='h-100'>
        <Flex justify={'space-between'} style={{ minHeight: 0 }}>
          <Space size='small'>
            <Dropdown menu={actionItems}>
              <Button>Actions</Button>
            </Dropdown>
            <ColumnViewButton
              type='job'
              disabled={false}
              visibleState={columnVisibility}
              updateVisibleState={setColumnVisibility}
            />
            <ExportListButton objectType='job' />
          </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={'job'}
          visibleColumns={columnVisibility}
          cards={viewMode === 'cards'}
          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={newJobOpen}
        footer={null}
        width={700}
        destroyOnHidden={true}
        onCancel={() => {
          setNewJobOpen(false)
        }}
      >
        <NewJob
          onOk={() => {
            setNewJobOpen(false)
            tableRef.current?.reload()
          }}
          reset={newJobOpen}
        />
      </Modal>
    </>
  )
}

export default Jobs
