import { HStack, Image, Spacer, Text } from "@expo/ui/swift-ui";
import {
  font,
  foregroundStyle,
  lineLimit,
  minimumScaleFactor,
  monospacedDigit,
  padding,
  widgetAccentedRenderingMode,
} from "@expo/ui/swift-ui/modifiers";
import { createLiveActivity, type LiveActivityEnvironment } from "expo-widgets";

import type { TimeClockActivityProps } from "@/lib/time-clock-live-activity.types";

function TimeClockActivity(props: TimeClockActivityProps, _environment: LiveActivityEnvironment) {
  "widget";

  const green = "green";
  const title = props.description.trim() || "Clock In";
  const clientLabel = props.clientName.trim() || title;
  const subtitle = props.invoiceLabel.trim();

  const timerMods = [
    font({ design: "monospaced", weight: "bold", size: 20 }),
    monospacedDigit(),
    foregroundStyle(green),
    lineLimit(1),
    minimumScaleFactor(0.85),
  ];
  const compactTimerMods = [
    font({ design: "monospaced", weight: "semibold", size: 11 }),
    monospacedDigit(),
    foregroundStyle(green),
    lineLimit(1),
    minimumScaleFactor(0.8),
  ];
  const clientMods = [
    font({ weight: "semibold", size: 13 }),
    foregroundStyle({ type: "hierarchical", style: "primary" }),
    lineLimit(1),
    minimumScaleFactor(0.85),
  ];
  const subtitleMods = [
    font({ size: 11 }),
    foregroundStyle({ type: "hierarchical", style: "secondary" }),
    lineLimit(1),
    minimumScaleFactor(0.85),
  ];

  return {
    banner: (
      <HStack alignment="center" spacing={8} modifiers={[padding({ horizontal: 14, vertical: 12 })]}>
        <Image
          systemName="dollarsign.circle.fill"
          color={green}
          size={22}
          modifiers={[widgetAccentedRenderingMode("fullColor")]}
        />
        <Text modifiers={clientMods}>{clientLabel}</Text>
        <Spacer minLength={12} />
        <Text modifiers={timerMods}>{props.elapsedShort}</Text>
      </HStack>
    ),
    bannerSmall: (
      <HStack alignment="center" spacing={8} modifiers={[padding({ horizontal: 12, vertical: 10 })]}>
        <Image
          systemName="dollarsign.circle.fill"
          color={green}
          size={18}
          modifiers={[widgetAccentedRenderingMode("fullColor")]}
        />
        <Text modifiers={clientMods}>{clientLabel}</Text>
        <Spacer minLength={8} />
        <Text modifiers={compactTimerMods}>{props.elapsedShort}</Text>
      </HStack>
    ),
    compactLeading: (
      <Image
        systemName="dollarsign.circle.fill"
        color={green}
        size={15}
        modifiers={[widgetAccentedRenderingMode("fullColor")]}
      />
    ),
    compactTrailing: <Text modifiers={compactTimerMods}>{props.elapsedShort}</Text>,
    minimal: (
      <Image
        systemName="dollarsign.circle.fill"
        color={green}
        size={12}
        modifiers={[widgetAccentedRenderingMode("fullColor")]}
      />
    ),
    expandedLeading: (
      <Image
        systemName="dollarsign.circle.fill"
        color={green}
        size={20}
        modifiers={[widgetAccentedRenderingMode("fullColor")]}
      />
    ),
    expandedCenter: <Text modifiers={clientMods}>{clientLabel}</Text>,
    expandedTrailing: <Text modifiers={timerMods}>{props.elapsedShort}</Text>,
    expandedBottom: (
      <Text modifiers={subtitleMods}>{subtitle || "beenvoice"}</Text>
    ),
  };
}

export default createLiveActivity("TimeClockActivity", TimeClockActivity);
