import React, { useState } from 'react';

const PercentChangeWidget: React.FC = () => {
  const [original, setOriginal] = useState(100);
  const [percent, setPercent] = useState(25); // -100 to 100

  const multiplier = 1 + (percent / 100);
  const newValue = original * multiplier;

  return (
    <div className="bg-white p-6 rounded-xl shadow-lg border border-slate-200">
      <div className="mb-8 space-y-4">
         <div className="flex items-center justify-between">
            <label className="text-sm font-bold text-slate-500 uppercase">Original Value</label>
            <input 
              type="number" value={original} onChange={e => setOriginal(Number(e.target.value))}
              className="w-24 p-1 border border-slate-300 rounded font-mono font-bold text-slate-700 text-right"
            />
         </div>
         <div>
            <div className="flex justify-between mb-2">
                <label className="text-sm font-bold text-slate-500 uppercase">Percent Change</label>
                <span className={`font-bold font-mono ${percent >= 0 ? 'text-emerald-600' : 'text-rose-600'}`}>
                    {percent > 0 ? '+' : ''}{percent}%
                </span>
            </div>
            <input 
              type="range" min="-50" max="100" step="5" value={percent} 
              onChange={e => setPercent(parseInt(e.target.value))}
              className="w-full h-2 bg-slate-200 rounded-lg appearance-none cursor-pointer accent-indigo-600"
            />
         </div>
      </div>

      <div className="flex items-end justify-center gap-8 h-48 border-b border-slate-200 pb-0 mb-6">
          {/* Original Bar */}
          <div className="flex flex-col items-center gap-2 w-24">
              <span className="font-bold text-slate-500">{original}</span>
              <div className="w-full bg-slate-400 rounded-t-lg transition-all duration-500" style={{ height: '120px' }}></div>
              <span className="text-xs font-bold text-slate-400 uppercase mt-2">Original</span>
          </div>

          {/* New Bar */}
          <div className="flex flex-col items-center gap-2 w-24">
              <span className={`font-bold ${percent >= 0 ? 'text-emerald-600' : 'text-rose-600'}`}>
                  {newValue.toFixed(1)}
              </span>
              <div 
                className={`w-full rounded-t-lg transition-all duration-500 ${percent >= 0 ? 'bg-emerald-500' : 'bg-rose-500'}`} 
                style={{ height: `${120 * multiplier}px` }}
              ></div>
              <span className="text-xs font-bold text-slate-400 uppercase mt-2">New</span>
          </div>
      </div>

      <div className="bg-slate-50 p-4 rounded-xl border border-slate-200">
          <h4 className="text-xs font-bold text-slate-400 uppercase mb-2">Formula</h4>
          <div className="font-mono text-lg text-center text-slate-800">
              New = Original × (1 {percent >= 0 ? '+' : '-'} <span className="text-indigo-600">{Math.abs(percent/100)}</span>)
          </div>
          <div className="font-mono text-xl font-bold text-center text-indigo-700 mt-2">
              New = {original} × {multiplier.toFixed(2)}
          </div>
      </div>
    </div>
  );
};

export default PercentChangeWidget;