import { useRef, useState } from 'react'
import { Button, Dropdown, Flex, Modal, Space } from 'antd'
import NewFilamentProfile from './FilamentProfiles/NewFilamentProfile'
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 FilamentProfiles = () => {
  const [newProfileOpen, setNewProfileOpen] = useState(false)
  const tableRef = useRef()
  const [viewMode, setViewMode] = useViewMode('FilamentProfiles')
  const [columnVisibility, setColumnVisibility] =
    useColumnVisibility('filamentProfile')
  const [showFilterSidebar, setShowFilterSidebar] =
    useFilterSidebarVisibility('FilamentProfiles')

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

  const actionItems = {
    items: [
      {
        label: 'New Filament Profile',
        key: 'newFilamentProfile',
        icon: <PlusIcon />
      },
      { type: 'divider' },
      {
        label: 'Reload List',
        key: 'reloadList',
        icon: <ReloadIcon />
      }
    ],
    onClick: ({ key }) => {
      if (key === 'reloadList') {
        tableRef.current?.reload()
      } else if (key === 'newFilamentProfile') {
        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='filamentProfile'
              visibleState={columnVisibility}
              updateVisibleState={setColumnVisibility}
            />
            <ExportListButton objectType='filamentProfile' />
          </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='filamentProfile'
          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 && (
          <NewFilamentProfile
            onOk={() => {
              setNewProfileOpen(false)
              tableRef.current?.reload()
            }}
          />
        )}
      </Modal>
    </>
  )
}

export default FilamentProfiles
