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

/**
 * Player 1 input step for multi-step game creation wizard.
 * @param {object} props
 * @param {string[]} props.playerNameHistory
 * @param {Function} props.onNext
 * @param {Function} props.onCancel
 * @param {string} [props.initialValue]
 * @returns {import('preact').VNode}
 */
const Player1Step = ({ playerNameHistory, onNext, onCancel, initialValue = '' }) => {
  const [player1, setPlayer1] = useState(initialValue);
  const [error, setError] = useState(null);
  const [filteredNames, setFilteredNames] = useState(playerNameHistory);
  const inputRef = useRef(null);

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

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

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

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

  return (
    <form className={styles['new-game-form']} onSubmit={handleSubmit} aria-label="Spieler 1 Eingabe" autoComplete="off">
      <div className={styles['screen-title']}>Neues Spiel – Schritt 1/5</div>
      <div className={styles['progress-indicator']} style={{ marginBottom: 24 }}>
        <span className={styles['progress-dot'] + ' ' + styles['active']} />
        <span className={styles['progress-dot']} />
        <span className={styles['progress-dot']} />
        <span className={styles['progress-dot']} />
        <span className={styles['progress-dot']} />
      </div>
      <div className={styles['player-input']} style={{ marginBottom: 32, position: 'relative' }}>
        <label htmlFor="player1-input" style={{ fontSize: '1.3rem', fontWeight: 600 }}>Spieler 1</label>
        <div style={{ position: 'relative', width: '100%' }}>
          <input
            id="player1-input"
            className={styles['name-input']}
            placeholder="Name Spieler 1"
            value={player1}
            onInput={e => setPlayer1(e.target.value)}
            autoFocus
            autoComplete="off"
            aria-label="Name Spieler 1"
            style={{ fontSize: '1.2rem', minHeight: 48, marginTop: 12, marginBottom: 12, width: '100%', paddingRight: 44 }}
            ref={inputRef}
          />
          {player1 && (
            <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}
            >
              {/* Unicode heavy multiplication X */}
              ×
            </button>
          )}
        </div>
        {filteredNames.length > 0 && (
          <div style={{ display: 'flex', flexWrap: 'wrap', gap: 12, marginTop: 8 }}>
            {filteredNames.slice(0, 4).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 className={styles['arrow-nav']} style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginTop: 48 }}>
        <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' }}
        >
          {/* Unicode left arrow */}
          &#8592;
        </button>
        <button
          type="submit"
          className={styles['arrow-btn']}
          aria-label="Weiter"
          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' }}
        >
          {/* Unicode right arrow */}
          &#8594;
        </button>
      </div>
    </form>
  );
};

export default Player1Step; 