/* eslint-disable no-nested-ternary */
// @flow
import React from 'react';
import styled from 'styled-components';
import ApoliticalBrand from '../../../Theme/ApoliticalBrand';

type Props = {
  /** Only strings should be placed inside buttons */
  +children: any,
  /** What to do when the button is clicked */
  action?: ()=>void,
  /** Takes a string to determine button variant. Defaults to 'primary'.
   * Tertiary is a styled `a`, rather than styled button.   */
  variant?: string,
  /** Boolean to determine whether disabled or not. Optional, defaults to false. */
  disabled?: boolean,
/** Optional href, for use with tertiary */
  href?: string,
  /** Optional, allows for overriding or extending styling */
  +className?: string,
    /** Optional, defaults to 'button', allows to be submit.
     * Setting to submit removes onClick event. */
  +type?: string,
  /** Optional, allows for setting the target attribute of an anchor tag */
  +target?: string,
}

const ButtonAsLink = styled.a`
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  display: inline-block;
  line-height: 2.5em;
  text-decoration: none;
  cursor: pointer;
  background: ${({ colorScheme }) => colorScheme.background};
  font-size: ${({ colorScheme }) => colorScheme.fontSize};
  height: ${({ colorScheme }) => colorScheme.height};
  border-radius: 32px;
  text-align: center;
  color: ${({ colorScheme }) => colorScheme.color};
  border: solid;
  border-color: ${({ colorScheme }) => colorScheme.border};
  border-width: .1rem;
  padding: 0 24px 1px;
  position:relative;
  z-index: 1;
  letter-spacing: ${({ colorScheme }) => colorScheme.letterSpacing};

  ${({ variant, disabled }) => (variant === 'primary' || variant === 'primarySmall') && (!disabled
  && `
  border: none;
  &::after{
    position: absolute;
    border-radius: 32px;
    border-color: transparent;
    width: 0%;
    content: "";
    top: 0;
    right: 0;
    bottom: 0;
    background-image: linear-gradient(104deg, rgba(76,40,154, 0.9), rgba(0, 179, 191, 0.9));
    transition: ease-in-out 0.3s;
    z-index: -1;
    opacity: 0;
  }
  &:hover::after{
    width: 100%;
    opacity: 1;
  }
  &:active::after{
    opacity: 0;
  }
  `
  )}

  &:hover{
    ${({ colorScheme, disabled, variant }) => (
    variant !== 'primary' && variant !== 'primarySmall' && !disabled && `background: ${colorScheme.hover}`
  )};
    ${({ variant, colorScheme, disabled }) => (
    (variant === 'primary' || variant === 'primarySmall') && !disabled
    && `border: transparent; background: ${colorScheme.active}`
  )};
  }

  &:active{
    background: ${({ colorScheme }) => colorScheme.active};
    color: ${({ colorScheme }) => colorScheme.background};
    border: ${({ colorScheme }) => colorScheme.active};
  }

  &:disabled{
    cursor: default;
    opacity: 0.3;
  }

`;
const ButtonWrapper = styled.button`
  cursor: pointer;
  background: ${({ colorScheme }) => colorScheme.background};
  font-size: ${({ colorScheme }) => colorScheme.fontSize};
  line-height: 1.7rem;
  height: ${({ colorScheme }) => colorScheme.height};
  border-radius: 32px;
  text-align: center;
  color: ${({ colorScheme }) => colorScheme.color};
  border: solid;
  border-color: ${({ colorScheme }) => colorScheme.border};
  border-width: .1rem;
  padding: 0 24px 1px;
  position:relative;
  z-index: 1;
  letter-spacing: ${({ colorScheme }) => colorScheme.letterSpacing};

  ${({ variant, disabled }) => (variant === 'primary' || variant === 'primarySmall') && (!disabled
  && `
  border: none;
  &::after{
    position: absolute;
    border-radius: 32px;
    border-color: transparent;
    width: 0%;
    content: "";
    top: 0;
    right: 0;
    bottom: 0;
    background-image: linear-gradient(104deg, rgba(76,40,154, 0.9), rgba(0, 179, 191, 0.9));
    transition: ease-in-out 0.3s;
    z-index: -1;
    opacity: 0;
  }
  &:hover::after{
    width: 100%;
    opacity: 1;
  }
  &:active::after{
    opacity: 0;
  }
  `
  )}

  &:hover{
    ${({ colorScheme, disabled, variant }) => (
    variant !== 'primary' && variant !== 'primarySmall' && !disabled && `background: ${colorScheme.hover}`
  )};
    ${({ variant, colorScheme, disabled }) => (
    (variant === 'primary' || variant === 'primarySmall') && !disabled
    && `border: transparent; background: ${colorScheme.active}`
  )};
  }

  &:active{
    background: ${({ colorScheme }) => colorScheme.active};
    color: ${({ colorScheme }) => colorScheme.background};
    border: ${({ colorScheme }) => colorScheme.active};
  }

  &:disabled{
    cursor: default;
    opacity: 0.3;
  }
`;


const TertiaryButton = styled.a`
cursor: pointer;
font-size: 14px;
font-weight: bold;
line-height: normal;
width: fit-content;
letter-spacing: 0.18px;
color: ${({ colorScheme }) => colorScheme.color};
text-decoration: none;
text-transform: uppercase;

&::after {
  border: solid 1px ${({ colorScheme }) => colorScheme.color};
  background-color: ${({ colorScheme }) => colorScheme.color};
  bottom: -2px;
  content: "";
  width: 50%;
  display: block;
  height: 1px;
  position: relative;
  transition: width 0.5s;
  border-top-right-radius: 1px;
  border-bottom-right-radius: 1px;
}
&:hover::after {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
  width: 98.5%;
}

&:active {
  &:after{
    background-color: ${({ colorScheme }) => colorScheme.active};
  }
  color: ${({ colorScheme }) => colorScheme.active};
}
`;

const SecondaryXS = styled.button`
  cursor: pointer;
  height: 32px;
  border-radius: 20px;
  background-color: transparent;
  border-width: 1px;
  border-color: ${({ colorScheme }) => colorScheme.border};
  color: ${({ colorScheme }) => colorScheme.border};
  font-size: 14px;
  padding: 0px 16px 1px;
  letter-spacing: 0.18px;

  &:hover {
    ${({ colorScheme, disabled }) => !disabled && `background: ${colorScheme.hover}`};
  }

  &:active{
    color: ${({ colorScheme }) => colorScheme.background};
    background: ${({ colorScheme }) => colorScheme.active};
    border: ${({ colorScheme }) => colorScheme.active};
  }


  &:disabled{
    opacity: 0.3;
  }
`;

/**
 * Call to Action Button
 */

const Button = ({
  action,
  children,
  variant,
  disabled,
  href,
  className,
  type,
  target,
  ...props
}: Props) => {
  let colorScheme;
  switch (variant) {
    case 'primary':
      colorScheme = ApoliticalBrand.buttons.primary;
      break;
    case 'primarySmall':
      colorScheme = ApoliticalBrand.buttons.primarySmall;
      break;
    case 'secondary':
      colorScheme = ApoliticalBrand.buttons.secondary;
      break;
    case 'onBackground':
      colorScheme = ApoliticalBrand.buttons.onBackground;
      break;
    case 'tertiary':
      colorScheme = ApoliticalBrand.buttons.tertiary;
      break;
    case 'secondaryXS':
      colorScheme = ApoliticalBrand.buttons.secondary;
      break;
    default:
      colorScheme = ApoliticalBrand.buttons.primary;
  }

  const handleClick = (e) => {
    if (action && (href === '' || href === '#')) {
      e.preventDefault();
      action();
    }
  };
  return (
    variant === 'tertiary'
      ? (
        <TertiaryButton
          colorScheme={colorScheme}
          href={href}
          onClick={handleClick}
          className={className}
          {...props}
        >{children}
        </TertiaryButton>
      )
      : (variant === 'secondaryXS'
        ? (
          <SecondaryXS
            colorScheme={colorScheme}
            onClick={handleClick}
            className={className}
            {...props}
          >{children}
          </SecondaryXS>
        )
        : (
          href !== '' && href !== undefined ? (
            <ButtonAsLink
              variant={variant}
              onKeyPress={action}
              colorScheme={colorScheme}
              disabled={disabled}
              className={className}
              type={type}
              href={href}
              target={target}
              rel="noopener"
              {...props}
            >

              {children}

            </ButtonAsLink>
          )
            : (type === 'submit' ? (
              <ButtonWrapper
                variant={variant}
                onKeyPress={action}
                colorScheme={colorScheme}
                disabled={disabled}
                className={className}
                type={type}
                {...props}
              >

                {children}

              </ButtonWrapper>
            ) : (
              <ButtonWrapper
                onClick={handleClick}
                variant={variant}
                onKeyPress={action}
                colorScheme={colorScheme}
                disabled={disabled}
                className={className}
                type={type}
                {...props}
              >

                {children}

              </ButtonWrapper>
            )
            )

        )
      )
  );
};

Button.defaultProps = {
  action() {},
  variant: 'primary',
  disabled: false,
  href: '',
  className: '',
  type: 'button',
  target: '_self',
};

export default Button;
