import React from "react";

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

import { Div, Span } from "../../base";
import { CheckIcon, CloseIcon, DragIcon } from "../../icon";

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

const boxShadow = (props) =>
  css`
    box-shadow: ${getBoxShadow(props, "dropContainer", "default")}
      ${(props) => getColor(props, "dropContainer", "boxShadowColor")};
  `;

const padding = (props) => {
  if (props.sm) {
    return css`
      padding: ${getPadding(props, "dropContainer", "sm")};
      padding-right: ${getPadding(props, "dropContainer", "rp")};
      padding-left: ${getPadding(props, "dropContainer", "default")};
    `;
  }

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

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

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

  return css`
    border: 1px solid ${getColor(props, "dropContainer", "borderColor")};
  `;
};

const backgroundColor = (props) => {
  if (props.disabled) {
    return css`
      background-image: none;
      background-color: ${getColor(
        props,
        "dropContainer",
        "backgroundColorDisabled"
      )};
    `;
  } else if (props.loading) {
    return css`
      width: auto;
      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-flex;
      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, "dropContainer", "backgroundColor")};
    &:focus,
    &:hover {
      background-color: ${getColor(
        props,
        "dropContainer",
        "backgroundColorHoverFocus"
      )};
    }
  `;
};

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

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

const color = (props) => {
  if (props.loading) {
    return css`
      color: ${getColor(props, "dropContainer", "color")};
    `;
  }

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

const cursor = (props) => {
  if (props.loading) {
    return css`
      cursor: wait;
    `;
  } else if (props.disabled) {
    return css`
      cursor: no-drop;
    `;
  }

  return css`
    cursor: move;
  `;
};

const buttonStyle = (props) => css`
  font-weight: 600;
  line-height: 0.875rem;
  text-align: center;
  font-family: Montserrat;
  text-transform: none;
  vertical-align: middle;
  user-select: none;
  display: inline-flex;
  flex-direction: row;
  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;
  &:hover,
  &:focus {
    outline: 0;
    ${boxShadow}
    text-decoration: none;
    ${border(props)};
    ${backgroundColor(props)};
    ${color(props)};
  }
  ${cursor(props)};
  ${border(props)};
  ${backgroundColor(props)};
  ${borderRadius(props)};
  ${fontSize(props)};
  ${color(props)};
  ${padding(props)};
  ${boxShadow(props)};
`;

const DragDropContainer = styled(Div)`
  ${(props) => buttonStyle(props)};
`;

const DropContainer = (props) => {
  if (props.success) {
    return (
      <DragDropContainer disabled {...props}>
        <CheckIcon width={16} height={16} fill="#14AE85" />
        <Span ml2 textCenter>
          {props.text}
        </Span>
      </DragDropContainer>
    );
  } else if (props.warning) {
    return (
      <DragDropContainer disabled {...props}>
        <CloseIcon width={16} height={16} fill="#FA8700" />
        <Span ml2 textCenter>
          {props.text}
        </Span>
      </DragDropContainer>
    );
  } else if (props.loading) {
    return (
      <DragDropContainer loading {...props}>
        <Div ml9 mr9 mt4 mb4 />
      </DragDropContainer>
    );
  }
  return (
    <DragDropContainer {...props}>
      <DragIcon width={16} height={16} fill="#A0A3BD" />
      <Span ml2 textCenter>
        {props.text}
      </Span>
    </DragDropContainer>
  );
};

DropContainer.defaultProps = {
  theme,
};

export { DropContainer };
