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

import { Input, Textarea } from "../../base";

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

const backgroundColor = (props) => {
  if (props.disabled || props.readonly) {
    return css`
      background-color: ${getColor(
        props,
        "formControl",
        "backgroundColorDisabledReadonly"
      )};
      &:focus {
        background-color: ${getColor(
          props,
          "formControl",
          "backgroundColorDisabledReadonly"
        )};
      }
    `;
  } 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, "formControl", "background")};
    &:focus {
      background-color: ${getColor(props, "formControl", "background")};
    }
  `;
};

const border = (props) => {
  if (props.valid) {
    return css`
      border-color: ${getColor(props, "formControl", "borderValid")};
      &:focus {
        border-color: ${getColor(props, "formControl", "borderValid")};
      }
    `;
  } else if (props.invalid) {
    return css`
      border-color: ${getColor(props, "formControl", "borderInvalid")};
      &:focus {
        border-color: ${getColor(props, "formControl", "borderInvalid")};
      }
    `;
  } else if (props.loading) {
    return css`
      border: none;
    `;
  }

  return css`
    border: ${getBorder(props, "formControl", "default")}
      ${getColor(props, "formControl", "border")};
    &:focus {
      border-color: ${getColor(props, "formControl", "borderFocus")};
    }
  `;
};

const boxShadow = (props) => {
  if (props.valid) {
    return css`
      &:focus {
        box-shadow: ${getBoxShadow(props, "formControl", "default")}
          ${getColor(props, "formControl", "formControlBoxShadowValid")};
      }
    `;
  } else if (props.invalid) {
    return css`
      &:focus {
        box-shadow: ${getBoxShadow(props, "formControl", "default")}
          ${getColor(props, "formControl", "formControlBoxShadowInvalid")};
      }
    `;
  }

  return css`
    &:focus {
      box-shadow: 0;
    }
  `;
};

const opacity = (props) => {
  if (props.disabled || props.readonly || props.loading) {
    return css`
      opacity: 1;
      &:focus {
        opacity: 1;
      }
    `;
  }

  return "";
};

const formControlFormInline = (props) =>
  props.formInline &&
  css`
    @media (min-width: ${getConcreteBreakpointSize(props, "sm")}) {
      display: inline-flex;
      width: auto;
      vertical-align: middle;
    } ;
  `;

const padding = (props) => {
  if (props.lg) {
    return css`
      padding-right: ${getPadding(props, "formControl", "lgRight")};
      padding-left: ${getPadding(props, "formControl", "lgLeft")};
      padding: ${getPadding(props, "formControl", "lgPadding")};
    `;
  } else if (props.sm) {
    return css`
      padding-right: ${getPadding(props, "formControl", "smRight")};
      padding-left: ${getPadding(props, "formControl", "smLeft")};
      padding: ${getPadding(props, "formControl", "smPadding")};
    `;
  } else if (props.select) {
    return css`
      padding: ${getPadding(props, "formControl", "selectPadding")};
    `;
  }

  return css`
    padding: ${getPadding(props, "formControl", "defaultPadding")};
  `;
};

const fontSize = (props) => {
  if (props.lg) {
    return css`
      font-size: ${getFontSize(props, "formControl", "lg")};
    `;
  } else if (props.sm) {
    return css`
      font-size: ${getFontSize(props, "formControl", "sm")};
    `;
  }

  return css`
    font-size: ${getFontSize(props, "formControl", "default")};
  `;
};

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

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

const height = (props) => {
  if (props.lg && !props.multiple && props.select) {
    return css`
      height: ${getHeight(props, "formControl", "selectLg")};
    `;
  } else if (props.sm && !props.multiple && props.select) {
    return css`
      height: ${getHeight(props, "formControl", "selectSm")};
    `;
  } else if (!props.multiple && props.select) {
    return css`
      height: ${getHeight(props, "formControl", "select")};
    `;
  }

  return "";
};

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

  return css`
    color: ${(props) => getColor(props, "formControl", "color")};
  `;
};

const placeholder = (props) => {
  if (props.loading) {
    return css`
      color: transparent;
    `;
  }

  return css`
    color: ${(props) => getColor(props, "formControl", "placeholder")};
    opacity: 1;
  `;
};

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

  return css``;
};

const formControlStyle = () => css`
  display: flex;
  flex-wrap: wrap;
  width: 100%;
  line-height: 1.5;
  font-family: Montserrat;
  background-clip: padding-box;
  transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
  box-sizing: border-box;
  margin-bottom: ${(props) => getMargin(props, "formControl", "bottom")}
  &::-ms-expand {
    background-color: transparent;
    border: 0;
  }
  &:focus {
    color: ${(props) => getColor(props, "formControl", "color")};
    outline: 0;
  }
  &::placeholder {
    ${(props) => placeholder(props)}
  }
  ${(props) => cursor(props)}
  ${(props) => color(props)}
  ${(props) => backgroundColor(props)}
  ${(props) => border(props)}
    ${(props) => boxShadow(props)}
    ${(props) => opacity(props)}
    ${(props) => formControlFormInline(props)}
    ${(props) => padding(props)}
    ${(props) => fontSize(props)}
    ${(props) => borderRadius(props)}
    ${(props) => height(props)}
`;

const CustomSelect = () => css`
  .Select__control {
    display: flex;
    flex-wrap: wrap;
    width: 100%;
    line-height: 1.5;
    font-family: Montserrat;
    cursor: pointer;
    ${(props) => borderRadius(props)}
    ${(props) => backgroundColor(props)}
    ${(props) => border(props)}
    ${(props) => boxShadow(props)}
    ${(props) => color(props)}
    ${(props) => opacity(props)}
    background-image: url("data:image/svg+xml,%3Csvg width='512' height='512' viewBox='0 0 512 512' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill-rule='evenodd' clip-rule='evenodd' d='M472 160.006C472 166.15 469.648 172.293 464.968 176.973L272.971 368.971C263.587 378.355 248.419 378.355 239.035 368.971L47.0379 176.973C37.654 167.59 37.654 152.422 47.0379 143.038C56.4218 133.654 71.5896 133.654 80.9734 143.038L256.003 318.067L431.033 143.038C440.416 133.654 455.584 133.654 464.968 143.038C469.648 147.718 472 153.862 472 160.006Z' fill='%236E7191'/%3E%3C/svg%3E");
    background-repeat: no-repeat, repeat;
    background-position: right 0.75rem top 50%, 0 0;
    background-size: 1.5rem auto, 100%;
  }

  .Select__value-container {
    ${(props) => padding(props)}
    ${(props) => height(props)}
    & .Select__placeholder {
      font-size: 0.875rem;
    }
    &. Select__input {
      vertical-align: top;
    }
  }

  .Select__control:hover {
    border-color: transparent;
  }

  .Select__control--is-focused {
    box-shadow: 0 0 0 1px black;
    outline: none;
  }

  .Select__indicators {
    display: none;
  }

  .Select__menu {
    ${(props) => color(props)}
    ${(props) => borderRadius(props)}
    ${(props) => backgroundColor(props)}
    ${(props) => border(props)}
    ${(props) => boxShadow(props)}
    height: auto;
  }

  .Select__option {
    ${(props) => padding(props)}
    ${(props) => color(props)}
    ${(props) => backgroundColor(props)}
    margin-top: ${(props) => getMargin(props, "formControl", "select")};
    margin-bottom: ${(props) => getMargin(props, "formControl", "select")};
    cursor: pointer;
  }
  .Select__option--is-focused {
    color: ${(props) => getColor(props, "formControl", "select")};
    ${(props) => backgroundColor(props)}
  }
  .Select__option--is-selected {
    color: ${(props) => getColor(props, "formControl", "select")};
    ${(props) => backgroundColor(props)}
  }

  .Select__placeholder {
    ${(props) => color(props)}
  }
`;

const FormControlInput = styled(Input)`
  ${formControlStyle};
`;

const FormControlTextarea = styled(Textarea)`
  ${formControlStyle};
`;

const FormControlSelect = styled(Select)`
  ${CustomSelect};
`;

const FormControl = (props) => {
  if (props.textarea) {
    return <FormControlTextarea {...props} />;
  } else if (props.select) {
    return <FormControlSelect classNamePrefix="Select" {...props} />;
  }

  return <FormControlInput {...props} />;
};

FormControl.defaultProps = {
  theme,
};

export { FormControl };
