import { Alert } from '@tidbcloud/uikit'
import { IconAlertTriangle } from '@tidbcloud/uikit/icons'

import { useClusterServiceListClusters } from 'dbaas/services/api-clients/v2023/cluster-management/v1beta1'
import { Commonv1beta1ClusterState } from 'dbaas/services/api-clients/v2023/cluster-management/v1beta1/interface'
import useStores from 'dbaas/stores/useStores'

export const useResourceInsufficientClusters = () => {
  const {
    store: {
      tenantData: { id: orgId },
      activeProjectId: projectId
    }
  } = useStores()

  const { data, isLoading } = useClusterServiceListClusters(
    {
      orgId,
      projectId,
      pageSize: 99999,
      clusterType: 'DEDICATED',
      onlyResourceInsufficient: true
    },
    {
      query: {
        refetchOnWindowFocus: false,
        refetchInterval: (data) => {
          const dedicatedClusters = data?.clusters
            ?.filter((item) => item.dedicatedCluster)
            ?.map((item) => item.dedicatedCluster!)

          if ((dedicatedClusters?.length ?? 0) > 0) {
            const refetchNecessary = dedicatedClusters?.some((item) =>
              Array.of<Commonv1beta1ClusterState | undefined>(
                Commonv1beta1ClusterState.CREATING,
                Commonv1beta1ClusterState.MODIFYING,
                Commonv1beta1ClusterState.PAUSED
              ).includes(item.state)
            )

            if (refetchNecessary) {
              return 60 * 1000
            }
          }

          return 30 * 60 * 1000
        }
      }
    }
  )

  return {
    insufficientClusters: data?.clusters,
    isLoading
  }
}

export const useResourceInsufficientClusterById = (id: string) => {
  const { insufficientClusters } = useResourceInsufficientClusters()
  return insufficientClusters?.find((c) => c.dedicatedCluster?.clusterId === id)
}

export const ResourceInsufficientAlert = ({ state, name }: { state?: Commonv1beta1ClusterState; name?: string }) => {
  switch (state) {
    case Commonv1beta1ClusterState.CREATING:
      return (
        <Alert icon={<IconAlertTriangle size={12} />} mb="xl" color="yellow">
          No sufficient resource is available on cloud provider now, so the creation of <b>{name}</b> is pending until
          resources become available. During this period, no additional charges will be incurred. You may choose to wait
          or delete the cluster and try again later.
        </Alert>
      )

    case Commonv1beta1ClusterState.MODIFYING:
      return (
        <Alert icon={<IconAlertTriangle size={12} />} mb="xl" color="yellow">
          No sufficient resource is available on cloud provider now, so the modification to <b>{name}</b> is pending
          until resources become available. Thank you for your patience.
        </Alert>
      )

    case Commonv1beta1ClusterState.PAUSED:
      return (
        <Alert icon={<IconAlertTriangle size={12} />} mb="xl" color="yellow">
          No sufficient resource is available on cloud provider now, so resuming <b>{name}</b> is pending until
          resources become available. Thank you for your patience.
        </Alert>
      )
    default:
      break
  }
}
