import { Group, HoverCard, Stack } from '@tidbcloud/uikit'
import { PropertyCard } from '@tidbcloud/uikit/biz'
import { IconInfoCircle } from '@tidbcloud/uikit/icons'
import { capitalize } from 'lodash-es'

import { showTidbExtension } from 'dbaas/features/Changefeed'
import { HandleDecimalOptions, HandleUnsignedBigIntOptions } from 'dbaas/features/Changefeed/constants'
import {
  ConsoleapiDataFormat,
  ConsoleapiS3Dataformat,
  DataflowDataFormat,
  DataflowProtocol,
  DataflowS3Dataformat,
  DataflowS3Protocol
} from 'dbaas/services/dataflow/interface'

import { getProtocolDisplayName } from '../CreateOrEditChangefeed/getProtocolDisplayName'

type SupportedDataFormat = DataflowS3Dataformat | DataflowDataFormat | ConsoleapiS3Dataformat | ConsoleapiDataFormat

const isS3DataFormat = (
  format: SupportedDataFormat | undefined
): format is DataflowS3Dataformat | ConsoleapiS3Dataformat => {
  return !!format && 'delimiter' in format
}

const isStructuredDataFormat = (
  format: SupportedDataFormat | undefined
): format is DataflowDataFormat | ConsoleapiDataFormat => {
  return !!format && 'schema_registry' in format
}

export function DataFormatProperties({ data }: { data?: SupportedDataFormat }) {
  const protocol = (data as { protocol?: string } | undefined)?.protocol

  return (
    <PropertyCard.Item label="Data Format">
      <Group gap={8}>
        <span>{protocol ? getProtocolDisplayName(protocol) : '-'}</span>
        <HoverCard shadow="md" withArrow withinPortal>
          <HoverCard.Target>
            <IconInfoCircle size={14} />
          </HoverCard.Target>
          <HoverCard.Dropdown>
            <Stack>
              {isS3DataFormat(data) && data.date_separator && (
                <PropertyCard.Item label="Date Separator">{capitalize(data.date_separator)}</PropertyCard.Item>
              )}
              {protocol === DataflowS3Protocol.S3_PROTOCOL_CSV && isS3DataFormat(data) && (
                <>
                  <PropertyCard.Item label="Binary Encoding Method">
                    {capitalize(data.binary_encoding_method || '')}
                  </PropertyCard.Item>
                  <PropertyCard.Item label="Delimiter">{data.delimiter}</PropertyCard.Item>
                  <PropertyCard.Item label="Quote">{data.quote}</PropertyCard.Item>
                  <PropertyCard.Item label="Null">{data.null}</PropertyCard.Item>
                  <PropertyCard.Item label="Include Commit Ts">
                    {data.include_commit_ts ? 'Enabled' : 'Disabled'}
                  </PropertyCard.Item>
                </>
              )}
              {protocol && showTidbExtension(protocol) && (
                <>
                  <PropertyCard.Item label="Enable TiDB Extension">
                    {data?.enable_tidb_extension ? 'Enable' : 'Disable'}
                  </PropertyCard.Item>
                </>
              )}
              {(protocol === DataflowProtocol.AVRO || protocol === DataflowProtocol.DEBEZIUM_AVRO) &&
                isStructuredDataFormat(data) && (
                  <>
                    {data.decimal_handling_mode && (
                      <PropertyCard.Item label="Decimal">
                        {HandleDecimalOptions.find((i) => i.value === data.decimal_handling_mode)?.text}
                      </PropertyCard.Item>
                    )}
                    {data.bigint_unsigned_handling_mode && (
                      <PropertyCard.Item label="Unsigned BigInt">
                        {HandleUnsignedBigIntOptions.find((i) => i.value === data.bigint_unsigned_handling_mode)?.text}
                      </PropertyCard.Item>
                    )}
                    {data.schema_registry && (
                      <>
                        <PropertyCard.Item label="Schema Registry Endpoint">
                          {data.schema_registry?.schema_registry_endpoints}
                        </PropertyCard.Item>
                        <PropertyCard.Item label="Schema Registry HTTP Authentication">
                          {data.schema_registry?.enableHttpAuth ? 'Enable' : 'Disable'}
                        </PropertyCard.Item>
                        {data.schema_registry?.enableHttpAuth && (
                          <PropertyCard.Item label="Schema Registry User Name">
                            {data.schema_registry?.userName}
                          </PropertyCard.Item>
                        )}
                      </>
                    )}
                  </>
                )}
              {protocol === DataflowProtocol.DEBEZIUM && isStructuredDataFormat(data) && (
                <>
                  <PropertyCard.Item label="Include Old Values in Output">
                    {data.debezium_config?.output_old_value ? 'Yes' : 'No'}
                  </PropertyCard.Item>
                  <PropertyCard.Item label="Include Schema Description in Output">
                    {data.debezium_config?.disable_schema ? 'No' : 'Yes'}
                  </PropertyCard.Item>
                </>
              )}
            </Stack>
          </HoverCard.Dropdown>
        </HoverCard>
      </Group>
    </PropertyCard.Item>
  )
}
