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

import { Div } from "../../base";
import { PlusCircleIcon } from "../../icon";

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

const display = (props) => {
  if (props.inline) {
    return css`
      display: inline-flex;
    `;
  }

  return css`
    display: flex;
    flex-wrap: wrap;
  `;
};

const backgroundColor = (props) => {
  if (props.info) {
    return css`
      background-color: ${getColor(props, "dropTarget", "background")};
    `;
  } else if (props.loading) {
    return css`
      width: 100%;
      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;
        }
      }
    `;
  } else if (props.pulse) {
    return css`
      background-color: ${getColor(props, "dropTarget", "background")};
      animation: pulse 2s infinite;
      @keyframes pulse {
        0% {
          -moz-box-shadow: 0 0 0 0 rgba(139, 175, 255, 1);
          box-shadow: 0 0 0 0 rgba(139, 175, 255, 1);
        }
        70% {
          -moz-box-shadow: 0 0 0 10px rgba(242, 246, 255, 0);
          box-shadow: 0 0 0 10px rgba(242, 246, 255, 0);
        }
        100% {
          -moz-box-shadow: 0 0 0 0 rgba(242, 246, 255, 0);
          box-shadow: 0 0 0 0 rgba(242, 246, 255, 0);
        }
      }
    `;
  }
  return css`
    background-color: ${getColor(props, "dropTarget", "background")};
  `;
};

const border = (props) => {
  if (props.loading) {
    return css`
      border: 0;
    `;
  }

  return css`
    border: 0.0625rem dashed ${getColor(props, "dropTarget", "border")};
  `;
};

const borderRadius = (props) => {
  return css`
    border-radius: ${getBorderRadius(props, "dropTarget", "default")};
  `;
};

const padding = (props) => {
  if (props.sm) {
    return css`
      padding-top: ${getPadding(props, "dropTarget", "smT")};
      padding-left: ${getPadding(props, "dropTarget", "smL")};
      padding-right: ${getPadding(props, "dropTarget", "smR")};
      padding-bottom: ${getPadding(props, "dropTarget", "smB")};
    `;
  } else if (props.active) {
    return css`
      padding: ${getPadding(props, "dropTarget", "active")};
    `;
  }
  return css`
    padding: ${getPadding(props, "dropTarget", "default")};
  `;
};

const dropTarget = () => css`
  display: flex;
  flex-wrap: wrap;
  max-width: auto;
  max-height: auto;
  height: auto;
  width: auto;
  background-clip: padding-box;
  transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
  box-sizing: border-box;
  box-shadow: 0;
  cursor: pointer;
  ${(props) => backgroundColor(props)}
  ${(props) => border(props)}
  ${(props) => borderRadius(props)}
  ${(props) => display(props)}
  ${(props) => padding(props)}
`;

const DragAndDropTarget = styled(Div)`
  ${dropTarget};
`;

const DropTarget = (props) => {
  if (props.active) {
    return <DragAndDropTarget active {...props} />;
  }
  return (
    <DragAndDropTarget pulse {...props}>
      <PlusCircleIcon width={24} height={24} fill="#165EFE" />
    </DragAndDropTarget>
  );
};

DropTarget.defaultProps = {
  theme,
};

export { DropTarget };
