// src/filaments.js

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

import NewFilament from './Filaments/NewFilament'

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 useViewMode from '../hooks/useViewMode'
import ObjectTableViewButton from '../common/ObjectTableViewButton'
import FilterSidebarButton from '../common/FilterSidebarButton'
import SortSidebarButton from '../common/SortSidebarButton'
import ExportListButton from '../common/ExportListButton'
import useFilterSidebarVisibility from '../hooks/useFilterSidebarVisibility'
import useSortSidebarVisibility from '../hooks/useSortSidebarVisibility'

const Filaments = () => {
  const [newFilamentOpen, setNewFilamentOpen] = useState(false)
  const tableRef = useRef()

  // View mode state (cards/list), persisted in sessionStorage via custom hook
  const [viewMode, setViewMode] = useViewMode('filament')

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

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

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

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

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

export default Filaments
