import React, { useState } from 'react';
import { Check, RotateCcw, ArrowRight } from 'lucide-react';

const LiteralEquationWidget: React.FC = () => {
  const [problemIdx, setProblemIdx] = useState(0);
  const [step, setStep] = useState(0);

  const problems = [
    {
      id: 'perimeter',
      title: 'Perimeter Formula',
      goal: 'Isolate W',
      steps: [
        {
          startEq: <>P = 2L + 2<span className="text-indigo-600">W</span></>,
          options: [
            { text: 'Subtract 2L', correct: true },
            { text: 'Divide by 2', correct: false }
          ],
          feedback: 'Moved 2L to the other side.',
          nextEq: <>P - 2L = 2<span className="text-indigo-600">W</span></>
        },
        {
          startEq: <>P - 2L = 2<span className="text-indigo-600">W</span></>,
          options: [
            { text: 'Divide by 2', correct: true },
            { text: 'Subtract 2', correct: false }
          ],
          feedback: 'Solved!',
          nextEq: <><span className="text-indigo-600">W</span> = <span className="inline-block align-middle text-center border-t border-slate-800 pt-1 leading-none"><span className="block pb-1">P - 2L</span>2</span></>
        }
      ]
    },
    {
      id: 'linear',
      title: 'Slope-Intercept',
      goal: 'Isolate x',
      steps: [
        {
          startEq: <>y = m<span className="text-indigo-600">x</span> + b</>,
          options: [
            { text: 'Subtract b', correct: true },
            { text: 'Divide by m', correct: false }
          ],
          feedback: 'Isolated the x term.',
          nextEq: <>y - b = m<span className="text-indigo-600">x</span></>
        },
        {
          startEq: <>y - b = m<span className="text-indigo-600">x</span></>,
          options: [
            { text: 'Divide by m', correct: true },
            { text: 'Subtract m', correct: false }
          ],
          feedback: 'Solved!',
          nextEq: <><span className="text-indigo-600">x</span> = <span className="inline-block align-middle text-center border-t border-slate-800 pt-1 leading-none"><span className="block pb-1">y - b</span>m</span></>
        }
      ]
    },
    {
      id: 'standard',
      title: 'Standard Form',
      goal: 'Isolate y',
      steps: [
        {
          startEq: <>Ax + B<span className="text-indigo-600">y</span> = C</>,
          options: [
            { text: 'Subtract Ax', correct: true },
            { text: 'Divide by B', correct: false }
          ],
          feedback: 'Moved the x term away.',
          nextEq: <>B<span className="text-indigo-600">y</span> = C - Ax</>
        },
        {
          startEq: <>B<span className="text-indigo-600">y</span> = C - Ax</>,
          options: [
            { text: 'Divide by B', correct: true },
            { text: 'Subtract B', correct: false }
          ],
          feedback: 'Solved!',
          nextEq: <><span className="text-indigo-600">y</span> = <span className="inline-block align-middle text-center border-t border-slate-800 pt-1 leading-none"><span className="block pb-1">C - Ax</span>B</span></>
        }
      ]
    },
    {
      id: 'physics',
      title: 'Velocity Formula',
      goal: 'Isolate a',
      steps: [
        {
          startEq: <>v = u + <span className="text-indigo-600">a</span>t</>,
          options: [
            { text: 'Subtract u', correct: true },
            { text: 'Divide by t', correct: false }
          ],
          feedback: 'Isolated the term with a.',
          nextEq: <>v - u = <span className="text-indigo-600">a</span>t</>
        },
        {
          startEq: <>v - u = <span className="text-indigo-600">a</span>t</>,
          options: [
            { text: 'Divide by t', correct: true },
            { text: 'Subtract t', correct: false }
          ],
          feedback: 'Solved!',
          nextEq: <><span className="text-indigo-600">a</span> = <span className="inline-block align-middle text-center border-t border-slate-800 pt-1 leading-none"><span className="block pb-1">v - u</span>t</span></>
        }
      ]
    }
  ];

  const currentProb = problems[problemIdx];
  const currentStepData = currentProb.steps[step];

  const handleNextProblem = () => {
    let next = Math.floor(Math.random() * problems.length);
    while (next === problemIdx) {
        next = Math.floor(Math.random() * problems.length);
    }
    setProblemIdx(next);
    setStep(0);
  };

  const reset = () => {
    setStep(0);
  };

  const handleOption = (isCorrect: boolean) => {
      if (isCorrect) {
          setStep(step + 1);
      }
  };

  return (
    <div className="bg-white p-6 rounded-xl shadow-lg border border-slate-200">
        <div className="flex justify-between items-center mb-6">
            <h4 className="font-bold text-slate-700 flex items-center gap-2">
                <span className="w-6 h-6 rounded-full bg-indigo-100 text-indigo-700 flex items-center justify-center text-xs font-bold">Ex</span>
                {currentProb.goal}
            </h4>
            <button onClick={reset} className="text-slate-400 hover:text-indigo-600 transition-colors" title="Reset this problem">
                <RotateCcw className="w-4 h-4" />
            </button>
        </div>

        <div className="text-center mb-8 h-32 flex flex-col items-center justify-center transition-all">
            {step < 2 ? (
                <div className="animate-fade-in">
                    <div className="text-3xl font-mono font-bold text-slate-800 mb-2">
                        {currentStepData.startEq}
                    </div>
                    {step === 1 && <p className="text-sm text-green-600 font-bold mb-2 animate-pulse">{problems[problemIdx].steps[0].feedback}</p>}
                    <p className="text-sm text-slate-500">
                        {step === 0 ? "What is the first step?" : "What is the next step?"}
                    </p>
                </div>
            ) : (
                <div className="animate-fade-in bg-green-50 p-6 rounded-xl border border-green-200 w-full">
                    <div className="text-3xl font-mono font-bold text-green-800 mb-2">
                        {currentProb.steps[1].nextEq}
                    </div>
                    <div className="flex items-center justify-center gap-2 text-green-700 font-bold">
                        <Check className="w-5 h-5" /> {currentProb.steps[1].feedback}
                    </div>
                </div>
            )}
        </div>

        <div className="grid grid-cols-2 gap-4">
            {step < 2 ? (
                <>
                    {currentStepData.options.map((opt, i) => (
                        <button 
                            key={i}
                            onClick={() => handleOption(opt.correct)} 
                            className={`p-4 rounded-xl border-2 transition-all text-left group ${
                                opt.correct 
                                ? 'bg-slate-50 border-slate-200 hover:border-indigo-400 hover:bg-indigo-50' 
                                : 'bg-slate-50 border-slate-200 hover:border-slate-300 opacity-80'
                            }`}
                        >
                            <span className={`text-xs font-bold uppercase mb-1 block ${opt.correct ? 'text-indigo-400 group-hover:text-indigo-600' : 'text-slate-400'}`}>
                                Option {i+1}
                            </span>
                            <span className="font-bold text-slate-700 group-hover:text-indigo-900">{opt.text}</span>
                        </button>
                    ))}
                </>
            ) : (
                <button 
                    onClick={handleNextProblem} 
                    className="col-span-2 p-4 bg-indigo-600 text-white font-bold rounded-xl hover:bg-indigo-700 transition-colors flex items-center justify-center gap-2 shadow-md hover:shadow-lg transform hover:-translate-y-0.5"
                >
                    Try Another Problem <ArrowRight className="w-4 h-4" />
                </button>
            )}
        </div>
    </div>
  );
};

export default LiteralEquationWidget;