import { useRef, useState } from 'react'
import { Button, Flex, Space, Modal, Dropdown } from 'antd'

import NewMaterial from './Materials/NewMaterial'

import useColumnVisibility from '../hooks/useColumnVisibility'
import ColumnViewButton from '../common/ColumnViewButton'
import ObjectTable from '../common/ObjectTable'
import PlusIcon from '../../Icons/PlusIcon'
import ReloadIcon from '../../Icons/ReloadIcon'
import ObjectTableViewButton from '../common/ObjectTableViewButton'
import FilterSidebarButton from '../common/FilterSidebarButton'
import useViewMode from '../hooks/useViewMode'
import useFilterSidebarVisibility from '../hooks/useFilterSidebarVisibility'
import ExportListButton from '../common/ExportListButton'

const Materials = () => {
  const [newMaterialOpen, setNewMaterialOpen] = useState(false)
  const tableRef = useRef()

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

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

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

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

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

        <ObjectTable
          ref={tableRef}
          type='material'
          cards={viewMode === 'cards'}
          visibleColumns={columnVisibility}
          showFilterSidebar={showFilterSidebar}
          expandHeight={true}
        />

        <Modal
          open={newMaterialOpen}
          footer={null}
          width={700}
          onCancel={() => {
            setNewMaterialOpen(false)
          }}
        >
          <NewMaterial
            onOk={() => {
              setNewMaterialOpen(false)
              tableRef.current?.reload()
            }}
            reset={newMaterialOpen}
          />
        </Modal>
      </Flex>
    </>
  )
}

export default Materials
