import React from "react";
import styled from "styled-components";

import {
  theme,
  getColor,
  getWidth,
  getHeight,
  getBoxShadow
} from "../../styled-config";

const ToggleWrapper = styled.div`
  position: relative;
`;

const ToggleLabel = styled.label`
  position: absolute;
  top: 0;
  left: 0;
  width: 42px;
  height: 26px;
  border-radius: 15px;
  background: #bebebe;
  cursor: pointer;
  &::after {
    content: "";
    display: block;
    border-radius: 50%;
    height: ${(props) => getHeight(props, 'toggle', 'default')};
    width:  ${(props) => getWidth(props, 'toggle', 'default')};
    margin: 3px;
    background: #ffffff;
    box-shadow: ${(props) => getBoxShadow(props, 'toggle', 'default')};
    transition: 0.2s;
  }
`;

const ToggleBox = styled.input`
  opacity: 0;
  z-index: 1;
  border-radius: 15px;
  width: 42px;
  height: 26px;
  &:checked + ${ToggleLabel} {
    background: ${(props) => getColor(props, 'toggle', 'primary')};
    &::after {
      content: "";
      display: block;
      border-radius: 50%;
      height: ${(props) => getHeight(props, 'toggle', 'default')};
      width:  ${(props) => getWidth(props, 'toggle', 'default')};
      margin-left: 21px;
      transition: 0.2s;
    }
  }
`;


const Toggle = React.forwardRef(
  ({hover, className, children, ...rest}, ref) => (
    <ToggleWrapper>
      <ToggleBox {...rest} ref={ref} type="checkbox"/>
      <ToggleLabel htmlFor="checkbox"/>
    </ToggleWrapper>
  )
);

Toggle.defaultProps = {
  theme,
};

export {Toggle};
