// @flow
import React, { type ComponentType } from 'react';
import styled, { useTheme } from 'styled-components';
import {
  Box,
  type BoxProps,
  Card,
  type CardProps,
  CardSection,
  SectionHeader,
  Span,
  Text,
} from '../../..';
import type { ColorPaletteColorNameKey } from '../branding.proptypes';

const BackgroundColor: ComponentType<BoxProps> = styled(Box)`
  min-height: 100px;
`;

const SwatchContainer: ComponentType<CardProps> = styled(Card)`
  overflow: hidden;
`;

type SwatchProps = {
  backgroundColorName: ColorPaletteColorNameKey,
  legalForegroundColorNames: ColorPaletteColorNameKey[],
};
const Swatch = ({
  backgroundColorName,
  legalForegroundColorNames,
}: SwatchProps) => {
  const theme = useTheme();
  return (
    <SwatchContainer spacing="mv2">
      <CardSection spacing="p1" stacked="row" justify="center">
        <Text spacing="m0" fontSize="f0">
          <Span bold>{backgroundColorName}</Span> (
          {theme.colorPalette[backgroundColorName]})
        </Text>
      </CardSection>
      <CardSection>
        <BackgroundColor
          colorName={backgroundColorName}
          spacing="pt1"
          stacked="column"
          align="end"
          justify="center"
        >
          {legalForegroundColorNames.map((legalForegroundColorName) => (
            <Text
              fontSize="f0"
              colorName={legalForegroundColorName}
              spacing="mr1"
            >
              <Span bold>{legalForegroundColorName}</Span> (
              {theme.colorPalette[legalForegroundColorName]})
            </Text>
          ))}
        </BackgroundColor>
      </CardSection>
    </SwatchContainer>
  );
};

const Basics = () => (
  <Box spacing="mr2" basis={225}>
    <SectionHeader>Basics</SectionHeader>
    <Swatch
      infoTextColorName="greyscale-dark"
      backgroundColorName="background"
      legalForegroundColorNames={[
        'greyscale-dark',
        'greyscale-black',
        'brand-primary-fg',
        'brand-secondary-fg',
      ]}
    />

    <Swatch
      infoTextColorName="greyscale-dark"
      backgroundColorName="white"
      legalForegroundColorNames={[
        'greyscale-dark',
        'greyscale-black',
        'brand-primary-fg',
        'brand-secondary-fg',
      ]}
    />
  </Box>
);

const Primary = () => (
  <Box spacing="mr2" basis={220}>
    <SectionHeader>Primary</SectionHeader>

    <Swatch
      infoTextColorName="brand-primary-fg"
      backgroundColorName="brand-primary-bg"
      legalForegroundColorNames={['brand-primary-fg']}
    />

    <Swatch
      infoTextColorName="brand-primary-bg"
      backgroundColorName="brand-primary-fg"
      legalForegroundColorNames={['brand-primary-bg', 'white']}
    />

    <Swatch
      infoTextColorName="white"
      backgroundColorName="brand-primary-base"
      legalForegroundColorNames={[]}
    />
  </Box>
);

const Secondary = () => (
  <Box spacing="mr2" basis={240}>
    <SectionHeader>Secondary</SectionHeader>

    <Swatch
      infoTextColorName="brand-secondary-fg"
      backgroundColorName="brand-secondary-bg"
      legalForegroundColorNames={['brand-secondary-fg']}
    />

    <Swatch
      infoTextColorName="brand-secondary-bg"
      backgroundColorName="brand-secondary-fg"
      legalForegroundColorNames={['brand-secondary-bg', 'white']}
    />

    <Swatch
      infoTextColorName="white"
      backgroundColorName="brand-secondary-base"
      legalForegroundColorNames={[]}
    />
  </Box>
);

const Alerts = () => (
  <Box basis={200}>
    <SectionHeader>Alerts</SectionHeader>
    <Swatch
      infoTextColorName="success-fg"
      backgroundColorName="success-bg"
      legalForegroundColorNames={['success-fg']}
    />
    <Swatch
      infoTextColorName="info-fg"
      backgroundColorName="info-bg"
      legalForegroundColorNames={['info-fg']}
    />
    <Swatch
      infoTextColorName="error-fg"
      backgroundColorName="error-bg"
      legalForegroundColorNames={['error-fg']}
    />
  </Box>
);

export const Palette = () => (
  <Box stacked="row" spacing={['mt3', 'pr1']} justify="space-between" wrap>
    <Basics />
    <Primary />
    <Secondary />
    <Alerts />
  </Box>
);
