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

const Invoices = () => {
  const [newInvoiceOpen, setNewInvoiceOpen] = useState(false)
  const tableRef = useRef()

  const [viewMode, setViewMode] = useViewMode('invoices')

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

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

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

  const actionItems = {
    items: [
      {
        label: 'New Invoice',
        key: 'newInvoice',
        icon: <PlusIcon />
      },
      { type: 'divider' },
      {
        label: 'Reload List',
        key: 'reloadList',
        icon: <ReloadIcon />
      }
    ],
    onClick: ({ key }) => {
      if (key === 'reloadList') {
        tableRef.current?.reload()
      } else if (key === 'newInvoice') {
        setNewInvoiceOpen(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='invoice'
              loading={false}
              visibleState={columnVisibility}
              updateVisibleState={setColumnVisibility}
            />
            <ExportListButton objectType='invoice' />
          </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}
          visibleColumns={columnVisibility}
          type='invoice'
          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={newInvoiceOpen}
        styles={{ content: { paddingBottom: '24px' } }}
        footer={null}
        width={800}
        onCancel={() => {
          setNewInvoiceOpen(false)
        }}
        destroyOnHidden={true}
      >
        <NewInvoice
          onOk={() => {
            setNewInvoiceOpen(false)
            tableRef.current?.reload()
          }}
          reset={newInvoiceOpen}
        />
      </Modal>
    </>
  )
}

export default Invoices
