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

export type NumberInputProps = {
  value: number
  name: string
  label?: string
  placeholder?: string
  /**
   * schema for validation
   */
  jsonSchema?: IJsonSchema
  /**
   * custom styling for number input
   */
  css?: string
  /**
   * function that receives the updated value
   */
  onChange: (change: number ) => void
}

export const NumberInput = ({
  value,
  name,
  label,
  placeholder,
  jsonSchema,
  onChange,
  css,
}: NumberInputProps):JSX.Element => {

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

  return (
    <InputWrapper 
      label={label} 
      jsonSchema={jsonSchema} 
      value={value}>
      <StyledNumberInput
        type="number"
        value={value}
        name={name}
        placeholder={placeholder}
        css={css}
        onChange={handleChange}
      />
    </InputWrapper>
  )
}

const StyledNumberInput = styled.input<{css?:string}>`
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
    height: 36px;
    -webkit-tap-highlight-color: transparent;
    line-height: 34px;
    appearance: none;
    resize: none;
    box-sizing: border-box;
      font-size: 1rem;
    color: #000;
    text-align: left;
    min-height: 36px;
    padding-left: 5px;
    border-radius: 4px;
    border: none;
    background-color: #ffffff;
    transition: border-color 100ms ease 0s;
    width:100%;
   
    &:focus {
      outline: none;
      border-color: #228be6;
    }

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