"use client"

import { Button } from "@/app/_components/ui/button"
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/app/_components/ui/tooltip"
import { AlertTriangle, Building, Car, Thermometer, History } from "lucide-react"
import type { ITooltips } from "./tooltips"
import { IconChartBubble, IconClock } from "@tabler/icons-react"

// Define the primary crime data controls
const crimeTooltips = [
    // { id: "incidents" as ITooltips, icon: <AlertTriangle size={20} />, label: "All Incidents" },
    { id: "heatmap" as ITooltips, icon: <Thermometer size={20} />, label: "Density Heatmap" },
    { id: "units" as ITooltips, icon: <Building size={20} />, label: "Police Units" },
    { id: "clusters" as ITooltips, icon: <IconChartBubble size={20} />, label: "Clustered Incidents" },
    { id: "patrol" as ITooltips, icon: <Car size={20} />, label: "Patrol Areas" },
    { id: "timeline" as ITooltips, icon: <IconClock size={20} />, label: "Time Analysis" },
]

interface CrimeTooltipsProps {
    activeControl?: string
    onControlChange?: (controlId: ITooltips) => void
    sourceType?: string
}

export default function CrimeTooltips({ activeControl, onControlChange, sourceType = "cbt" }: CrimeTooltipsProps) {
    const handleControlClick = (controlId: ITooltips) => {
      // If control is disabled, don't do anything
      if (isDisabled(controlId)) {
          return
      }

      // Force the value to be set when clicking
      if (onControlChange) {
        onControlChange(controlId)
        console.log("Control changed to:", controlId)
    }
    }

    // Determine which controls should be disabled based on source type
    const isDisabled = (controlId: ITooltips) => {
        return sourceType === "cbu" && controlId !== "clusters"
    }

    return (
        <div className="z-10 bg-background rounded-md p-1 flex items-center space-x-1">
            <TooltipProvider>
              {crimeTooltips.map((control) => {
                  const isButtonDisabled = isDisabled(control.id)

                  return (
                      <Tooltip key={control.id}>
                          <TooltipTrigger asChild>
                              <Button
                                  variant={activeControl === control.id ? "default" : "ghost"}
                                  size="medium"
                          className={`h-8 w-8 rounded-md ${isButtonDisabled
                                  ? "opacity-40 cursor-not-allowed bg-gray-700/30 text-gray-400 border-gray-600 hover:bg-gray-700/30 hover:text-gray-400"
                                  : activeControl === control.id
                                      ? "bg-emerald-500 text-black hover:bg-emerald-500/90"
                                      : "text-white hover:bg-emerald-500/90 hover:text-background"
                              }`}
                          onClick={() => handleControlClick(control.id)}
                          disabled={isButtonDisabled}
                          aria-disabled={isButtonDisabled}
                      >
                          {control.icon}
                          <span className="sr-only">{control.label}</span>
                      </Button>
                  </TooltipTrigger>
                  <TooltipContent side="bottom">
                      <p>{isButtonDisabled ? "Not available for CBU data" : control.label}</p>
                  </TooltipContent>
              </Tooltip>
            )
        })}
          </TooltipProvider>
      </div>
  )
}
