import type {
  ChangeEvent, FormEvent,
} from 'react';
import type { ElementRef } from '../../lib/teact/teact';
import { memo } from '../../lib/teact/teact';

import { IS_TAURI } from '../../util/browser/globalEnvironment';
import buildClassName from '../../util/buildClassName';

import useLang from '../../hooks/useLang';

import AnimatedCounter from '../common/AnimatedCounter';

type OwnProps = {
  ref?: ElementRef<HTMLInputElement>;
  id?: string;
  className?: string;
  value?: string;
  label?: string;
  error?: string;
  success?: string;
  disabled?: boolean;
  readOnly?: boolean;
  placeholder?: string;
  autoComplete?: string;
  maxLength?: number;
  hasLengthIndicator?: boolean;
  tabIndex?: number;
  title?: string;
  autoFocus?: boolean;
  teactExperimentControlled?: boolean;
  noMargin?: boolean;
  inputMode?: 'text' | 'none' | 'tel' | 'url' | 'email' | 'numeric' | 'decimal' | 'search';
  onChange?: (e: ChangeEvent<HTMLInputElement>) => void;
  onInput?: (e: FormEvent<HTMLInputElement>) => void;
  onKeyPress?: (e: React.KeyboardEvent<HTMLInputElement>) => void;
  onKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>) => void;
  onBlur?: (e: React.FocusEvent<HTMLInputElement>) => void;
  onPaste?: (e: React.ClipboardEvent<HTMLInputElement>) => void;
  onClick?: (e: React.MouseEvent<HTMLInputElement>) => void;
};

const InputText = ({
  ref,
  id,
  className,
  value,
  label,
  error,
  success,
  disabled,
  readOnly,
  placeholder,
  autoComplete = 'off',
  inputMode,
  maxLength,
  hasLengthIndicator,
  tabIndex,
  title,
  autoFocus,
  teactExperimentControlled,
  noMargin,
  onChange,
  onInput,
  onKeyPress,
  onKeyDown,
  onBlur,
  onPaste,
  onClick,
}: OwnProps) => {
  const lang = useLang();
  const labelText = error || success || label;
  const fullClassName = buildClassName(
    'input-group',
    value && 'touched',
    error ? 'error' : success && 'success',
    disabled && 'disabled',
    readOnly && 'disabled',
    labelText && 'with-label',
    noMargin && 'no-margin',
    className,
  );

  return (
    <div className={fullClassName} dir={lang.isRtl ? 'rtl' : undefined}>
      <input
        ref={ref}
        className="form-control"
        type="text"
        id={id}
        dir="auto"
        value={value || ''}
        tabIndex={tabIndex}
        placeholder={placeholder}
        maxLength={maxLength}
        autoComplete={autoComplete}
        spellCheck={IS_TAURI ? false : undefined}
        inputMode={inputMode}
        disabled={disabled}
        readOnly={readOnly}
        onChange={onChange}
        onInput={onInput}
        onKeyPress={onKeyPress}
        onKeyDown={onKeyDown}
        onBlur={onBlur}
        onPaste={onPaste}
        aria-label={labelText}
        title={title}
        teactExperimentControlled={teactExperimentControlled}
        onClick={onClick}
        autoFocus={autoFocus}
      />
      {labelText && (
        <label htmlFor={id}>{labelText}</label>
      )}
      {hasLengthIndicator && maxLength !== undefined && (
        <div className="max-length-indicator">
          <AnimatedCounter text={Math.max(0, maxLength - (value || '').length).toString()} />
        </div>
      )}
    </div>
  );
};

export default memo(InputText);
