#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <SDL3/SDL_properties.h>
#include <SDL3_image/SDL_image.h>
#include <SDL3_mixer/SDL_mixer.h>

#include <akerror.h>
#include <akgl/assets.h>
#include <akgl/iterator.h>
#include <akgl/tilemap.h>
#include <akgl/heap.h>
#include <akgl/game.h>
#include <akgl/controller.h>
#include <akgl/draw.h>
#include <akgl/sprite.h>
#include <akgl/actor.h>
#include <akgl/registry.h>
#include <akgl/error.h>

int numsprites = 8;
char *spritepaths[] = {
    "assets/sprites/little_guy_walking_left.json",
    "assets/sprites/little_guy_walking_right.json",
    "assets/sprites/little_guy_walking_up.json",
    "assets/sprites/little_guy_walking_down.json",
    "assets/sprites/little_guy_facing_left.json",
    "assets/sprites/little_guy_facing_right.json",
    "assets/sprites/little_guy_facing_up.json",
    "assets/sprites/little_guy_facing_down.json"
};

int main(void)
{
    PREPARE_ERROR(errctx);
    SDL3GControlMap *controlmap;
    actor *actorptr = NULL;

    ATTEMPT {

	SDL_SetAppMetadata("SDL3-GameTest", "0.1", "net.aklabs.sdl3-gametest");

	if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO )) {
	    FAIL_BREAK(errctx, AKGL_ERR_SDL, "Couldn't initialize SDL: %s", SDL_GetError());
	}

	if (!SDL_CreateWindowAndRenderer("net/aklabs/libakgl/test_sprite", 640, 480, 0, &window, &renderer)) {
	    FAIL_BREAK(errctx, AKGL_ERR_SDL, "Couldn't create window/renderer: %s", SDL_GetError());
	}

	CATCH(errctx, akgl_heap_init());
	CATCH(errctx, akgl_registry_init());

	strcpy((char *)&game.name, "charviewer");
	strcpy((char *)&game.version, "0.0.1");
	strcpy((char *)&game.uri, "net.aklabs.libakgl.charviewer");
	game.screenwidth = 640;
	game.screenheight = 480;

	CATCH(errctx, akgl_GAME_init());

	for ( int i = 0; i < numsprites ; i++) {
	    CATCH(errctx, akgl_sprite_load_json(spritepaths[i]));
	}
	CATCH(errctx, akgl_character_load_json("assets/characters/littleguy.json"));
	CATCH(errctx, akgl_heap_next_actor(&actorptr));
	CATCH(errctx, akgl_actor_initialize((actor *)actorptr, "player"));
	actorptr->basechar = SDL_GetPointerProperty(
	    AKGL_REGISTRY_CHARACTER,
	    "little guy",
	    NULL);
	FAIL_ZERO_BREAK(errctx, actorptr->basechar, AKGL_ERR_REGISTRY, "Can't load character 'little guy' from the registry");
	actorptr->movement_controls_face = false;
	actorptr->state = (AKGL_ACTOR_STATE_ALIVE | AKGL_ACTOR_STATE_FACE_LEFT);
	actorptr->x = 320;
	actorptr->y = 240;
	actorptr->visible = true;

	// set up the control map
	controlmap = &akgl_controlmaps[0];
	controlmap->kbid = 0;
	controlmap->target = SDL_GetPointerProperty(AKGL_REGISTRY_ACTOR, "player", NULL);
	// Move down
	controlmap->controls[0].key = SDLK_DOWN;
	//controlmap->controls[0].target_state_gate = AKGL_ACTOR_STATE_MOVING_DOWN;
	controlmap->controls[0].target_add_state_on = AKGL_ACTOR_STATE_MOVING_DOWN | AKGL_ACTOR_STATE_FACE_DOWN;
	controlmap->controls[0].target_del_state_on = AKGL_ACTOR_STATE_MOVING_UP | AKGL_ACTOR_STATE_FACE_ALL;
	controlmap->controls[0].target_del_state_off = AKGL_ACTOR_STATE_MOVING_DOWN;
	controlmap->controls[0].event_on = SDL_EVENT_KEY_DOWN;
	controlmap->controls[0].event_off = SDL_EVENT_KEY_UP;

	// Move up
	controlmap->controls[1].key = SDLK_UP;
	//controlmap->controls[1].target_state_gate = AKGL_ACTOR_STATE_MOVING_UP;
	controlmap->controls[1].target_add_state_on = AKGL_ACTOR_STATE_MOVING_UP | AKGL_ACTOR_STATE_FACE_UP;
	controlmap->controls[1].target_del_state_on = AKGL_ACTOR_STATE_MOVING_DOWN | AKGL_ACTOR_STATE_FACE_ALL;
	controlmap->controls[1].target_del_state_off = AKGL_ACTOR_STATE_MOVING_UP;
	controlmap->controls[1].event_on = SDL_EVENT_KEY_DOWN;
	controlmap->controls[1].event_off = SDL_EVENT_KEY_UP;

	// Move left
	controlmap->controls[2].key = SDLK_LEFT;
	//controlmap->controls[2].target_state_gate = AKGL_ACTOR_STATE_MOVING_LEFT;
	controlmap->controls[2].target_add_state_on = AKGL_ACTOR_STATE_MOVING_LEFT | AKGL_ACTOR_STATE_FACE_LEFT;
	controlmap->controls[2].target_del_state_on = AKGL_ACTOR_STATE_MOVING_RIGHT | AKGL_ACTOR_STATE_FACE_ALL;
	controlmap->controls[2].target_del_state_off = AKGL_ACTOR_STATE_MOVING_LEFT;
	controlmap->controls[2].event_on = SDL_EVENT_KEY_DOWN;
	controlmap->controls[2].event_off = SDL_EVENT_KEY_UP;

	// Move right
	controlmap->controls[3].key = SDLK_RIGHT;
	//controlmap->controls[3].target_state_gate = AKGL_ACTOR_STATE_MOVING_RIGHT;
	controlmap->controls[3].target_add_state_on = AKGL_ACTOR_STATE_MOVING_RIGHT | AKGL_ACTOR_STATE_FACE_RIGHT;
	controlmap->controls[3].target_del_state_on = AKGL_ACTOR_STATE_MOVING_LEFT | AKGL_ACTOR_STATE_FACE_ALL;
	controlmap->controls[3].target_del_state_off = AKGL_ACTOR_STATE_MOVING_RIGHT;
	controlmap->controls[3].event_on = SDL_EVENT_KEY_DOWN;
	controlmap->controls[3].event_off = SDL_EVENT_KEY_UP;
    } CLEANUP {
    } PROCESS(errctx) {
    } HANDLE_DEFAULT(errctx) {
	LOG_ERROR(errctx);
	return 1;
    } FINISH_NORETURN(errctx);

    return 0;
}
