import { useState } from "react";
import { useApp } from "../AppContext";
import { businesses } from "@/mocks/dshn";
import { VerifiedBadge } from "../components/BusinessCard";

const PINS = [
  { id: "biz-rim", top: "26%", left: "38%" },
  { id: "biz-tech", top: "44%", left: "62%" },
  { id: "biz-fashion", top: "58%", left: "30%" },
  { id: "biz-factory", top: "35%", left: "70%" },
  { id: "biz-digital", top: "66%", left: "55%" },
];

export default function MapScreen() {
  const { goBack, navigate } = useApp();
  const [activeId, setActiveId] = useState<string | null>(null);
  const [denied, setDenied] = useState(false);
  const active = businesses.find((b) => b.id === activeId);

  return (
    <div className="relative h-full bg-background-200 overflow-hidden">
      <iframe
        title="خريطة المنشآت"
        className="absolute inset-0 w-full h-full border-0"
        src="https://www.google.com/maps?q=Riyadh,Saudi%20Arabia&z=11&output=embed"
      />
      <div className="absolute inset-0 bg-foreground-950/5" />

      <button
        type="button"
        onClick={() => setDenied((v) => !v)}
        className="absolute top-4 left-1/2 -translate-x-1/2 z-10 h-8 px-3 rounded-full bg-background-100 border border-background-200 text-[10.5px] font-medium text-foreground-600 cursor-pointer whitespace-nowrap"
      >
        {denied ? "إعادة تفعيل الموقع" : "محاكاة رفض إذن الموقع"}
      </button>

      {denied && (
        <div className="absolute top-[70px] left-4 right-4 z-10 flex items-start gap-2.5 rounded-[16px] bg-background-100 border border-background-200 px-4 py-3 animate-fade-in">
          <i className="ri-map-pin-2-line text-[18px] text-foreground-600 leading-none mt-0.5" />
          <div className="flex-1">
            <p className="text-[12.5px] font-semibold text-foreground-950">تم رفض إذن الموقع</p>
            <p className="text-[11.5px] text-foreground-600 mt-0.5 leading-relaxed">
              نعرض الخريطة العامة دون ترتيب المنشآت حسب الأقرب إليك.
            </p>
          </div>
        </div>
      )}

      <button
        type="button"
        onClick={() => navigate("businesses")}
        className="absolute top-4 right-4 h-10 px-3.5 rounded-full bg-background-100 border border-background-200 text-[12.5px] font-semibold text-foreground-950 flex items-center gap-1.5 cursor-pointer"
      >
        <i className="ri-list-check-2 text-[16px] leading-none" />
        عرض كقائمة
      </button>

      <button
        type="button"
        onClick={goBack}
        className="absolute top-4 left-4 w-10 h-10 rounded-full bg-background-100 border border-background-200 flex items-center justify-center text-foreground-950 cursor-pointer"
      >
        <i className="ri-arrow-right-line text-[20px] leading-none" />
      </button>

      {PINS.map((pin) => {
        const biz = businesses.find((b) => b.id === pin.id);
        if (!biz) return null;
        const isActive = activeId === pin.id;
        return (
          <button
            key={pin.id}
            type="button"
            onClick={() => setActiveId(pin.id)}
            style={{ top: pin.top, left: pin.left }}
            className="absolute -translate-x-1/2 -translate-y-full flex flex-col items-center cursor-pointer"
          >
            <div
              className={`w-11 h-11 rounded-full overflow-hidden border-2 shadow-none ${
                isActive ? "border-foreground-950 scale-110" : "border-background-100"
              } transition-all`}
            >
              <img src={biz.logo} alt={biz.name} className="w-full h-full object-cover object-top" />
            </div>
            <span className="w-2.5 h-2.5 rotate-45 bg-foreground-950 -mt-1.5" />
          </button>
        );
      })}

      {active && (
        <div className="absolute bottom-4 left-4 right-4 animate-fade-up">
          <div className="bg-background-100 border border-background-200 rounded-[20px] p-4">
            <div className="flex items-center gap-3.5">
              <div className="w-14 h-14 rounded-full overflow-hidden bg-background-200 shrink-0">
                <img src={active.logo} alt={active.name} className="w-full h-full object-cover object-top" />
              </div>
              <div className="flex-1 min-w-0">
                <div className="flex items-center gap-2 flex-wrap">
                  <h3 className="text-[15px] font-semibold text-foreground-950 truncate">{active.name}</h3>
                  {active.verified && <VerifiedBadge />}
                </div>
                <p className="text-[12px] text-foreground-600 mt-0.5">
                  {active.activity} • {active.city}
                </p>
                <p className="text-[11.5px] text-foreground-600 mt-1">
                  التقييم السوقي: <strong className="text-foreground-900">{active.estimatedValue.toLocaleString("en-US")} ر.س</strong>
                </p>
              </div>
              <button
                type="button"
                onClick={() => setActiveId(null)}
                className="w-8 h-8 rounded-full flex items-center justify-center text-foreground-500 cursor-pointer"
              >
                <i className="ri-close-line text-[20px] leading-none" />
              </button>
            </div>
            <div className="mt-3.5">
              <button
                type="button"
                onClick={() => navigate("business", { businessId: active.id })}
                className="w-full h-[46px] rounded-[14px] bg-primary-500 text-background-50 text-[14px] font-semibold cursor-pointer active:scale-[0.98] transition-transform"
              >
                عرض الصفحة
              </button>
            </div>
          </div>
        </div>
      )}
    </div>
  );
}