import { h } from 'preact';
import { useEffect, useRef, useState } from 'preact/hooks';
import styles from '../NewGame.module.css';
import { ProgressIndicator } from '../ProgressIndicator';

interface PlayerStepProps {
  playerNameHistory: string[];
  onNext: (name: string) => void;
  onCancel: () => void;
  initialValue?: string;
}

export const Player2Step = ({ playerNameHistory, onNext, onCancel, initialValue = '' }: PlayerStepProps) => {
  const [player2, setPlayer2] = useState(initialValue);
  const [error, setError] = useState<string | null>(null);
  const [filteredNames, setFilteredNames] = useState(playerNameHistory);
  const inputRef = useRef<HTMLInputElement>(null);

  useEffect(() => {
    if (!player2) {
      setFilteredNames(playerNameHistory);
    } else {
      setFilteredNames(
        playerNameHistory.filter(name =>
          name.toLowerCase().includes(player2.toLowerCase())
        )
      );
    }
  }, [player2, playerNameHistory]);

  const handleSubmit = (e: Event) => {
    e.preventDefault();
    if (!player2.trim()) {
      setError('Bitte Namen für Spieler 2 eingeben');
      return;
    }
    setError(null);
    onNext(player2.trim());
  };

  const handleQuickPick = (name: string) => {
    setError(null);
    onNext(name);
  };

  const handleClear = () => {
    setPlayer2('');
    setError(null);
    if (inputRef.current) inputRef.current.focus();
  };

  return (
    <form className={styles['new-game-form']} onSubmit={handleSubmit} aria-label="Spieler 2 Eingabe" autoComplete="off">
      <div className={styles['form-header']}>
        <div className={styles['screen-title']}>Name Spieler 2</div>
        <ProgressIndicator currentStep={2} style={{ marginBottom: 24 }} />
      </div>
      
      <div className={styles['form-content']}>
        <div className={styles['player-input'] + ' ' + styles['player2-input']} style={{ marginBottom: 32, position: 'relative' }}>
        <label htmlFor="player2-input" style={{ fontSize: '1.3rem', fontWeight: 600 }}>Spieler 2</label>
        <div style={{ position: 'relative', width: '100%' }}>
          <input
            id="player2-input"
            className={styles['name-input']}
            placeholder="Name Spieler 2"
            value={player2}
            onInput={(e: Event) => {
              const target = e.target as HTMLInputElement;
              setPlayer2(target.value);
            }}
            autoComplete="off"
            autoCapitalize="words"
            spellCheck={false}
            enterKeyHint="next"
            aria-label="Name Spieler 2"
            onFocus={(e) => {
              const target = e.currentTarget as HTMLInputElement;
              target.scrollIntoView({ block: 'center', behavior: 'smooth' });
            }}
            style={{ fontSize: '1.2rem', minHeight: 48, marginTop: 12, marginBottom: 12, width: '100%', paddingRight: 44 }}
            ref={inputRef}
          />
          {player2 && (
            <button
              type="button"
              className={styles['clear-input-btn']}
              aria-label="Feld leeren"
              onClick={handleClear}
              style={{
                position: 'absolute',
                right: 8,
                top: '50%',
                transform: 'translateY(-50%)',
                background: 'none',
                border: 'none',
                cursor: 'pointer',
                fontSize: 24,
                color: '#aaa',
                padding: 0,
                zIndex: 2
              }}
              tabIndex={0}
            >
              ×
            </button>
          )}
        </div>
        {filteredNames.length > 0 && (
          <div style={{ display: 'flex', flexWrap: 'wrap', gap: 12, marginTop: 8 }}>
            {filteredNames.slice(0, 10).map((name, idx) => (
              <button
                type="button"
                key={name + idx}
                className={styles['quick-pick-btn']}
                style={{ fontSize: '1.1rem', padding: '12px 20px', borderRadius: 8, background: '#333', color: '#fff', border: 'none', cursor: 'pointer' }}
                onClick={() => handleQuickPick(name)}
                aria-label={`Schnellauswahl: ${name}`}
              >
                {name}
              </button>
            ))}
          </div>
        )}
        </div>
        {error && <div className={styles['validation-error']} style={{ marginBottom: 16 }}>{error}</div>}
      </div>
      
      <div className={styles['form-footer']}>
        <div className={styles['arrow-nav']} style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
          <button
            type="button"
            className={styles['arrow-btn']}
            aria-label="Zurück"
            onClick={onCancel}
            style={{ fontSize: 48, width: 80, height: 80, borderRadius: '50%', background: '#222', color: '#fff', border: 'none', boxShadow: '0 2px 8px rgba(0,0,0,0.15)', cursor: 'pointer' }}
          >
            &#8592;
          </button>
          <button
            type="submit"
            className={styles['arrow-btn']}
            aria-label="Weiter"
            disabled={!player2.trim()}
            style={{ fontSize: 48, width: 80, height: 80, borderRadius: '50%', background: '#222', color: '#fff', border: 'none', boxShadow: '0 2px 8px rgba(0,0,0,0.15)', cursor: 'pointer', opacity: !player2.trim() ? 0.5 : 1 }}
          >
            &#8594;
          </button>
        </div>
      </div>
    </form>
  );
};


