import { useEffect, useMemo } from 'react'

import config from 'config'
import { HeroComponent } from 'dbaas/components/Measurement'
import { SharedCluster } from 'dbaas/features/ClusterManagement/cluster'
import { ClusterPageShell } from 'dbaas/screens/ClusterV2/components'

import { TableFallback } from '../../components/PausedStatus'

import { LogRedaction } from './LogRedaction'

const SecurityPageShell = ({ cluster }: { cluster: SharedCluster }) => {
  useEffect(() => {
    document.title = `${config.PAGE_TITLE_PREFIX}Security`
  }, [])

  const content = useMemo(() => {
    if (cluster.computed?.paused)
      return (
        <TableFallback.Paused
          cluster={cluster}
          featureName="General Security Settings"
          featureDesc="General security settings support users to configure and manage TiDB cluster system parameters."
        />
      )
    else if (cluster.computed?.pausing)
      return (
        <TableFallback.Empty
          title="General security settings support users to configure and manage TiDB cluster system parameters."
          desc="The cluster is pausing, and General Security Settings is inaccessible."
        />
      )
    else if (cluster.computed?.resuming)
      return (
        <TableFallback.Empty
          title="General security settings support users to configure and manage TiDB cluster system parameters."
          desc="The cluster is resuming. General Security Settings will be accessible after it is active.
Please wait for a while."
        />
      )
    else return <LogRedaction cluster={cluster} />
  }, [cluster])

  return (
    <ClusterPageShell
      wrapperProps={{
        display: 'flex',
        h: '100%',
        sx: {
          flexDirection: 'column'
        }
      }}
      bodyProps={{
        sx: { flex: 1, minHeight: 0 }
      }}
      title="Security"
    >
      {(cluster.computed?.paused || cluster.computed?.pausing || cluster.computed?.resuming) && <HeroComponent />}
      {content}
    </ClusterPageShell>
  )
}

export default SecurityPageShell
