// src/hosts.js

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

import NewHost from './Hosts/NewHost'

import useColumnVisibility from '../hooks/useColumnVisibility'
import ColumnViewButton from '../common/ColumnViewButton'
import ExportListButton from '../common/ExportListButton'
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 useFilterSidebarVisibility from '../hooks/useFilterSidebarVisibility'

const Hosts = () => {
  const [newHostOpen, setNewHostOpen] = useState(false)
  const tableRef = useRef()

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

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

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

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

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

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

        <Modal
          open={newHostOpen}
          footer={null}
          width={700}
          destroyOnHidden={true}
          onCancel={() => {
            setNewHostOpen(false)
          }}
        >
          <NewHost
            onOk={() => {
              setNewHostOpen(false)
              tableRef.current?.reload()
            }}
            reset={newHostOpen}
          />
        </Modal>
      </Flex>
    </>
  )
}

export default Hosts
