import React, { useState, useEffect, useCallback, useRef } from 'react';
import { FiAward, FiPercent, FiActivity } from 'react-icons/fi';

function getStatusColor(validCert) {
  if (validCert === false) return 'bg-red-600';
  if (validCert === true) return 'bg-green-600';
  return 'bg-gray-400';
}

function getHeartbeatColor(status) {
  if (status === 1) return 'bg-green-600'; // UP
  if (status === 0) return 'bg-red-600';   // DOWN
  if (status === 2) return 'bg-yellow-400'; // PENDING
  if (status === 3) return 'bg-blue-500';   // MAINTENANCE
  return 'bg-gray-300';
}

function getCertBg(days) {
  if (typeof days !== 'number') return 'bg-gray-400 dark:bg-gray-600';
  if (days < 0) return 'bg-red-600 animate-pulse border-2 border-red-500 dark:bg-red-800';
  if (days < 5) return 'bg-red-600 dark:bg-red-800';
  if (days <= 15) return 'bg-yellow-400 dark:bg-yellow-500 text-gray-900';
  return 'bg-green-600 dark:bg-green-700';
}

function getCertText(days) {
  if (typeof days !== 'number') return 'Cert Exp';
  if (days < 0) return 'Cert Expired!';
  return 'Cert Exp';
}

function getAvgPingBg(avgPing) {
  if (typeof avgPing !== 'number') return 'bg-gray-300 dark:bg-gray-700';
  if (avgPing <= 80) return 'bg-green-600 text-white dark:bg-green-700 dark:text-white';
  if (avgPing <= 200) return 'bg-yellow-400 text-gray-900 dark:bg-yellow-500 dark:text-gray-900';
  return 'bg-red-600 text-white dark:bg-red-800 dark:text-white';
}

function getUptime24hBg(uptime) {
  if (typeof uptime !== 'number') return 'bg-gray-300 dark:bg-gray-700';
  if (uptime >= 99.9) return 'bg-green-600 text-white dark:bg-green-700 dark:text-white';
  if (uptime >= 99) return 'bg-yellow-400 text-gray-900 dark:bg-yellow-500 dark:text-gray-900';
  return 'bg-red-600 text-white dark:bg-red-800 dark:text-white';
}

export default function UptimeStatusIsland() {
  const [data, setData] = useState(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);
  // Track open badge for each monitor by monitor.id and badge type
  const [openBadge, setOpenBadge] = useState({});
  // Refs for each badge popup
  const badgeRefs = useRef({});

  const fetchData = useCallback(async () => {
    setLoading(true);
    setError(null);
    try {
      const response = await fetch('/api/uptime');
      if (!response.ok) throw new Error('Failed to fetch uptime data');
      const json = await response.json();
      setData(json);
    } catch (err) {
      setError(err.message || 'Unknown error');
    } finally {
      setLoading(false);
    }
  }, []);

  useEffect(() => {
    fetchData();
  }, [fetchData]);

  // Helper to toggle badge info
  const toggleBadge = (monitorId, badge) => {
    setOpenBadge((prev) => ({
      ...prev,
      [monitorId]: {
        ...prev[monitorId],
        [badge]: !prev[monitorId]?.[badge],
      },
    }));
  };

  // Close popup on outside click
  useEffect(() => {
    function handleClick(e) {
      let clickedInside = false;
      Object.values(badgeRefs.current).forEach((monitorBadges) => {
        Object.values(monitorBadges || {}).forEach((ref) => {
          if (ref && ref.current && ref.current.contains(e.target)) {
            clickedInside = true;
          }
        });
      });
      if (!clickedInside) setOpenBadge({});
    }
    document.addEventListener('mousedown', handleClick);
    return () => document.removeEventListener('mousedown', handleClick);
  }, []);

  // Helper to get/create refs for each badge
  const getBadgeRef = (monitorId, badge) => {
    if (!badgeRefs.current[monitorId]) badgeRefs.current[monitorId] = {};
    if (!badgeRefs.current[monitorId][badge]) badgeRefs.current[monitorId][badge] = React.createRef();
    return badgeRefs.current[monitorId][badge];
  };

  return (
    <div>
      <div className="grid gap-10 md:grid-cols-2">
        {loading ? (
          <div className="text-center text-gray-500 dark:text-gray-400">Loading...</div>
        ) : error ? (
          <div className="text-center text-red-500 dark:text-red-400">{error}</div>
        ) : data && data.publicGroupList && data.publicGroupList.length > 0 ? (
          data.publicGroupList.map((group) => (
            <section key={group.id} className="bg-white dark:bg-slate-800 rounded-2xl shadow-lg p-8 flex flex-col">
              <h2 className="text-2xl font-bold mb-6 text-gray-900 dark:text-white">{group.name}</h2>
              <ul className="space-y-6">
                {group.monitorList.map((monitor) => (
                  <li key={monitor.id} className="flex flex-col gap-2 bg-gray-50 dark:bg-slate-900 rounded-xl px-5 py-4 shadow border border-gray-200 dark:border-gray-700">
                    {/* First row: 5-column grid for fixed badge placement, name gets col-span-2 */}
                    <div className="grid grid-cols-5 items-center w-full gap-2">
                      {/* Name (left, col-span-2, min-w-0) */}
                      <div className="flex items-center min-w-0 col-span-2">
                        <span className={`inline-block w-3 h-3 rounded-full ${getStatusColor(monitor.validCert)} mr-2`}></span>
                        <span className="font-semibold text-lg text-gray-800 dark:text-gray-100 truncate">{monitor.name}</span>
                      </div>
                      {/* Cert Exp (center-left, col-span-1) */}
                      <div className="flex flex-col items-center col-span-1 relative">
                        {monitor.certExpiryDaysRemaining !== undefined && (
                          <>
                            {/* Desktop: full badge */}
                            <span className={`hidden sm:inline px-2 py-0 rounded-full text-xs font-medium text-white ${getCertBg(monitor.certExpiryDaysRemaining)}`}
                              title={monitor.certExpiryDaysRemaining < 0 ? 'Certificate expired!' : `Certificate expires in ${monitor.certExpiryDaysRemaining} days`}>
                              {getCertText(monitor.certExpiryDaysRemaining)}
                            </span>
                            {/* Mobile: icon badge with click */}
                            <span
                              ref={getBadgeRef(monitor.id, 'cert')}
                              className={`sm:hidden px-2 py-0 rounded-full text-xs font-medium flex items-center justify-center ${getCertBg(monitor.certExpiryDaysRemaining)}`}
                              title={monitor.certExpiryDaysRemaining < 0 ? 'Certificate expired!' : `Certificate expires in ${monitor.certExpiryDaysRemaining} days`}
                              onClick={() => toggleBadge(monitor.id, 'cert')}
                              style={{ cursor: 'pointer', position: 'relative' }}
                            >
                              <FiAward className="w-4 h-4" />
                              {openBadge[monitor.id]?.cert && (
                                <span className="absolute bottom-full left-1/2 -translate-x-1/2 mb-2 z-20 bg-white dark:bg-slate-800 text-gray-900 dark:text-gray-100 rounded-lg shadow-lg px-3 py-2 text-xs whitespace-nowrap border border-gray-200 dark:border-gray-700">
                                  {monitor.certExpiryDaysRemaining < 0
                                    ? 'Expired!'
                                    : `Expires in ${monitor.certExpiryDaysRemaining} days`}
                                </span>
                              )}
                            </span>
                          </>
                        )}
                      </div>
                      {/* Avg. (center-right, col-span-1) */}
                      <div className="flex flex-col items-center col-span-1 relative">
                        {monitor.avgPing !== undefined && (
                          <>
                            {/* Desktop: full badge */}
                            <span className={`hidden sm:inline px-2 py-0 rounded-full text-xs font-medium ${getAvgPingBg(monitor.avgPing)}`}>
                              Avg.: {monitor.avgPing < 100 ? monitor.avgPing.toFixed(1) : Math.round(monitor.avgPing)} ms
                            </span>
                            {/* Mobile: icon badge with click */}
                            <span
                              ref={getBadgeRef(monitor.id, 'avg')}
                              className={`sm:hidden px-2 py-0 rounded-full text-xs font-medium flex items-center justify-center ${getAvgPingBg(monitor.avgPing)}`}
                              onClick={() => toggleBadge(monitor.id, 'avg')}
                              style={{ cursor: 'pointer', position: 'relative' }}
                            >
                              <FiActivity className="w-4 h-4" />
                              {openBadge[monitor.id]?.avg && (
                                <span className="absolute bottom-full left-1/2 -translate-x-1/2 mb-2 z-20 bg-white dark:bg-slate-800 text-gray-900 dark:text-gray-100 rounded-lg shadow-lg px-3 py-2 text-xs whitespace-nowrap border border-gray-200 dark:border-gray-700">
                                  {monitor.avgPing < 100 ? monitor.avgPing.toFixed(1) : Math.round(monitor.avgPing)} ms
                                </span>
                              )}
                            </span>
                          </>
                        )}
                      </div>
                      {/* 24h (right, col-span-1) */}
                      <div className="flex flex-col items-center col-span-1 relative">
                        {monitor.uptime24h !== undefined && (
                          <>
                            {/* Desktop: full badge */}
                            <span className={`hidden sm:inline px-2 py-0 rounded-full text-xs font-medium ${getUptime24hBg(monitor.uptime24h)}`}>
                              24h: {monitor.uptime24h.toFixed(1)}%
                            </span>
                            {/* Mobile: icon badge with click */}
                            <span
                              ref={getBadgeRef(monitor.id, 'uptime')}
                              className={`sm:hidden px-2 py-0 rounded-full text-xs font-medium flex items-center justify-center ${getUptime24hBg(monitor.uptime24h)}`}
                              onClick={() => toggleBadge(monitor.id, 'uptime')}
                              style={{ cursor: 'pointer', position: 'relative' }}
                            >
                              <FiPercent className="w-4 h-4" />
                              {openBadge[monitor.id]?.uptime && (
                                <span className="absolute bottom-full left-1/2 -translate-x-1/2 mb-2 z-20 bg-white dark:bg-slate-800 text-gray-900 dark:text-gray-100 rounded-lg shadow-lg px-3 py-2 text-xs whitespace-nowrap border border-gray-200 dark:border-gray-700">
                                  {monitor.uptime24h.toFixed(1)}%
                                </span>
                              )}
                            </span>
                          </>
                        )}
                      </div>
                    </div>
                    {/* Second row: Heartbeat bar, always full width, evenly distributed squares */}
                    {monitor.heartbeatHistory && monitor.heartbeatHistory.length > 0 && (
                      <div className="flex items-center w-full">
                        <div className="grid grid-cols-40 gap-x-0.5 bg-gray-200 dark:bg-slate-700 rounded px-2 py-1 w-full overflow-x-auto">
                          {Array.from({ length: 40 }).map((_, i) => {
                            const hbArr = monitor.heartbeatHistory ?? [];
                            const hb = hbArr.slice(-40)[i];
                            return (
                              <span
                                key={i}
                                title={hb ? `Status: ${hb.status === 1 ? 'Up' : 'Down'}\nTime: ${hb.time}` : ''}
                                className={`w-full h-4 rounded-sm ${hb ? getHeartbeatColor(hb.status) : 'bg-gray-400 dark:bg-gray-600'} block`}
                              ></span>
                            );
                          })}
                        </div>
                      </div>
                    )}
                  </li>
                ))}
              </ul>
            </section>
          ))
        ) : (
          <div className="text-center text-gray-500 dark:text-gray-400">No status data available</div>
        )}
      </div>
      <style>{`
        .grid-cols-40 {
          display: grid;
          grid-template-columns: repeat(40, minmax(0, 1fr));
        }
      `}</style>
    </div>
  );
} 