#import "Basic";
#import,file "../module.jai";

// State shared between the main loop and D-Bus callbacks.
// Callbacks are #c_call so they can't use Jai allocators directly —
// communicate via a plain struct instead.
Player_State :: struct {
    should_play_pause : bool;
    should_next       : bool;
    should_previous   : bool;
}

on_play_pause :: (ud: *void) #c_call {
    state := cast(*Player_State) ud;
    state.should_play_pause = true;
}
on_next :: (ud: *void) #c_call {
    (cast(*Player_State) ud).should_next = true;
}
on_previous :: (ud: *void) #c_call {
    (cast(*Player_State) ud).should_previous = true;
}

main :: () {
    player := mpris_player_create("ExamplePlayer", "Example Music Player");
    if !player { log_error("Failed to create MPRIS player"); return; }
    defer mpris_player_destroy(player);

    state: Player_State;
    mpris_on_play_pause(player, on_play_pause, *state);
    mpris_on_next      (player, on_next,       *state);
    mpris_on_previous  (player, on_previous,   *state);

    mpris_set_playback_status(player, "Playing");
    mpris_set_can_go_next    (player, true);
    mpris_set_can_go_previous(player, true);

    meta: Mpris_Metadata;
    meta.title     = "Some Song";
    meta.artist    = "Some Artist";
    meta.album     = "Some Album";
    meta.length_us = 210 * 1_000_000;
    mpris_set_metadata(player, meta);

    print("Player registered. Press Ctrl+C to stop.\n");

    while true {
        mpris_process(player);

        if state.should_play_pause {
            state.should_play_pause = false;
            print("PlayPause\n");
        }
        if state.should_next {
            state.should_next = false;
            print("Next\n");
        }
        if state.should_previous {
            state.should_previous = false;
            print("Previous\n");
        }
    }
}
