"use client";

import React, { useId } from "react";
import * as T from "@/lib/themeTokens";

export interface DayNightSwitchProps {
  isNight: boolean;
  onToggle: (checked: boolean) => void;
  defaultNight?: boolean;
  className?: string;
  style?: React.CSSProperties;
  size?: number | "xs" | "sm" | "md" | "lg";
}

export function DayNightSwitch({
  isNight,
  onToggle,
  defaultNight = false,
  className = "",
  style = {},
  size = "md",
}: DayNightSwitchProps) {
  const l = useId();
  const sunId = `light-${l}`;
  const moonId = `dark-${l}`;

  const fontSize =
    typeof size === "number"
      ? size
      : { xs: 20, sm: 24, md: 30, lg: 36 }[size] || 30;

  return (
    <>
      <svg
        aria-hidden="true"
        width="0"
        height="0"
        style={{
          position: "absolute",
          width: 0,
          height: 0,
          overflow: "hidden",
        }}
      >
        <symbol id={sunId} viewBox="0 0 24 24">
          <g
            stroke="currentColor"
            strokeWidth="2"
            strokeLinecap="round"
          >
            <line x1="12" y1="17" x2="12" y2="20" transform="rotate(0,12,12)" />
            <line x1="12" y1="17" x2="12" y2="20" transform="rotate(45,12,12)" />
            <line x1="12" y1="17" x2="12" y2="20" transform="rotate(90,12,12)" />
            <line x1="12" y1="17" x2="12" y2="20" transform="rotate(135,12,12)" />
            <line x1="12" y1="17" x2="12" y2="20" transform="rotate(180,12,12)" />
            <line x1="12" y1="17" x2="12" y2="20" transform="rotate(225,12,12)" />
            <line x1="12" y1="17" x2="12" y2="20" transform="rotate(270,12,12)" />
            <line x1="12" y1="17" x2="12" y2="20" transform="rotate(315,12,12)" />
          </g>
          <circle fill="currentColor" cx="12" cy="12" r="5" />
        </symbol>
        <symbol id={moonId} viewBox="0 0 24 24">
          <path
            fill="currentColor"
            d="M12.3,4.9c0.4-0.2,0.6-0.7,0.5-1.1c-0.1-0.5-0.6-0.8-1.1-0.7C7,3.8,3.9,7.3,3.9,11.8c0,5,4.1,9.1,9.1,9.1c4.5,0,8-3.1,8.7-7.8c0.1-0.5-0.2-1-0.7-1.1c-0.5-0.1-0.9,0.1-1.1,0.5c-1,1.9-3,3.3-5.3,3.3c-3.3,0-6-2.7-6-6C8.6,7.5,10.2,5.7,12.3,4.9z"
          />
        </symbol>
      </svg>

      <label
        data-daynight-switch="true"
        className={`switch ${className}`}
        style={{
          fontSize: `${fontSize}px`,
          ...style,
        }}
      >
        <input
          type="checkbox"
          checked={isNight}
          onChange={(e) => onToggle(e.target.checked)}
          className="switch__input"
          name="dark"
          role="switch"
          aria-label="Toggle dark mode"
        />
        <svg width="24" height="24" aria-hidden="true" className="switch__icon switch__icon--sun">
          <use href={`#${sunId}`} />
        </svg>
        <svg width="24" height="24" aria-hidden="true" className="switch__icon switch__icon--moon">
          <use href={`#${moonId}`} />
        </svg>
        <span className="switch__inner" />
        <span className="switch__inner-icons">
          <svg width="24" height="24" aria-hidden="true" className="switch__icon switch__active-sun">
            <use href={`#${sunId}`} />
          </svg>
          <svg width="24" height="24" aria-hidden="true" className="switch__icon switch__active-moon">
            <use href={`#${moonId}`} />
          </svg>
        </span>
        <span className="switch__sr">Dark Mode</span>
      </label>
    </>
  );
}
