import {
  Divider,
  Flex,
  Grid,
  Icon as ChakraIcon,
  Image,
  Stack,
  useColorModeValue,
} from "@chakra-ui/react";
import type { FunctionComponent } from "react";
import { HOME_ANALYTICS, SECTION_PADDING } from "./constants";
import { InfoSection } from "./InfoSection";
import testIds from "./testIds";
import { CatalogSearchSort } from "../../api/catalog-search/constants";
import { NavLink } from "../../components/NavLink";
import { CDKType, CDKTYPE_RENDER_MAP } from "../../constants/constructs";
import {
  Language,
  LANGUAGE_RENDER_MAP,
  TEMP_SUPPORTED_LANGUAGES,
} from "../../constants/languages";
import { AWSIcon } from "../../icons/AWSIcon";
import { DatadogIcon } from "../../icons/DatadogIcon";
import { MongoDBIcon } from "../../icons/MongoDBIcon";
import { getSearchPath } from "../../util/url";

const publishers = [
  {
    name: "Datadog",
    Icon: DatadogIcon,
    url: getSearchPath({ keywords: ["Datadog"] }),
  },
  {
    name: "AWS",
    Icon: AWSIcon,
    url: getSearchPath({ query: "AWS" }),
  },
  {
    name: "MongoDB",
    Icon: MongoDBIcon,
    url: getSearchPath({ keywords: ["MongoDB"] }),
  },
  {
    name: "Aqua Security",
    imgsrc: "/assets/aqua-security.png",
    url: getSearchPath({ keywords: ["aqua"] }),
  },
];

const IconLink = ({
  url,
  imgsrc,
  label,
  Icon,
}: {
  url: string;
  label: string;
  imgsrc?: string;
  Icon?: typeof ChakraIcon;
}) => {
  const imgFilter = useColorModeValue(
    undefined,
    "invert(100%) brightness(1.5)"
  );

  return (
    <NavLink
      color="link"
      data-event={HOME_ANALYTICS.INFO.eventName(label)}
      data-testid={testIds.infoSectionIcon}
      fontWeight="bold"
      to={url}
    >
      <Stack align="center" spacing={2} textAlign="center">
        {imgsrc && (
          <Image
            aria-label={label}
            filter={label === "AWS CDK" ? imgFilter : undefined}
            h={8}
            src={imgsrc}
          />
        )}
        {Icon && <Icon aria-label={label} h={8} w={8} />}
        <span>{label}</span>
      </Stack>
    </NavLink>
  );
};

const ResponsiveDivider = () => (
  <>
    <Divider display={{ xl: "none" }} w="auto" />
    <Divider
      display={{ base: "none", xl: "initial" }}
      h="auto"
      my={SECTION_PADDING.Y}
      orientation="vertical"
    />
  </>
);

const Row: FunctionComponent = ({ children }) => (
  <Flex align="center" pt={4} sx={{ gap: "2rem" }}>
    {children}
  </Flex>
);

export const Info: FunctionComponent = () => (
  <Flex bg="bgSecondary" data-testid={testIds.infoContainer} direction="column">
    <Grid
      gap={SECTION_PADDING.Y}
      paddingX={SECTION_PADDING.X}
      templateColumns={{ base: "1fr", xl: "1fr auto 1fr auto 1fr" }}
      templateRows={{ base: "1fr auto 1fr auto 1fr", xl: "auto" }}
    >
      <InfoSection
        description="Find libraries for AWS Cloud Development Kit (AWS CDK), which generates AWS CloudFormation templates, CDK for Terraform (CDKTF), which generates HashiCorp Terraform configuration files, and CDK for Kubernetes (CDK8s), which generates Kubernetes manifests."
        title="One home for all CDKs"
      >
        <Row>
          {Object.entries(CDKTYPE_RENDER_MAP).map(
            ([cdktype, { name, imgsrc }]) => (
              <IconLink
                imgsrc={imgsrc}
                key={cdktype}
                label={name}
                url={getSearchPath({
                  cdkType: cdktype as CDKType,
                  sort: CatalogSearchSort.DownloadsDesc,
                })}
              />
            )
          )}
        </Row>
      </InfoSection>

      <ResponsiveDivider />

      <InfoSection
        description="Define, test, and deploy cloud infrastructure using high level programming languages such as TypeScript, Python, Java, and .NET. Find documentation, API references and code samples to quickly build your application."
        title="Support across languages"
      >
        <Row>
          {Object.entries(LANGUAGE_RENDER_MAP)
            .filter(([language]) =>
              TEMP_SUPPORTED_LANGUAGES.has(language as Language)
            )
            .map(([language, { icon: Icon, name }]) => (
              <IconLink
                Icon={Icon as typeof ChakraIcon}
                key={language}
                label={name}
                url={getSearchPath({
                  languages: [language as Language],
                  sort: CatalogSearchSort.DownloadsDesc,
                })}
              />
            ))}
        </Row>
      </InfoSection>

      <ResponsiveDivider />

      <InfoSection
        description="Find construct libraries published by the community and cloud service providers such as Datadog, Amazon Web Services (AWS), MongoDB, Aqua Security, and more."
        title="Provision a range of cloud resources"
      >
        <Row>
          {publishers.map(({ url, name, imgsrc, Icon }) => (
            <IconLink
              Icon={Icon}
              imgsrc={imgsrc}
              key={name}
              label={name}
              url={url}
            />
          ))}
        </Row>
      </InfoSection>
    </Grid>
  </Flex>
);
