import AstalNetwork from "gi://AstalNetwork"
import { createBinding } from "ags"

export default function Network() {
  const net = AstalNetwork.get_default()

  const text = createBinding(net, "primary")((type: AstalNetwork.Primary) => {
    if (type === AstalNetwork.Primary.WIFI) {
      const wifi = net.wifi
      if (!wifi) return "󰤮 offline"
      const ssid = wifi.ssid || "wifi"
      const strength = wifi.strength
      let icon = "󰤯"
      if (strength > 80) icon = "󰤨"
      else if (strength > 60) icon = "󰤥"
      else if (strength > 40) icon = "󰤢"
      else if (strength > 20) icon = "󰤟"
      return `${icon} ${ssid}`
    }
    if (type === AstalNetwork.Primary.WIRED) return "󰈀 eth"
    return "󰤮 offline"
  })

  const cls = createBinding(net, "primary")((type: AstalNetwork.Primary) => {
    if (type === AstalNetwork.Primary.WIFI) return "network wifi"
    if (type === AstalNetwork.Primary.WIRED) return "network wired"
    return "network disconnected"
  })

  return <label class={cls} label={text} />
}
