import styled from 'styled-components'
import React from 'react'
import { InputWrapper } from '../InputWrapper/InputWrapper'
import { IJsonSchema } from '../../types/IJsonSchema'

export type ToggleProps = {
  label?: string
  value?: boolean
  square?: boolean
  sized?: string
  /**
   * custom styling for swtich
   */
  css?: string
  onChange: (change: boolean) => void
  /**
   * schema for validation
   */
  jsonSchema?: IJsonSchema
  name?: string
}

export const Toggle = ({
  label,
  value,
  css,
  onChange,
  jsonSchema,
  name='switch',
  square,
  sized='md',
}: ToggleProps):JSX.Element => {

  const handleChange = (event:React.ChangeEvent<HTMLInputElement>) => {
    onChange(event.target.checked)
  }

  return (
    <InputWrapper 
      label={label} 
      jsonSchema={jsonSchema} 
      value={value}>
      <StyledToggle 
        sized={sized}
        square={square}
        css={css}
        name={name}
        type="checkbox" 
        checked={value}
        onChange={handleChange}
      />
    </InputWrapper>
  )
}

const handleSize = (size:string,element:string,property:string) => { 
  // This function controls the size of the component, using the 'size' prop. 'xm', 'sm', 'md'(default), 'lg', 'xl'

  // md (default)
  if (size === 'md' || !size) {
    if (element === 'main') 
      switch (property) {
        case 'width':
          return '46px'
        case 'height':
          return '24px'
        case 'max-width':
          return '46px'
      }
    
    if (element === 'before') 
      switch (property) {
        case 'width':
          return '18px'
        case 'height':
          return '18px'
      }
    
    if (element === 'before:checked') 
      switch (property) {
        case 'transform':
          return 'translate(24px)'
      }
    
  }
  // xs
  if (size === 'xs') {
    if (element === 'main') 
      switch (property) {
        case 'width':
          return '30px'
        case 'height':
          return '16px'
        case 'max-width':
          return '30px'
      }
    
    if (element === 'before') 
      switch (property) {
        case 'width':
          return '12px'
        case 'height':
          return '12px'
      }
    
    if (element === 'before:checked') 
      switch (property) {
        case 'transform':
          return 'translate(15px)'
      }
    
  }
  // sm
  if (size === 'sm') {
    if (element === 'main') 
      switch (property) {
        case 'width':
          return '38px'
        case 'height':
          return '20px'
        case 'max-width':
          return '38px'
      }
    
    if (element === 'before') 
      switch (property) {
        case 'width':
          return '14px'
        case 'height':
          return '14px'

      }
    
    if (element === 'before:checked') 
      switch (property) {
        case 'transform':
          return 'translate(20px)'
      }
    
  }
  // lg
  if (size === 'lg') {
    if (element === 'main') 
      switch (property) {
        case 'width':
          return '56px'
        case 'height':
          return '30px'
        case 'max-width':
          return '56px'
      }
    
    if (element === 'before') 
      switch (property) {
        case 'width':
          return '22px'
        case 'height':
          return '22px'
      }
    
    if (element === 'before:checked') 
      switch (property) {
        case 'transform':
          return 'translate(30px)'
      }
    
  }
  // xl
  if (size === 'xl') {
    if (element === 'main') 
      switch (property) {
        case 'width':
          return '68px'
        case 'height':
          return '36px'
        case 'max-width':
          return '68px'
      }
    
    if (element === 'before') 
      switch (property) {
        case 'width':
          return '28px'
        case 'height':
          return '28px'
      }
    
    if (element === 'before:checked') 
      switch (property) {
        case 'transform':
          return 'translate(36px)'
      }
    
  }
}

const StyledToggle = styled.input<{sized?:string,square?:boolean,css?:string}>` 
  box-sizing: border-box;
  overflow: hidden;
  position: relative;
  border-radius: ${({ square }) => square ? '0px': '32px'};
  border: 1px solid #dee2e6;
  height: ${({ sized })=> sized && handleSize(sized,'main','height')};
  width: ${({ sized })=> sized && handleSize(sized,'main','width')};
  min-width: ${({ sized })=> sized && handleSize(sized,'main','max-width')};
  margin: 0px;
  transition-property: background-color, border-color;
  transition-timing-function: ease;
  transition-duration: 150ms;
  box-sizing: border-box;
  appearance: none;
  display: flex;
  -webkit-box-align: center;
  align-items: center;
  font-size: 0.5rem;
  font-weight: 600;
  cursor: default;
  background-color:lightgray;

  &:checked {
    box-sizing: border-box;
    background-color: #228be6;
  }
  &:focus:not(:focus-visible) {
    box-sizing: border-box;
    outline: none;
  }
  &:focus {
    box-sizing: border-box;
    outline-offset: 2px;
    outline: #000 solid 2px;
  }
  &:before {
    z-index: 1;
    border-radius: ${({ square }) => square ? '0px': '32px'};
    box-sizing: border-box;
    content: "";
    display: block;
    background-color: #ffffff;
    height: ${({ sized })=> sized && handleSize(sized,'before','height')};
    width: ${({ sized })=> sized && handleSize(sized,'before','width')};
    border: 1px solid #dee2e6;
    transition: transform 150ms ease 0s;
    transform: translateX(1px);
  }
  &:checked::before {
    box-sizing: border-box;
    transform: ${({ sized })=>sized && handleSize(sized,'before:checked','transform')};
    border-color: #ffffff;
  }
  &:after {
    box-sizing: border-box;
    position: absolute;
    z-index: 0;
    display: flex;
    height: 100%;
    -webkit-box-align: center;
    align-items: center;
    line-height: 0;
    right: 10%;
    transform: translateX(0px);
    content: "";
    color:#868e96;
    transition: color 150ms ease 0s;
  }
  &:checked::after {
    box-sizing: border-box;
    position: absolute;
    z-index: 0;
    display: flex;
    height: 100%;
    -webkit-box-align: center;
    align-items: center;
    line-height: 0;
    left: 10%;
    transform: translateX(0px);
    content: "";
    color: #ffffff;
    transition: color 150ms ease 0s;
  }

  ${(p) => p.css}
`




