import Gtk from "gi://Gtk";
import AstalNetwork from "gi://AstalNetwork";
import AstalBluetooth from "gi://AstalBluetooth";
import { exec } from "gi://GLib";

let quickActionsMenu;

// Toggle WiFi connection
function toggleWiFi() {
    const network = AstalNetwork.get_default();
    const wifi = network.get_wifi()!;
    const state = wifi.get_state();

    if (state === AstalNetwork.WifiState.DISCONNECTED) {
        wifi.connect();
    } else {
        wifi.disconnect();
    }
}

// Toggle Bluetooth power state
function toggleBluetooth() {
    const bluetooth = AstalBluetooth.get_default();
    const adapter = bluetooth.get_adapter();
    const powered = adapter?.get_powered();

    adapter?.set_powered(!powered);
}

// Adjust volume or microphone (opens pavucontrol)
function adjustVolume() {
    exec("pavucontrol");
}

function adjustMic() {
    exec("pavucontrol");
}

// Power menu
function showPowerMenu() {
    exec("power-menu");
}

// Show WiFi network picker
function pickWiFi() {
    const wifi = AstalNetwork.get_default().get_wifi();
    wifi.show_picker();
}

// Show Bluetooth device picker
function pickBluetooth() {
    const bluetooth = AstalBluetooth.get_default();
    bluetooth.show_picker();
}

// Create menu using JSX
function createQuickActionsMenu() {
    quickActionsMenu = (
        <menu>
            <item label="📶 WiFi" onActivate={toggleWiFi} />
            <item label="→ Pick Network" onActivate={pickWiFi} />

            <item label="🔵 Bluetooth" onActivate={toggleBluetooth} />
            <item label="→ Pick Device" onActivate={pickBluetooth} />

            <item label="🔊 Volume" onActivate={adjustVolume} />
            <item label="🎙️ Microphone" onActivate={adjustMic} />
            <item label="🔌 Power" onActivate={showPowerMenu} />
        </menu>
    );

    return quickActionsMenu;
}

// Export the function
export { createQuickActionsMenu };
