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

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

const AuditLogs = () => {
  const tableRef = useRef()

  const [columnVisibility, updateColumnVisibility] =
    useColumnVisibility('auditLog')

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

  const actionItems = {
    items: [
      {
        label: 'Reload List',
        key: 'reloadList',
        icon: <ReloadIcon />
      }
    ],
    onClick: ({ key }) => {
      if (key === 'reloadList') {
        tableRef.current?.reload()
      }
    }
  }

  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='auditLog'
              loading={false}
              visibleState={columnVisibility}
              updateVisibleState={updateColumnVisibility}
            />
            <ExportListButton objectType='auditLog' />
          </Space>
          <Space>
            <FilterSidebarButton
              active={showFilterSidebar}
              onClick={() => setShowFilterSidebar(!showFilterSidebar)}
            />
          </Space>
        </Flex>

        <ObjectTable
          ref={tableRef}
          visibleColumns={columnVisibility}
          type='auditLog'
          showFilterSidebar={showFilterSidebar}
          expandHeight={true}
          saveFilterInSession={true}
          saveFilterInUrl={true}
          useFilterInSession={true}
          useFilterInUrl={true}
          saveSortInSession={true}
          saveSortInUrl={true}
          useSortInSession={true}
          useSortInUrl={true}
        />
      </Flex>
    </>
  )
}

export default AuditLogs
