// @flow
import React, { useState, useRef, useLayoutEffect } from 'react';
import randomstring from 'randomstring';
import styled from 'styled-components';
import type { TextAreaProps } from './text-area.proptypes';
import { Box } from '../../../primitives';
import { FieldLabel } from '../../typography/field-label';
import { HideAccessibly } from '../../../primitives/hide-accessibly';
import { getPaletteColor } from '../../../mixins/branding';
import {
  getFontSize,
  getFontFamily,
  getFontWeight,
} from '../../../mixins/typography';
import {
  FOCUS_VISIBLE_SELECTOR,
  getBaseFocusState,
} from '../../../mixins/focus';
import { getTextColor } from '../internals/form-input/form-input.css-rules';
import { getTextareaHeight, getResizable } from './text-area.css-rules';
import { getSpacingRules } from '../../../mixins/spacing';
import { getStackeeRules } from '../../../mixins/stackee';

const StyledTextarea = styled.textarea.attrs(() => ({
  fontSize: 'f1',
}))`
  width: 100%;
  height: ${getTextareaHeight};
  color: ${getTextColor};
  font-family: ${getFontFamily};
  font-size: ${getFontSize};
  font-weight: ${getFontWeight};
  border-radius: 6px;
  border: 1px solid ${getPaletteColor('greyscale-light')};
  padding: 10px 20px;
  resize: ${getResizable};
  ${getBaseFocusState({
    hoverTransition: false,
    activeTransition: false,
    pointerCursor: false,
  })}
  ${FOCUS_VISIBLE_SELECTOR} {
    border: 1px solid ${getPaletteColor('greyscale-dark')};
  }
  &:not([disabled]):hover {
    border: 1px solid ${getPaletteColor('greyscale-medium')};
  }
`;

const TextAreaContainer = styled(Box)`
  &&& {
    ${getSpacingRules};
  }
  ${getStackeeRules};
`;

export const TextArea = ({
  id = '',
  placeholder,
  label,
  value,
  disabled,
  focusOnMount,
  resize = false,
  hideLabel = false,
  height,
  onChange = (
    _value: string,
    _event: SyntheticInputEvent<HTMLInputElement>,
  ) => {},
  onBlur = () => {},
  onFocus = () => {},
  className,
  spacing,
  grow,
  shrink,
  basis,
  'data-qa': dataQa,
  'data-cy': dataCy,
}: TextAreaProps<string>) => {
  const textareaId = `textarea-${id}-${randomstring.generate(5)}`;
  const [managedValue, setValue] = useState(value);

  const handleChange = (evt) => {
    const newValue = evt.target.value;
    onChange(newValue, evt);
    setValue(newValue);
  };
  const textareaRef = useRef();
  useLayoutEffect(() => {
    if (focusOnMount && textareaRef.current) {
      textareaRef.current.focus();
    }
  }, [focusOnMount]);

  return (
    <TextAreaContainer
      stacked="column"
      className={className}
      {...{ spacing, grow, shrink, basis }}
      data-cy={dataCy}
      data-qa={dataQa}
      id={id}
    >
      {hideLabel ? (
        <HideAccessibly>
          <FieldLabel htmlFor={textareaId}>{label}</FieldLabel>
        </HideAccessibly>
      ) : (
        <FieldLabel htmlFor={textareaId}>{label}</FieldLabel>
      )}

      <StyledTextarea
        ref={textareaRef}
        id={textareaId}
        resize={resize}
        placeholder={placeholder}
        value={managedValue}
        height={height}
        focusOnMount={focusOnMount}
        onChange={handleChange}
        onBlur={onBlur}
        onFocus={onFocus}
        disabled={disabled}
      />
    </TextAreaContainer>
  );
};

TextArea.displayName = 'TextArea';
