#include <akgltest.h>

akerr_ErrorContext AKERR_NOIGNORE *akgltest_menupointer_logic_movement(akgl_Actor *obj, float32_t dt)
{
    PREPARE_ERROR(e);
    FAIL_ZERO_RETURN(e, obj, AKERR_NULLPOINTER, "NULL actor");
    if ( AKGL_BITMASK_HAS(obj->state, AKGL_ACTOR_STATE_MOVING_DOWN) ) {
	if ( menuselection < maxmenu ) {
	    menuselection += 1;
	} else {
	    menuselection = 0;
	}
    } else if ( AKGL_BITMASK_HAS(obj->state, AKGL_ACTOR_STATE_MOVING_UP) ) {
	if ( menuselection == 0 ) {
	    menuselection = maxmenu;
	} else {
	    menuselection -= 1;
	}
    }
    obj->y = 85 + (menuselection * 100);
    AKGL_BITMASK_DEL(obj->state, (AKGL_ACTOR_STATE_MOVING_UP | AKGL_ACTOR_STATE_MOVING_DOWN));
    FAIL(e, AKGL_ERR_LOGICINTERRUPT, "NULL");
    SUCCEED_RETURN(e);
}

akerr_ErrorContext AKERR_NOIGNORE *akgltest_init_menupointer(void)
{
    PREPARE_ERROR(e);
    akgl_Actor *actorptr = NULL;
    PASS(e, akgl_heap_next_actor(&actorptr));
    PASS(e, akgl_actor_initialize((akgl_Actor *)actorptr, "menupointer"));
    actorptr->basechar = SDL_GetPointerProperty(
	AKGL_REGISTRY_CHARACTER,
	"menupointer",
	NULL);
    FAIL_ZERO_RETURN(e, actorptr->basechar, AKGL_ERR_REGISTRY, "menupointer character missing");
    actorptr->movementlogicfunc = &akgltest_menupointer_logic_movement;
    actorptr->movement_controls_face = false;
    actorptr->state = (AKGL_ACTOR_STATE_ALIVE | AKGL_ACTOR_STATE_FACE_DOWN);
    actorptr->x = 50;
    actorptr->y = 100;
    actorptr->layer = 1;
    actorptr->visible = false;
    SUCCEED_RETURN(e);
}

akerr_ErrorContext AKERR_NOIGNORE *akgltest_init_player(void)
{
    PREPARE_ERROR(e);
    akgl_Actor *actor = NULL;
    
    akgl_Actor *actorptr = NULL;
    PASS(e, akgl_heap_next_actor(&actorptr));
    PASS(e, akgl_actor_initialize((akgl_Actor *)actorptr, "player"));
    actorptr->basechar = SDL_GetPointerProperty(
	AKGL_REGISTRY_CHARACTER,
	"little guy",
	NULL);
    FAIL_ZERO_RETURN(e, actorptr->basechar, AKGL_ERR_REGISTRY, "little guy character missing");
    actorptr->movement_controls_face = false;
    actorptr->state = (AKGL_ACTOR_STATE_ALIVE | AKGL_ACTOR_STATE_FACE_LEFT);
    actorptr->x = 320;
    actorptr->y = 240;
    actorptr->layer = 1;
    actorptr->visible = false;    
    SUCCEED_RETURN(e);
}

akerr_ErrorContext AKERR_NOIGNORE *akgltest_release_actor(char *actorname)
{
    PREPARE_ERROR(e);
    akgl_Actor *actor = SDL_GetPointerProperty(AKGL_REGISTRY_ACTOR, actorname, NULL);
    FAIL_ZERO_RETURN(e, actor, AKGL_ERR_REGISTRY, "%s", actorname);
    PASS(e, akgl_heap_release_actor(actor));
    SUCCEED_RETURN(e);
}

akerr_ErrorContext AKERR_NOIGNORE *akgltest_set_actor_visible(char *actorname, bool visible)
{
    PREPARE_ERROR(e);
    akgl_Actor *actorptr = NULL;
    actorptr = SDL_GetPointerProperty(AKGL_REGISTRY_ACTOR, actorname, NULL);
    FAIL_ZERO_RETURN(e, actorptr, AKERR_NULLPOINTER, "%s missing in registry", actorname);
    actorptr->visible = visible;
    SUCCEED_RETURN(e);
}

