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

import { Sizes, Colors, TextHelper } from '../index';
import { IProps } from './Textarea';

const bounceUp = keyframes`
  from { transform: scale3d(1, 1, 1); }
  50% { transform: scale3d(1.1618, 1.1618, 1.1618); }
  to { transform: scale3d(1, 1, 1); }
`;

const shake = keyframes`
  from, to { transform: translate3d(0, 0, 0); }
  10%, 30%, 50%, 70%, 90% { transform: translate3d(-3px, 0, 0); }
  20%, 40%, 60%, 80% { transform: translate3d(3px, 0, 0); }
`;

const bounceUpRule = css`${bounceUp} .4s forwards;`;

const shakeRule = (props: any) => css`${props.isInvalid && shake} 1s forwards;`;

const GroupInput = styled.div`
  position: relative;
  height: 75px;
  @media screen and (max-width: 600px) {
    width: 100%;
  }

`;

const opacityAndTransition = (opacity: number, transition: string) => `
  opacity: ${opacity};
  transition: ${transition};
`;

const Textarea = styled.textarea<IProps>`
  position: relative;
  font-weight: 500;
  color: ${Colors.codGray};
  width: 100%;
  box-sizing: border-box;
  letter-spacing: 1px;
  border: 0;
  padding: 4px 0;
  border-bottom: 1px solid currentColor;
  background-color: transparent;
  font-family: ${TextHelper.fontVariant('medium')};
  resize: none;
  line-height: 22px;
  overflow: auto;

  &:disabled {
    opacity: 0.5;
    border-bottom-color: ${Colors.line};
  }

  &:not(:placeholder-shown) ~ label {
    top: -16px;
    color: ${Colors.support};
    z-index: 1; 
    ${opacityAndTransition(1, '0.3s')};
  }

  ::placeholder {
    color: ${Colors.support};
    ${opacityAndTransition(1, '0.2s')};
    font-family: ${TextHelper.fontVariant('medium')};
  }

  &:focus::placeholder {
    ${opacityAndTransition(0, '0.2s')};
  }

  &:focus {
    outline: none;
    background-color: #f0f0f0;

    & ~ .bar {
      width: 100%;
      transition: 0.4s;
    }

    & ~ label {
      top: -16px;
      color: ${Colors.lochmara};
      opacity: 1;
      z-index: 1;
      animation: ${bounceUpRule};
    }
  }

  & ~ .bar {
    position: relative;
    display: inherit;
    top: -5px;
    width: 0;
    height: 2px;
    background-color: ${Colors.lochmara};
    transition: 0.4s;
  }

  ~ label {
    position: absolute;
    font-size: ${TextHelper.pxToEm(Sizes.s2)}em;
    font-family: ${TextHelper.fontVariant('medium')};
    font-weight: 500;
    left: 0;
    top: -16px;
    z-index: -1;
    letter-spacing: 0.5px;
    ${opacityAndTransition(0, 'all .2s ease')};
    animation: ${shakeRule};
    text-transform: uppercase;
  }

  ~ .feedback {
    position: absolute;
    font-size: ${TextHelper.pxToEm(Sizes.s2)}em;
    font-family: ${TextHelper.fontVariant('medium')};
    overflow: hidden;
    font-style: italic;

    ${({comment}) => comment && `
      font-style: italic;
    `}
  }

  ${({ isInvalid, isWarning }) => (isInvalid || isWarning) && `
    border-color: ${isInvalid ? Colors.chestnut : Colors.goldDrop};
    ~ .feedback,
    ~ label,
    &:focus ~ label, &:not(:placeholder-shown) ~ label {
      color: ${isInvalid ? Colors.chestnut : Colors.goldDrop}
    }

    ~ label { opacity: 1; }
  `}
`;

export default {
  Textarea,
  GroupInput,
};
