import { App } from "astal/gtk4"
import style from "./style.scss"
import Bar from "./components/bar/Bar";
import AstalHyprland from "gi://AstalHyprland?version=0.1";
import { hyprToGdk } from "./util/hyprland";
import Brightness from "./util/brightness";

App.start({
    instanceName: "runner",
    css: style,
    main() {
        const hypr = AstalHyprland.get_default();
        const bars = new Map<number, string>();

        const barCreator = ( monitor: AstalHyprland.Monitor ) => {
            const gdkMonitor = hyprToGdk( monitor );
            if ( gdkMonitor ) {
                print( 'Bar added for screen ' + monitor.get_id() );
                bars.set( monitor.get_id(), Bar.BarLauncher( gdkMonitor ) );
            }
        }

        for (const monitor of hypr.monitors) {
            barCreator( monitor );
        }

        hypr.connect( 'monitor-added', ( _, monitor ) => {
            barCreator( monitor );
        } );

        hypr.connect( 'monitor-removed', ( _, monitor ) => {
            const windowName = bars.get( monitor );
            if ( windowName ) {
                const win = App.get_window( windowName );
                if ( win ) {
                    App.toggle_window( windowName );
                    win.set_child( null );
                    App.remove_window( win );
                    print( 'Bar removed for screen', monitor );
                }
                bars.delete( monitor );
            }
        } );

        // const monitors = App.get_monitors();
        // print( "adding bar to monitors" );
        // for (let index = 0; index < monitors.length; index++) {
        //     Bar.BarLauncher( monitors[ index ] );
        // }
        // Launcher();
    },
    requestHandler(request, res) {
        const args = request.trimStart().split( ' ' );

        if ( args[ 0 ] === 'notifier' ) {
            res( 'Not available here yet, run astal -i notifier ' + args[ 1 ] );
            // res( notifications.cliHandler( args ) );
        } else if ( args[ 0 ] === 'bar' ) {
            res( Bar.cliHandler( args ) );
        } else if ( args[ 0 ] === 'brightness' ) {
            try {
                const brightness = Brightness.get_default();
                if ( brightness.screenAvailable ) {
                    if ( args[ 1 ] === 'increase' ) {
                        brightness.screen += args.length > 1 ? parseInt( args[ 2 ] ) / 100 : 1;
                    } else if ( args[ 1 ] === 'decrease' ) {
                        brightness.screen -= args.length > 1 ? parseInt( args[ 2 ] ) / 100 : 1;
                    } else if ( args[ 1 ] === 'set' ) {
                        if ( args.length > 1 ) {
                            brightness.screen = parseInt( args[ 2 ] ) / 100;
                        } else {
                            res( 'Argument <brightness> unspecified' );
                            return;
                        }
                    } else {
                        res( 'Unknown command ' + args[ 1 ] );
                        return;
                    }
                    res( 'Ok' );
                } else {
                    res( 'No controllable screen available' );
                }
            } catch ( e ) {
                res( 'Error running brightness change' );
            }
        }
        // } else if ( args[ 0 ] === 'launcher' ) {
        //     if ( args[ 1 ] === 'show' ) {
        //         App.get_window( 'launcher' )?.show();
        //         res( '[Launcher] Shown' );
        //     } else if ( args[ 1 ] === 'hide' ) {
        //         App.get_window( 'launcher' )?.hide();
        //         res( '[Launcher] Hidden' );
        //     } else if ( args[ 1 ] === 'toggle' ) {
        //         App.toggle_window( 'launcher' );
        //         res( '[Launcher] Toggled' );
        //     } else {
        //         res( '[Launcher] unknown command' );
        //     }
        // }
    },
})

