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

import { Div } from '../../base';

import {
  theme,
  getColor,
  getBorderRadius,
  getBoxShadow,
} from '../../styled-config';

const borderRadius = (props) => {
  if (props.noRadius) {
    return css`
      border-radius: ${getBorderRadius(props, 'card', 'noRadius')};
    `;
  }

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

const boxShadow = (props) => {
  return css`
    box-shadow: ${getBoxShadow(props, 'card', 'default')} ${(props) => getColor(props, 'card', 'shadowColor')};
  `;
};

const background = (props) => {
  if (props.background) {
    return css`
    background-color: ${props.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;
        }
      }
    `;
  }

  return css`
  background-color: ${(props) => getColor(props, 'card', 'backgroundColor')};
  `;
};

const Card = styled(Div)`
  height: 100%;
  position: relative;
  font-family: Montserrat;
  display: flex;
  flex-direction: column;
  min-width: 0;
  word-wrap: break-word;
  background-clip: border-box;
  ${(props) => boxShadow(props)}
  ${(props) => borderRadius(props)}
  ${(props) => background(props)}
`;

Card.defaultProps = {
  theme,
};

export { Card };
