#include "states.h"

#include <stdio.h>
#include <assert.h>

Presentation_State_Machine presentation_state_machine = (Presentation_State_Machine) {
    .current = NULL
};

Presentation_State presentation_state_create(
    const char *name,
    void *context,
    Presentation_State_Enter_Callback enter,
    Presentation_State_Tick_Callback tick,
    Presentation_State_Render_Callback render,
    Presentation_State_Exit_Callback exit
) {
    return (Presentation_State) {
        .name = name,
        .context = context,
        .enter = enter,
        .tick = tick,
        .render = render,
        .exit = exit,
    };
}

bool presentation_state_machine_go_to(Presentation_State *target) {
    if(presentation_state_machine.current == target) {
        return false;
    }

    if(presentation_state_machine.current != NULL) {
        assert(presentation_state_machine.current->exit != NULL);
        presentation_state_machine.current->exit(presentation_state_machine.current);
    }

    presentation_state_machine.current = target;

    if(presentation_state_machine.current == NULL) {
        return true; // Return true here even though it's NULL. This is nice for when we want to exit the program and the current state.
    }

    assert(presentation_state_machine.current->enter != NULL);
    presentation_state_machine.current->enter(presentation_state_machine.current);
    
    return true;
}