import QtQuick
import Quickshell
import Quickshell.Io
import Quickshell.Wayland
import qs.Commons
import qs.Services

Item {
  id: root

  // Using Wayland protocols to get focused window then determine which screen it's on.
  function getActiveScreen() {
    const activeWindow = ToplevelManager.activeToplevel
    if (activeWindow && activeWindow.screens.length > 0) {
      return activeWindow.screens[0]
    }

    // Fall back to the primary screen
    return Quickshell.screens[0]
  }

  IpcHandler {
    target: "screenRecorder"
    function toggle() {
      ScreenRecorderService.toggleRecording()
    }
  }

  IpcHandler {
    target: "settings"
    function toggle() {
      settingsPanel.toggle(getActiveScreen())
    }
  }

  IpcHandler {
    target: "notifications"
    function toggleHistory() {
      notificationHistoryPanel.toggle(getActiveScreen())
    }
    function toggleDoNotDisturb() {// TODO
    }
  }

  IpcHandler {
    target: "idleInhibitor"
    function toggle() {
      return IdleInhibitorService.manualToggle()
    }
  }

  IpcHandler {
    target: "launcher"
    function toggle() {
      launcherPanel.toggle(getActiveScreen())
    }
    function clipboard() {
      launcherPanel.setSearchText(">clip ")
      launcherPanel.toggle(getActiveScreen())
    }
    function calculator() {
      launcherPanel.setSearchText(">calc ")
      launcherPanel.toggle(getActiveScreen())
    }
  }

  IpcHandler {
    target: "lockScreen"
    function toggle() {
      // Only lock if not already locked (prevents the red screen issue)
      // Note: No unlock via IPC for security reasons
      if (!lockScreen.active) {
        lockScreen.active = true
      }
    }
  }

  IpcHandler {
    target: "brightness"
    function increase() {
      BrightnessService.increaseBrightness()
    }
    function decrease() {
      BrightnessService.decreaseBrightness()
    }
  }

  IpcHandler {
    target: "darkMode"
    function toggle() {
      Settings.data.colorSchemes.darkMode = !Settings.data.colorSchemes.darkMode
    }
    function setDark() {
      Settings.data.colorSchemes.darkMode = true
    }
    function setLight() {
      Settings.data.colorSchemes.darkMode = false
    }
  }

  IpcHandler {
    target: "volume"
    function increase() {
      AudioService.increaseVolume()
    }
    function decrease() {
      AudioService.decreaseVolume()
    }
    function muteOutput() {
      AudioService.setMuted(!AudioService.muted)
    }
    function muteInput() {
      if (AudioService.source?.ready && AudioService.source?.audio) {
        AudioService.source.audio.muted = !AudioService.source.audio.muted
      }
    }
  }

  IpcHandler {
    target: "powerPanel"
    function toggle() {
      powerPanel.toggle(getActiveScreen())
    }
  }

  IpcHandler {
    target: "sidePanel"
    function toggle() {
      sidePanel.toggle(getActiveScreen())
    }
  }

  // Wallpaper IPC: trigger a new random wallpaper
  IpcHandler {
    target: "wallpaper"
    function random() {
      if (Settings.data.wallpaper.enabled) {
        WallpaperService.setRandomWallpaper()
      }
    }

    function set(path: string, screen: string) {
      if (screen === "all" || screen === "") {
        screen = undefined
      }
      WallpaperService.changeWallpaper(path, screen)
    }
  }
}
