/* global React, window */
// Guitar Lab — String-name exercise.
//
// Highlights one open string on the fretboard, asks the user which
// string it is. The answer combines string number + open-note name
// (eg. "Low E (6th string)").

(function () {
  'use strict';
  window.GuitarLab = window.GuitarLab || {};
  window.GuitarLab.ui = window.GuitarLab.ui || {};

  const ExerciseStringName = function (props) {
    const difficulty = props.difficulty || 'beginner';
    const onAttempt = props.onAttempt || function () {};
    const onNext = props.onNext || function () {};

    const [prompt, setPrompt] = React.useState(null);
    const [choices, setChoices] = React.useState([]);
    const [picked, setPicked] = React.useState(null);
    const submittedRef = React.useRef(false);

    React.useEffect(function () {
      const p = window.GuitarLab.theory.randomString(difficulty);
      const count = 4;
      const c = window.GuitarLab.theory.choicesFor(p.correct, p.pool, count);
      setPrompt(p);
      setChoices(c);
      setPicked(null);
      submittedRef.current = false;
      // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [difficulty]);

    function handlePick(choice) {
      if (picked || !prompt || submittedRef.current) return;
      submittedRef.current = true;
      setPicked(choice);
      const wasCorrect = choice.id === prompt.correct.id;
      onAttempt({
        exerciseType: 'string_name',
        difficulty: difficulty,
        prompt: { string_index: prompt.correct.stringIndex },
        userAnswer: choice.id,
        correctAnswer: prompt.correct.id,
        wasCorrect: wasCorrect,
      });
    }

    if (!prompt) return null;
    const Fretboard = window.GuitarLab.ui.Fretboard;
    const highlight = {
      stringIndex: prompt.correct.stringIndex,
      fret: 0,
      label: '?',
      color: 'oklch(50% 0.2 50)',
    };

    return (
      <div>
        <div style={{ fontSize: 14, color: 'oklch(45% 0.04 265)', fontWeight: 600, letterSpacing: '0.04em', textTransform: 'uppercase', marginBottom: 6 }}>String name</div>
        <div style={{ fontSize: 18, color: 'oklch(20% 0.04 265)', fontWeight: 600, marginBottom: 18 }}>Which string is highlighted?</div>

        <div style={{ background: 'oklch(98% 0.005 265)', border: '1px solid oklch(92% 0.012 265)', borderRadius: 12, padding: 14, marginBottom: 20 }}>
          {Fretboard && <Fretboard frets={5} highlight={highlight} width={560} />}
        </div>

        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(180px, 1fr))', gap: 10, marginBottom: 20 }}>
          {choices.map(function (c) {
            const isPicked = picked && picked.id === c.id;
            const isCorrect = c.id === prompt.correct.id;
            let bg = '#fff';
            let border = 'oklch(88% 0.02 265)';
            let color = 'oklch(20% 0.04 265)';
            if (picked) {
              if (isCorrect) { bg = 'oklch(94% 0.07 145)'; border = 'oklch(60% 0.16 145)'; color = 'oklch(28% 0.14 145)'; }
              else if (isPicked) { bg = 'oklch(95% 0.07 25)'; border = 'oklch(60% 0.18 25)'; color = 'oklch(32% 0.16 25)'; }
              else { bg = 'oklch(98% 0.005 265)'; color = 'oklch(55% 0.04 265)'; }
            }
            return (
              <button
                key={c.id}
                onClick={function () { handlePick(c); }}
                disabled={!!picked}
                style={{
                  padding: '14px 16px',
                  borderRadius: 10,
                  border: '1px solid ' + border,
                  background: bg,
                  color: color,
                  fontSize: 14,
                  fontWeight: 600,
                  cursor: picked ? 'default' : 'pointer',
                  fontFamily: "'Plus Jakarta Sans', sans-serif",
                  minHeight: 50,
                  textAlign: 'left',
                  transition: 'all 0.14s',
                }}
              >{c.label}</button>
            );
          })}
        </div>

        {picked && (
          <div style={{ display: 'flex', alignItems: 'center', gap: 12, flexWrap: 'wrap' }}>
            {picked.id === prompt.correct.id ? (
              <div style={{ fontSize: 14, fontWeight: 700, color: 'oklch(35% 0.16 145)' }}>Correct — that's {prompt.correct.label}.</div>
            ) : (
              <div style={{ fontSize: 14, fontWeight: 700, color: 'oklch(38% 0.18 25)' }}>Not quite — the answer was {prompt.correct.label}.</div>
            )}
            <button
              onClick={onNext}
              style={{
                marginLeft: 'auto',
                padding: '10px 22px',
                borderRadius: 10,
                border: 'none',
                background: 'oklch(35% 0.16 265)',
                color: '#fff',
                fontSize: 13,
                fontWeight: 700,
                cursor: 'pointer',
                fontFamily: "'Plus Jakarta Sans', sans-serif",
              }}
            >Next</button>
          </div>
        )}
      </div>
    );
  };

  window.GuitarLab.ui.ExerciseStringName = ExerciseStringName;
})();
