"use client";

import { useEffect, useState } from "react";
import { GoogleAd } from "./GoogleAd";

interface AdRewardProps {
  onRewardEarned: () => void;
  onClose: () => void;
  watchSeconds?: number;
}

export function AdReward({
  onRewardEarned,
  onClose,
  watchSeconds = 15,
}: AdRewardProps) {
  const [elapsed, setElapsed] = useState(0);
  const [rewarded, setRewarded] = useState(false);

  const progress = Math.min((elapsed / watchSeconds) * 100, 100);
  const remaining = Math.max(watchSeconds - elapsed, 0);

  useEffect(() => {
    if (elapsed >= watchSeconds || rewarded) return;
    const timer = setTimeout(() => setElapsed((e) => e + 1), 1000);
    return () => clearTimeout(timer);
  }, [elapsed, watchSeconds, rewarded]);

  useEffect(() => {
    if (elapsed >= watchSeconds && !rewarded) {
      setRewarded(true);
      onRewardEarned();
    }
  }, [elapsed, watchSeconds, rewarded, onRewardEarned]);

  return (
    <div
      className="fixed inset-0 z-[9999] flex items-center justify-center"
      style={{ background: "rgba(0,0,0,0.9)" }}
    >
      <div
        className="relative w-full max-w-lg mx-4 rounded-2xl overflow-hidden flex flex-col"
        style={{
          background: "var(--bg-secondary)",
          border: "1px solid var(--border)",
        }}
      >
        {/* Header */}
        <div
          className="flex items-center justify-between px-4 py-3"
          style={{ borderBottom: "1px solid var(--border)" }}
        >
          <div className="flex flex-col gap-0.5">
            <span
              className="text-xs font-bold"
              style={{ color: "var(--text-primary)" }}
            >
              Watch to earn reward
            </span>
            <span className="text-[10px]" style={{ color: "var(--text-muted)" }}>
              {rewarded ? "Reward earned!" : `${remaining}s remaining`}
            </span>
          </div>
          {rewarded ? (
            <button
              onClick={onClose}
              className="flex items-center gap-1.5 px-3 py-1 rounded-full text-xs font-bold transition-all active:scale-95"
              style={{ background: "var(--accent)", color: "#fff" }}
            >
              Continue
            </button>
          ) : (
            <button
              onClick={onClose}
              className="text-xs px-3 py-1 rounded-full"
              style={{
                color: "var(--text-muted)",
                background: "var(--btn-ghost)",
              }}
            >
              Skip reward
            </button>
          )}
        </div>

        {/* Progress bar */}
        <div style={{ background: "var(--border)", height: 3 }}>
          <div
            className="h-full transition-all duration-1000"
            style={{ width: `${progress}%`, background: "var(--accent)" }}
          />
        </div>

        {/* Ad / Reward state */}
        <div className="p-4">
          {rewarded ? (
            <div className="flex flex-col items-center justify-center gap-3 py-10">
              <div
                className="w-14 h-14 rounded-full flex items-center justify-center"
                style={{ background: "var(--accent)" }}
              >
                <svg
                  width="28"
                  height="28"
                  viewBox="0 0 24 24"
                  fill="none"
                  stroke="#fff"
                  strokeWidth="2.5"
                  strokeLinecap="round"
                  strokeLinejoin="round"
                >
                  <polyline points="20 6 9 17 4 12" />
                </svg>
              </div>
              <p
                className="text-sm font-semibold"
                style={{ color: "var(--text-primary)" }}
              >
                Reward earned!
              </p>
            </div>
          ) : (
            <GoogleAd placement="others" />
          )}
        </div>
      </div>
    </div>
  );
}
