import React, { useRef, useState, useEffect } from "react";
import { ArrowDown, Check, BookOpen, Target, Layers } from "lucide-react";
import CircleTheoremsWidget from "../../../components/lessons/CircleTheoremsWidget";
import TangentPropertiesWidget from "../../../components/lessons/TangentPropertiesWidget";
import PowerOfPointWidget from "../../../components/lessons/PowerOfPointWidget";
import Quiz from "../../../components/lessons/Quiz";
import { CIRCLE_PROP_QUIZ_DATA } from "../../../utils/constants";
import { Frac } from "../../../components/Math";

interface LessonProps {
  onFinish?: () => void;
}

const CirclePropertiesLesson: React.FC<LessonProps> = ({ onFinish }) => {
  const [activeSection, setActiveSection] = useState(0);
  const sectionsRef = useRef<(HTMLElement | null)[]>([]);

  const scrollToSection = (index: number) => {
    setActiveSection(index);
    sectionsRef.current[index]?.scrollIntoView({
      behavior: "smooth",
      block: "start",
    });
  };

  useEffect(() => {
    const observer = new IntersectionObserver(
      (entries) => {
        entries.forEach((entry) => {
          if (entry.isIntersecting) {
            const index = sectionsRef.current.indexOf(
              entry.target as HTMLElement,
            );
            if (index !== -1) setActiveSection(index);
          }
        });
      },
      { rootMargin: "-20% 0px -60% 0px" },
    );
    sectionsRef.current.forEach((section) => {
      if (section) observer.observe(section);
    });
    return () => observer.disconnect();
  }, []);

  const SectionMarker = ({
    index,
    title,
    icon: Icon,
  }: {
    index: number;
    title: string;
    icon: any;
  }) => {
    const isActive = activeSection === index;
    const isPast = activeSection > index;
    return (
      <button
        onClick={() => scrollToSection(index)}
        className={`flex items-center gap-3 p-3 w-full rounded-lg transition-all ${isActive ? "bg-white shadow-md border border-violet-100" : "hover:bg-slate-100"}`}
      >
        <div
          className={`w-8 h-8 rounded-full flex items-center justify-center shrink-0 ${isActive ? "bg-violet-600 text-white" : isPast ? "bg-violet-400 text-white" : "bg-slate-200 text-slate-500"}`}
        >
          {isPast ? (
            <Check className="w-4 h-4" />
          ) : (
            <Icon className="w-4 h-4" />
          )}
        </div>
        <div className="text-left">
          <p
            className={`text-sm font-bold ${isActive ? "text-violet-900" : "text-slate-600"}`}
          >
            {title}
          </p>
        </div>
      </button>
    );
  };

  return (
    <div className="flex flex-col lg:flex-row min-h-screen">
      <aside className="w-full lg:w-64 lg:fixed lg:top-20 lg:bottom-0 lg:overflow-y-auto p-4 border-r border-slate-200 bg-slate-50 z-0 hidden lg:block">
        <nav className="space-y-2">
          <SectionMarker index={0} title="Central vs Inscribed" icon={Target} />
          <SectionMarker index={1} title="Tangents" icon={Layers} />
          <SectionMarker index={2} title="Power of a Point" icon={BookOpen} />
          <SectionMarker index={3} title="Practice" icon={BookOpen} />
        </nav>
      </aside>

      <div className="flex-1 lg:ml-64 p-6 md:p-12 max-w-4xl mx-auto">
        {/* Section 1: Central vs Inscribed Angles */}
        <section
          ref={(el) => {
            sectionsRef.current[0] = el;
          }}
          className="min-h-screen flex flex-col justify-center mb-24 pt-20 lg:pt-0"
        >
          <h2 className="text-4xl font-extrabold text-slate-900 mb-6">
            Central vs. Inscribed Angles
          </h2>
          <div className="prose prose-slate text-lg text-slate-600 mb-8">
            <p>
              Circle angle theorems are among the highest-frequency SAT topics.
              The core relationship is simple: angles and arcs are linked by a
              factor of 2.
            </p>
          </div>

          <div className="bg-violet-50 border border-violet-200 rounded-2xl p-6 mb-8 space-y-5">
            <h3 className="text-lg font-bold text-violet-900">
              The Central vs. Inscribed Relationship
            </h3>

            <div className="grid md:grid-cols-2 gap-4">
              <div className="bg-white rounded-xl p-5 border border-violet-200">
                <p className="font-bold text-violet-900 mb-1">Central Angle</p>
                <p className="text-sm text-slate-700 mb-2">
                  Vertex at the <strong>center</strong>. Degree measure equals
                  the intercepted arc.
                </p>
                <div className="font-mono text-center bg-violet-50 py-2 rounded text-violet-700 font-bold">
                  ∠central = arc°
                </div>
                <p className="text-xs text-slate-500 mt-2">
                  Example: Central angle = 80° → arc = 80°
                </p>
              </div>
              <div className="bg-indigo-50 rounded-xl p-5 border border-indigo-200">
                <p className="font-bold text-indigo-900 mb-1">
                  Inscribed Angle
                </p>
                <p className="text-sm text-slate-700 mb-2">
                  Vertex on the <strong>circle</strong>. Measure is exactly half
                  the intercepted arc.
                </p>
                <div className="font-mono text-center bg-indigo-50 py-2 rounded text-indigo-700 font-bold">
                  ∠inscribed = <Frac n="arc°" d="2" />
                </div>
                <p className="text-xs text-slate-500 mt-2">
                  Example: Arc = 120° → inscribed angle = 60°
                </p>
              </div>
            </div>

            {/* Key Corollaries */}
            <div className="bg-white rounded-xl p-5 border border-violet-100">
              <p className="font-bold text-violet-800 mb-3">
                Key Corollaries (SAT Favorites)
              </p>
              <div className="space-y-2">
                <div className="bg-violet-50 rounded-lg p-3 text-sm">
                  <p className="font-semibold text-violet-800 mb-1">
                    Thales' Theorem
                  </p>
                  <p className="text-slate-700">
                    An inscribed angle that intercepts a{" "}
                    <strong>semicircle</strong> (its chord is a diameter) is
                    always <strong>90°</strong>. If you see a triangle inscribed
                    in a circle where one side is the diameter, the opposite
                    angle is 90°.
                  </p>
                </div>
                <div className="bg-violet-50 rounded-lg p-3 text-sm">
                  <p className="font-semibold text-violet-800 mb-1">
                    Inscribed Angles on the Same Arc
                  </p>
                  <p className="text-slate-700">
                    All inscribed angles intercepting the same arc are equal,
                    regardless of where on the circle the vertex sits.
                  </p>
                </div>
                <div className="bg-violet-50 rounded-lg p-3 text-sm">
                  <p className="font-semibold text-violet-800 mb-1">
                    Cyclic Quadrilateral
                  </p>
                  <p className="text-slate-700">
                    Opposite angles in a quadrilateral inscribed in a circle sum
                    to 180°. So ∠A + ∠C = 180° and ∠B + ∠D = 180°.
                  </p>
                </div>
              </div>
            </div>

            {/* Worked Examples */}
            <div className="space-y-3">
              <div className="bg-sky-50 rounded-xl p-4 border border-sky-200 text-sm">
                <p className="font-semibold text-sky-800 mb-2">
                  Worked Example 1: Find inscribed angle
                </p>
                <div className="font-mono text-xs text-slate-700 space-y-1">
                  <p>
                    A central angle is 110°. An inscribed angle intercepts the
                    same arc. Find the inscribed angle.
                  </p>
                  <p>Arc = 110° (central angle equals arc)</p>
                  <p>
                    Inscribed angle = <Frac n="110°" d="2" /> ={" "}
                    <strong className="text-sky-800">55°</strong>
                  </p>
                </div>
              </div>
              <div className="bg-sky-50 rounded-xl p-4 border border-sky-200 text-sm">
                <p className="font-semibold text-sky-800 mb-2">
                  Worked Example 2: Cyclic quadrilateral
                </p>
                <div className="font-mono text-xs text-slate-700 space-y-1">
                  <p>
                    Quadrilateral ABCD is inscribed in a circle. ∠A = 75°, ∠B =
                    85°. Find ∠C and ∠D.
                  </p>
                  <p>
                    ∠C = 180° − 75° ={" "}
                    <strong className="text-sky-800">105°</strong> (opposite to
                    A)
                  </p>
                  <p>
                    ∠D = 180° − 85° ={" "}
                    <strong className="text-sky-800">95°</strong> (opposite to
                    B)
                  </p>
                </div>
              </div>
            </div>
          </div>

          <CircleTheoremsWidget />

          <button
            onClick={() => scrollToSection(1)}
            className="mt-12 group flex items-center text-violet-600 font-bold hover:text-violet-800 transition-colors"
          >
            Next: Tangent Properties{" "}
            <ArrowDown className="ml-2 w-5 h-5 group-hover:translate-y-1 transition-transform" />
          </button>
        </section>

        {/* Section 2: Tangents */}
        <section
          ref={(el) => {
            sectionsRef.current[1] = el;
          }}
          className="min-h-screen flex flex-col justify-center mb-24"
        >
          <h2 className="text-4xl font-extrabold text-slate-900 mb-6">
            Tangent Properties
          </h2>
          <div className="prose prose-slate text-lg text-slate-600 mb-8">
            <p>
              A tangent line touches the circle at exactly one point (the point
              of tangency). Two critical theorems govern all SAT tangent
              questions.
            </p>
          </div>

          <div className="bg-violet-50 border border-violet-200 rounded-2xl p-6 mb-8 space-y-5">
            <h3 className="text-lg font-bold text-violet-900">
              Two Fundamental Tangent Theorems
            </h3>

            <div className="space-y-3">
              <div className="bg-white rounded-xl p-5 border border-violet-200">
                <p className="font-bold text-violet-900 mb-2">
                  Property 1: Tangent-Radius Perpendicularity
                </p>
                <p className="text-sm text-slate-700 mb-2">
                  A radius drawn to the point of tangency is always{" "}
                  <strong>perpendicular</strong> to the tangent line — they form
                  a 90° angle. This creates a right triangle you can use with
                  the Pythagorean theorem.
                </p>
                <div className="bg-violet-50 rounded-lg p-3 text-sm">
                  <p className="font-semibold text-violet-700 mb-1">
                    Worked Example:
                  </p>
                  <div className="font-mono text-xs text-slate-700 space-y-1">
                    <p>
                      External point P is 13 units from center O. Radius = 5.
                      Find tangent length PT.
                    </p>
                    <p>PT² + r² = PO² (right angle at T)</p>
                    <p>PT² + 25 = 169</p>
                    <p>
                      PT = √144 ={" "}
                      <strong className="text-violet-700">12</strong>
                    </p>
                  </div>
                </div>
              </div>
              <div className="bg-white rounded-xl p-5 border border-violet-200">
                <p className="font-bold text-violet-900 mb-2">
                  Property 2: Two Tangents from One External Point
                </p>
                <p className="text-sm text-slate-700 mb-2">
                  If two tangent segments are drawn from the same external
                  point, they are <strong>equal in length</strong>. If PA and PB
                  are both tangents from P, then PA = PB.
                </p>
                <div className="bg-violet-50 rounded-lg p-3 text-sm">
                  <p className="font-semibold text-violet-700 mb-1">
                    Worked Example:
                  </p>
                  <div className="font-mono text-xs text-slate-700 space-y-1">
                    <p>
                      From external point P, tangent PA = 3x + 2 and tangent PB
                      = 5x − 4.
                    </p>
                    <p>Set equal: 3x + 2 = 5x − 4</p>
                    <p>6 = 2x → x = 3</p>
                    <p>
                      PA = PB = <strong className="text-violet-700">11</strong>
                    </p>
                  </div>
                </div>
              </div>
            </div>

            {/* SAT Trap */}
            <div className="bg-red-50 border border-red-200 rounded-xl p-4 text-sm">
              <p className="font-bold text-red-900 mb-1">
                SAT Trap: Don't Confuse Tangent Line with Tangent Segment
              </p>
              <p className="text-slate-700">
                The "two tangents are equal" rule applies to the{" "}
                <em>segments</em> from the external point to the points of
                tangency — not to the full tangent lines extending beyond the
                circle.
              </p>
            </div>
          </div>

          <TangentPropertiesWidget />

          <button
            onClick={() => scrollToSection(2)}
            className="mt-12 group flex items-center text-violet-600 font-bold hover:text-violet-800 transition-colors"
          >
            Next: Power of a Point{" "}
            <ArrowDown className="ml-2 w-5 h-5 group-hover:translate-y-1 transition-transform" />
          </button>
        </section>

        {/* Section 3: Power of a Point */}
        <section
          ref={(el) => {
            sectionsRef.current[2] = el;
          }}
          className="min-h-screen flex flex-col justify-center mb-24"
        >
          <h2 className="text-4xl font-extrabold text-slate-900 mb-6">
            Power of a Point
          </h2>
          <div className="prose prose-slate text-lg text-slate-600 mb-8">
            <p>
              "Power of a Point" relates segment lengths when lines pass through
              or near a circle. Two main cases appear on the SAT.
            </p>
          </div>

          <div className="bg-violet-50 border border-violet-200 rounded-2xl p-6 mb-8 space-y-5">
            <h3 className="text-lg font-bold text-violet-900">
              The Two Power-of-a-Point Cases
            </h3>

            <div className="grid md:grid-cols-2 gap-4">
              <div className="bg-white rounded-xl p-5 border border-violet-200">
                <p className="font-bold text-violet-900 mb-1">
                  Case 1: Chord-Chord (Inside)
                </p>
                <p className="text-sm text-slate-700 mb-2">
                  Two chords intersect inside the circle at point P.
                </p>
                <div className="font-mono text-center bg-violet-50 py-2 rounded text-violet-700 font-bold">
                  a × b = c × d
                </div>
                <p className="text-xs text-slate-500 mt-2">
                  a and b are the two segments of one chord; c and d are the two
                  segments of the other.
                </p>
              </div>
              <div className="bg-indigo-50 rounded-xl p-5 border border-indigo-200">
                <p className="font-bold text-indigo-900 mb-1">
                  Case 2: Secant-Secant or Tangent-Secant (Outside)
                </p>
                <p className="text-sm text-slate-700 mb-2">
                  Two secants, or a tangent and secant, from external point P.
                </p>
                <div className="font-mono text-center bg-indigo-50 py-2 rounded text-indigo-700 font-bold">
                  ext₁ × whole₁ = ext₂ × whole₂
                </div>
                <p className="text-xs text-slate-500 mt-2">
                  For tangent: tangent² = ext × whole (since both segments of
                  the tangent chord are equal).
                </p>
              </div>
            </div>

            {/* Worked Examples */}
            <div className="space-y-3">
              <div className="bg-sky-50 rounded-xl p-4 border border-sky-200 text-sm">
                <p className="font-semibold text-sky-800 mb-2">
                  Worked Example 1: Chord-Chord
                </p>
                <div className="font-mono text-xs text-slate-700 space-y-1">
                  <p>
                    Two chords intersect inside. Chord 1 has segments 4 and 9.
                    Chord 2 has segments 6 and x.
                  </p>
                  <p>4 × 9 = 6 × x</p>
                  <p>
                    36 = 6x → x = <strong className="text-sky-800">6</strong>
                  </p>
                </div>
              </div>
              <div className="bg-sky-50 rounded-xl p-4 border border-sky-200 text-sm">
                <p className="font-semibold text-sky-800 mb-2">
                  Worked Example 2: Tangent-Secant
                </p>
                <div className="font-mono text-xs text-slate-700 space-y-1">
                  <p>
                    From external point P: tangent PT = 6, secant passes through
                    circle with external part = 4 and whole length = x.
                  </p>
                  <p>PT² = ext × whole</p>
                  <p>6² = 4 × x</p>
                  <p>
                    36 = 4x → x = <strong className="text-sky-800">9</strong>
                  </p>
                </div>
              </div>
              <div className="bg-sky-50 rounded-xl p-4 border border-sky-200 text-sm">
                <p className="font-semibold text-sky-800 mb-2">
                  Worked Example 3: Secant-Secant
                </p>
                <div className="font-mono text-xs text-slate-700 space-y-1">
                  <p>
                    Two secants from P: first has external 3, whole 12. Second
                    has external 4, whole x.
                  </p>
                  <p>3 × 12 = 4 × x</p>
                  <p>
                    36 = 4x → x = <strong className="text-sky-800">9</strong>
                  </p>
                </div>
              </div>
            </div>
          </div>

          <PowerOfPointWidget />

          <button
            onClick={() => scrollToSection(3)}
            className="mt-12 group flex items-center text-violet-600 font-bold hover:text-violet-800 transition-colors"
          >
            Next: Practice Quiz{" "}
            <ArrowDown className="ml-2 w-5 h-5 group-hover:translate-y-1 transition-transform" />
          </button>
        </section>

        {/* Section 4: Quiz */}
        <section
          ref={(el) => {
            sectionsRef.current[3] = el;
          }}
          className="min-h-screen flex flex-col justify-center"
        >
          <h2 className="text-4xl font-extrabold text-slate-900 mb-8">
            Practice Time
          </h2>
          {CIRCLE_PROP_QUIZ_DATA.map((quiz, idx) => (
            <div key={quiz.id} className="mb-12">
              <Quiz data={quiz} />
            </div>
          ))}
          <div className="p-8 bg-violet-900 rounded-2xl text-white text-center mt-12">
            <h3 className="text-2xl font-bold mb-4">Topic Mastered!</h3>
            <button
              onClick={onFinish}
              className="px-6 py-3 bg-white text-violet-900 font-bold rounded-full hover:bg-violet-50 transition-colors"
            >
              Finish Lesson ✓
            </button>
          </div>
        </section>
      </div>
    </div>
  );
};

export default CirclePropertiesLesson;
