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 useSortSidebarVisibility from '../hooks/useSortSidebarVisibility'
import ObjectTable from '../common/ObjectTable'
import FilterSidebarButton from '../common/FilterSidebarButton'
import SortSidebarButton from '../common/SortSidebarButton'
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 [showSortSidebar, setShowSortSidebar] =
    useSortSidebarVisibility('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>
            <SortSidebarButton
              active={showSortSidebar}
              onClick={() => setShowSortSidebar(!showSortSidebar)}
            />
            <FilterSidebarButton
              active={showFilterSidebar}
              onClick={() => setShowFilterSidebar(!showFilterSidebar)}
            />
          </Space>
        </Flex>

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

export default AuditLogs
