import app from "ags/gtk3/app"
import { Astal, Gtk, Gdk } from "ags/gtk3"
import Clock from "./modules/Clock"
// import Battery from "./modules/Battery"  // desktop — no battery
import Volume from "./modules/Volume"
import Network from "./modules/Network"
import SystemStats from "./modules/SystemStats"
import Media from "./modules/Media"
// import SysTray from "./modules/SysTray"  // TODO: needs astal-tray (appmenu-glib-translator)
import Prompt from "./modules/Prompt"

export default function Bar(gdkmonitor: Gdk.Monitor) {
  const { TOP, LEFT, RIGHT } = Astal.WindowAnchor

  let hideTimeout: number | null = null

  function scheduleHide(win: Astal.Window) {
    if (hideTimeout) clearTimeout(hideTimeout)
    hideTimeout = setTimeout(() => {
      win.visible = false
      hideTimeout = null
    }, 800)
  }

  function cancelHide() {
    if (hideTimeout) {
      clearTimeout(hideTimeout)
      hideTimeout = null
    }
  }

  return (
    <window
      class="Bar"
      name="bar"
      visible={true}
      gdkmonitor={gdkmonitor}
      exclusivity={Astal.Exclusivity.EXCLUSIVE}
      anchor={TOP | LEFT | RIGHT}
      application={app}
      layer={Astal.Layer.TOP}
    >
      <eventbox
        onHover={() => cancelHide()}
        onHoverLost={(_self) => {
          // disabled for debug — auto-hide off
        }}
      >
        <centerbox>
          <box $type="start" class="modules-left" halign={Gtk.Align.START}>
            <Prompt />
            <label class="separator" label="│" />
            <SystemStats />
            <label class="separator" label="│" />
            <Media />
          </box>
          <box $type="center" class="modules-center">
            <Clock />
          </box>
          <box $type="end" class="modules-right" halign={Gtk.Align.END}>
            {/* <SysTray /> */}
            <Network />
            <label class="separator" label="│" />
            <Volume />
          </box>
        </centerbox>
      </eventbox>
    </window>
  )
}
