import * as React from 'react'
import styled from 'styled-components'

// TODO implement web-client text inputs!

export enum MultilineTextInputSize {
  SMALL = 'small',
  MEDIUM = 'medium',
  LARGE = 'large',
}

const MIN_HEIGHTS: Record<MultilineTextInputSize, string | number> = {
  [MultilineTextInputSize.SMALL]: '2.5rem',
  [MultilineTextInputSize.MEDIUM]: '3rem',
  [MultilineTextInputSize.LARGE]: '4rem',
}

const LABEL_VERTICAL_PADDING_SIZES: Record<MultilineTextInputSize, string | number> = {
  [MultilineTextInputSize.SMALL]: '0.125rem',
  [MultilineTextInputSize.MEDIUM]: '0.25rem',
  [MultilineTextInputSize.LARGE]: '0.375rem',
}

const VERTICAL_PADDING_SIZES: Record<MultilineTextInputSize, string | number> = {
  [MultilineTextInputSize.SMALL]: '0.6rem',
  [MultilineTextInputSize.MEDIUM]: '0.85rem',
  [MultilineTextInputSize.LARGE]: '1.25rem',
}

const INPUT_FONT_SIZES: Record<MultilineTextInputSize, string | number> = {
  [MultilineTextInputSize.SMALL]: '0.85em',
  [MultilineTextInputSize.MEDIUM]: '0.85em',
  [MultilineTextInputSize.LARGE]: '1em',
}

const SECONDARY_TEXT_SIZES: Record<MultilineTextInputSize, string | number> = {
  [MultilineTextInputSize.SMALL]: '0.65em',
  [MultilineTextInputSize.MEDIUM]: '0.75em',
  [MultilineTextInputSize.LARGE]: '0.85em',
}

const ComponentBase = styled('div')({
  verticalAlign: 'middle',
  position: 'relative',
  borderRadius: '0.25rem',
  fontFamily: 'var(--font-family-base, sans-serif)',
  maxWidth: '100%',
  ':focus-within': {
    '--color-accent': 'var(--color-active, Highlight)',
  },
  'fieldset[disabled] &': {
    opacity: 0.5,
  },
})

ComponentBase.displayName = 'div'

const CaptureArea = styled('label')({
  display: 'block',
  borderRadius: 'inherit',
  overflow: 'hidden',
  boxSizing: 'border-box',
})

CaptureArea.displayName = 'label'

const LabelWrapper = styled('span')({
  color: 'var(--color-accent, blue)',
  boxSizing: 'border-box',
  position: 'absolute',
  top: 0,
  left: 0,
  fontSize: '0.85em',
  maxWidth: '100%',
  width: '100%',
  overflow: 'hidden',
  textOverflow: 'ellipsis',
  whiteSpace: 'nowrap',
  fontWeight: 'bolder',
  zIndex: 2,
  pointerEvents: 'none',
  transitionProperty: 'color',
  lineHeight: 1,
  userSelect: 'none',
})

LabelWrapper.displayName = 'span'

const Border = styled('span')({
  borderColor: 'var(--color-accent, blue)',
  boxSizing: 'border-box',
  display: 'inline-block',
  borderWidth: '0.125rem',
  borderStyle: 'solid',
  position: 'absolute',
  top: 0,
  left: 0,
  width: '100%',
  height: '100%',
  borderRadius: 'inherit',
  zIndex: 3,
  pointerEvents: 'none',
  transitionProperty: 'border-color',
  '::before': {
    position: 'absolute',
    top: 0,
    left: 0,
    width: '100%',
    height: '100%',
    content: "''",
    borderRadius: '0.125rem',
    opacity: 0.5,
    pointerEvents: 'none',
  },
  [`${ComponentBase}:focus-within &::before`]: {
    boxShadow: '0 0 0 0.375rem var(--color-accent, blue)',
  },
})

const TextArea = styled('textarea')({
  display: 'block',
  verticalAlign: 'top',
  width: '100%',
  height: '100%',
  boxSizing: 'border-box',
  position: 'relative',
  border: 0,
  borderRadius: 'inherit',
  paddingTop: 0,
  paddingBottom: 0,
  margin: 0,
  font: 'inherit',
  minWidth: '3rem',
  maxWidth: '100%',
  zIndex: 1,
  transitionProperty: 'background-color, color',
  resize: 'none',
  lineHeight: 'normal',
  ':focus': {
    outline: 0,
    color: 'var(--color-fg, black)',
  },
  ':disabled': {
    cursor: 'not-allowed',
  },
  '@media only screen': {
    backgroundColor: 'var(--color-bg, white)',
    color: 'var(--color-fg, black)',
  },
})

TextArea.displayName = 'textarea'

const HintWrapper = styled('span')({
  boxSizing: 'border-box',
  position: 'absolute',
  left: 0,
  fontSize: '0.85em',
  maxWidth: '100%',
  overflow: 'hidden',
  textOverflow: 'ellipsis',
  whiteSpace: 'nowrap',
  zIndex: 2,
  pointerEvents: 'none',
  userSelect: 'none',
  width: '100%',
})

const HintContent = styled('span')({
  display: 'block',
  opacity: 0.5,
})

const IndicatorWrapper = styled('span')({
  color: 'var(--color-accent, blue)',
  boxSizing: 'border-box',
  position: 'absolute',
  bottom: 0,
  right: 0,
  display: 'grid',
  placeContent: 'center',
  padding: '0 1rem',
  zIndex: 1,
  pointerEvents: 'none',
  transitionProperty: 'color',
  lineHeight: 1,
  userSelect: 'none',
})

export type Props = Omit<React.HTMLProps<HTMLTextAreaElement>, 'as' | 'className' | 'style' | 'placeholder' | 'size'> & {
  /**
   * Short textual description indicating the nature of the component's value.
   */
  label?: React.ReactNode,
  /**
   * Short textual description as guidelines for valid input values.
   */
  hint?: React.ReactNode,
  /**
   * Size of the component.
   */
  size?: MultilineTextInputSize,
  /**
   * Additional description, usually graphical, indicating the nature of the component's value.
   */
  indicator?: React.ReactNode,
  /**
   * Should the component display a border?
   */
  border?: boolean,
  /**
   * Should the component be displayed with an alternate appearance?
   */
  alternate?: boolean,
  /**
   * Should the component occupy the whole width of its parent?
   */
  block?: boolean,
  /**
   * How many rows should the component display if it accepts multiline input?
   */
  rows?: number,
  /**
   * Should the component resize itself to show all its value?
   */
  autoResize?: boolean,
}

/**
 * Component for inputting textual values.
 *
 * This component supports multiline input and adjusts its layout accordingly.
 */
export const MultilineTextInput = React.forwardRef<HTMLTextAreaElement, Props>(
  (
    {
      label = '',
      hint = '',
      size = MultilineTextInputSize.MEDIUM,
      indicator = null,
      border = false,
      alternate = false,
      block = false,
      disabled = false,
      rows = 3,
      defaultValue,
      value,
      name,
      ...etcProps
    },
    ref,
  ) => (
    <ComponentBase
      style={{
        display: block ? 'block' : 'inline-block',
        opacity: disabled ? 0.5 : undefined,
      }}
    >
      {border && <Border />}
      <CaptureArea
        style={{
          paddingTop: alternate ? `calc(${SECONDARY_TEXT_SIZES[size!]} * 2)` : `calc(${SECONDARY_TEXT_SIZES[size!]} * 1.25)`,
          paddingBottom: alternate ? '0.375rem' : VERTICAL_PADDING_SIZES[size!],
          height: `calc(${MIN_HEIGHTS[size!]} * ${rows})`,
          minHeight: MIN_HEIGHTS[size!],
          resize: block ? 'vertical' : 'both',
        }}
      >
        {label && (
          <>
            <LabelWrapper
              style={{
                paddingLeft: alternate ? (border ? '0.5rem' : undefined) : '0.5rem',
                paddingTop: alternate ? `calc(${LABEL_VERTICAL_PADDING_SIZES[size!]} * 0.25)` : LABEL_VERTICAL_PADDING_SIZES[size!],
                paddingBottom: alternate ? LABEL_VERTICAL_PADDING_SIZES[size!] : undefined,
                paddingRight: indicator ? MIN_HEIGHTS[size!] : (alternate ? (border ? '0.5rem' : undefined) : '0.5rem'),
                fontSize: SECONDARY_TEXT_SIZES[size!],
              }}
            >
              {label}
            </LabelWrapper>
            {' '}
          </>
        )}
        <TextArea
          {...etcProps}
          ref={ref as React.Ref<HTMLTextAreaElement>}
          disabled={disabled}
          style={{
            paddingLeft: alternate ? (border ? '0.5rem' : undefined) : '1rem',
            fontSize: INPUT_FONT_SIZES[size!],
            paddingRight: indicator ? MIN_HEIGHTS[size!] : (alternate ? (border ? '0.5rem' : undefined) : '1rem'),
          }}
          defaultValue={defaultValue}
          value={value}
          name={name}
          rows={rows}
        />
      </CaptureArea>
      {hint && (
        <>
          {' '}
          <HintWrapper
            style={{
              top: alternate ? `calc(${SECONDARY_TEXT_SIZES[size!]} * 0.75)` : undefined,
              bottom: alternate ? undefined : 0,
              paddingLeft: alternate ? (border ? '0.5rem' : undefined) : '1rem',
              paddingTop: alternate ? undefined : LABEL_VERTICAL_PADDING_SIZES[size!],
              paddingBottom:alternate ? undefined : LABEL_VERTICAL_PADDING_SIZES[size!],
              paddingRight: indicator ? MIN_HEIGHTS[size!] : (alternate ? (border ? '0.5rem' : undefined) : '1rem'),
              fontSize: SECONDARY_TEXT_SIZES[size!],
              lineHeight: alternate ? 1.5 : 1,
            }}
          >
            <HintContent>
              {hint}
            </HintContent>
          </HintWrapper>
        </>
      )}
      {indicator && (
        <IndicatorWrapper
          style={{
            width: MIN_HEIGHTS[size!],
            height: MIN_HEIGHTS[size!],
          }}
        >
          {indicator}
        </IndicatorWrapper>
      )}
    </ComponentBase>
  ),
)

MultilineTextInput.displayName = 'MultilineTextInput'
