import styled, { css } from "styled-components";

import { Utilities, Button as BaseButton, A } from "../../base";

import {
  theme,
  getColor,
  getBreakpointSize,
  getBorderRadius,
  getBorder,
  getPadding,
  getMargin,
  getFontSize,
} from "../../styled-config";

const boxShadow = (props) =>
  css`
    box-shadow: 0 0 0 0.2rem ${getColor(props, "button", "boxShadow")};
  `;

const padding = (props) => {
  if (props.split) {
    if (props.sm) {
      return css`
        padding: 0;
        padding-right: ${getPadding(props, "button", "splitSmRight")};
        padding-left: ${getPadding(props, "button", "splitSmLeft")};
        &::after {
          margin-left: 0;
        }
      `;
    } else if (props.lg) {
      return css`
        padding: 0;
        padding-right: ${getPadding(props, "button", "splitLgRight")};
        padding-left: ${getPadding(props, "button", "splitLgLeft")};
        &::after {
          margin-left: 0;
        }
      `;
    }

    return css`
      padding: 0;
      padding-right: ${getPadding(props, "button", "splitDefaultRight")};
      padding-left: ${getPadding(props, "button", "splitDefaultLeft")};
      &::after {
        margin-left: 0;
      }
    `;
  } else if (props.lg) {
    return css`
      padding: ${getPadding(props, "button", "lg")};
    `;
  } else if (props.sm) {
    return css`
      padding: ${getPadding(props, "button", "sm")};
    `;
  }

  return css`
    padding: ${getPadding(props, "button", "default")};
  `;
};

const block = (props) => {
  if (props.block) {
    return css`
      display: block;
      width: 100%;
      & + & {
        margin-top: ${getMargin(props, "button", "blockTop")};
      }
    `;
  }

  return css`
    display: inline-block;
  `;
};

const fontSize = (props) => {
  return css`
    font-size: ${getFontSize(props, "button", "default")};
  `;
};

const dropdownToggle = (props) =>
  props.dropdownToggle &&
  css`
    &::after {
      display: inline-block;
      width: 0;
      height: 0;
      margin-left: ${getMargin(props, "button", "dropdownToggleLeft")};
      vertical-align: 0.255em;
      content: "";
      border-top: 0.3em solid;
      border-right: 0.3em solid transparent;
      border-bottom: 0;
      border-left: 0.3em solid transparent;
    }
    &:empty::after {
      margin-left: 0;
    }
    &::after {
      margin-left: 0.255em;
    }
  `;

const disabled = (props) =>
  props.disabled &&
  css`
    opacity: 0.65;
  `;

const loading = (props) =>
  props.loading &&
  css`
    opacity: 0.8;
  `;

const border = (props) => {
  if (props.active) {
    return css`
      border: ${getBorder(props, "button", "default")}
        ${getColor(props, "button", "borderColorActive")};
    `;
  } else if (props.disabled) {
    return css`
      border: ${getBorder(props, "button", "default")}
        ${getColor(props, "button", "borderColorDisabled")};
    `;
  } else if (props.loading) {
    return css`
      border: none;
    `;
  }

  return css`
    border: 1px solid ${getColor(props, "button", "borderColor")};
    ${!props.outline &&
    css`
      &:focus,
      &:hover {
        border: 1px solid ${getColor(props, "button", "borderColorHoverFocus")};
      }
    `}
  `;
};

const linkColor = (props) => css`
  background-image: none;
  color: ${getColor(props, "button", "colorOutline")};
`;

const backgroundColor = (props) => {
  if (props.active) {
    return css`
      background-image: none;
      background-color: ${getColor(props, "button", "backgroundColorActive")};
    `;
  } else if (props.outline) {
    return css`
      background-image: none;
      background-color: transparent;
      &:hover {
        background-color: ${props.disabled
          ? "transparent"
          : getColor(props, "button", "backgroundColor")};
      }
    `;
  } else if (props.disabled) {
    return css`
      background-image: none;
      background-color: ${getColor(props, "button", "backgroundColorDisabled")};
    `;
  } else if (props.loading) {
    return css`
    box-sizing: border-box;
    background-color: #F6F6F6;
    background-image: linear-gradient(
      90deg,
      #eee,
      #f5f5f5,
      #eee
    );
    background-size: 12.5rem 100%;
    background-repeat: no-repeat;
    display: inline-block;
    line-height: 1;
    animation: skeletonKeyframes 1.2s ease-in-out infinite;
      @keyframes skeletonKeyframes {
        0% {
          background-position: -200px 0;
        }
        100% {
          background-position: calc(200px + 100%) 0;
        }
      }
    `;
  }

  return css`
    background-color: ${getColor(props, "button", "backgroundColor")};
    &:focus,
    &:hover {
      background-color: ${getColor(
        props,
        "button",
        "backgroundColorHoverFocus"
      )};
    }
  `;
};

const borderRadius = (props) => {
  if (props.pill) {
    return css`
      border-radius: ${getBorderRadius(props, "button", "pill")};
    `;
  } else if (props.noRadius) {
    return css`
      border-radius: ${getBorderRadius(props, "button", "noRadius")};
    `;
  } else if (props.sm) {
    return css`
      border-radius: ${getBorderRadius(props, "button", "sm")};
    `;
  } else if (props.lg) {
    return css`
      border-radius: ${getBorderRadius(props, "button", "lg")};
    `;
  }

  return css`
    border-radius: ${getBorderRadius(props, "button", "default")};
  `;
};

const buttonToggler = (props) =>
  css`
    color: ${getColor(props, "button", "toggle", "color")};
    border-color: ${getColor(props, "button", "toggle", "borderColor")};
    &:hover,
    &:focus {
      color: ${getColor(props, "button", "toggle", "color")};
      border-color: ${getColor(props, "button", "toggle", "borderColor")};
    }
  `;

const buttonToggleCollapse = (props) => {
  if (
    (props.expandSm || props.expandMd || props.expandLg || props.expandXl) &&
    props.toggleCollapse
  ) {
    return css`
      display: none;
      @media (max-width: ${getBreakpointSize(props)}) {
        display: block;
        font-size: ${fontSize(props).toggle};
        ${buttonToggler};
      }
    `;
  }

  return "";
};

const color = (props) => {
  if (props.outline) {
    if (props.disabled) {
      return css`
        color: ${getColor(props, "button", "colorOutline")};
      `;
    }

    return css`
      color: ${getColor(props, "button", "colorOutline")};
      &:hover {
        color: ${getColor(props, "button", "colorOutlineHover")};
      }
    `;
  } else if (props.loading) {
    return css`
      color: transparent;
    `;
  }

  return css`
    color: ${getColor(props, "button", "color")};
  `;
};

const cursor = (props) =>
  !props.disabled &&
  css`
    cursor: pointer;
  `;

const cursorLoading = (props) =>
  props.loading &&
  css`
    cursor: progress;
  `;

  const cursorCopy = (props) =>
  props.copy &&
  css`
    cursor: copy;
  `;

const buttonStyle = (props) => css`
  font-weight: 600;
  line-height: 0.875rem;
  text-align: center;
  font-family: Montserrat;
  text-transform: uppercase;
  white-space: wrap;
  vertical-align: middle;
  user-select: none;
  transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out,
    border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
  &:focus {
    outline: 0;
    ${boxShadow};
  }
  &:hover,
  &:focus {
    text-decoration: none;
    ${border(props)};
    ${backgroundColor(props)};
    ${color(props)};
    ${cursor(props)};
    ${cursorLoading(props)};
    ${cursorCopy(props)};
  }
  ${border(props)};
  ${backgroundColor(props)};
  ${borderRadius(props)};
  ${block(props)};
  ${dropdownToggle(props)};
  ${buttonToggleCollapse(props)};
  ${disabled(props)};
  ${loading(props)};
  ${fontSize(props)};
  ${color(props)};
  ${padding(props)};
`;

const Button = styled(BaseButton)`
  ${(props) => buttonStyle(props)};
`;

Button.defaultProps = {
  theme,
};

const linkStyle = (props) => css`
  font-weight: 600;
  line-height: 0.875rem;
  text-align: center;
  font-family: Montserrat;
  text-transform: uppercase;
  white-space: wrap;
  vertical-align: middle;
  user-select: none;
  background-color: transparent;
  border: none;
  cursor: pointer;
  text-decoration: none;
  transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out,
    border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
  ${borderRadius(props)};
  ${block(props)};
  ${disabled(props)};
  ${fontSize(props)};
  ${padding(props)};
  ${linkColor(props)};
  &:hover,
  &:focus {
    text-decoration: none;
    color: ${getColor(props, "button", "backgroundColorHoverFocus")};
    ${cursor(props)};
    ${cursorLoading(props)};
    ${cursorCopy(props)};
  }
  padding: 0.9375rem 0 0.9375rem 0;
`;

const LinkButton = styled(A)`
  ${Utilities};
  ${(props) => linkStyle(props)};
`;

LinkButton.defaultProps = {
  theme,
};

export { Button, LinkButton };
