#define SDL_MAIN_USE_CALLBACKS
#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 <akgl/assets.h>
#include <akgl/error.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/text.h>

#define AKGLTEST_STATE_WAITFORINPUT                   1 << 8
#define AKGLTEST_STATE_LOADING                        1 << 9
#define AKGLTEST_STATE_RUNNING                        1 << 10
#define AKGLTEST_STATE_MAPMENU                        1 << 11
#define AKGLTEST_STATE_QUIT                           1 << 12

#define AKGLTEST_CONTROLMAP_MENU                      0
#define AKGLTEST_CONTROLMAP_INGAMEACTOR               1

int menuselection = 0;
int maxmenu = 2;

int numchars = 2;
char *characterpaths[] = {
    "assets/characters/littleguy.json",
    "assets/characters/menupointer.json"
};

int numsounds = 2;
char *soundfiles[] = {
    "assets/vgmenuhighlight.wav",
    "assets/vgmenuselect.wav"
};

int numsprites = 9;
char *spritepaths[] = {
  "assets/sprites/menupointer.json",
  "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"
};

char dirnamebuf[1024];

akerr_ErrorContext AKERR_NOIGNORE *akgltest_set_gamemode_menu(akgl_Actor *appstate, SDL_Event *event)
{
    akgl_Actor *menupointer = SDL_GetPointerProperty(AKGL_REGISTRY_ACTOR, "menupointer", NULL);
    akgl_Actor *player = SDL_GetPointerProperty(AKGL_REGISTRY_ACTOR, "player", NULL);
    PREPARE_ERROR(e);
    PASS(e, akgl_tilemap_release(&gamemap));
    FAIL_ZERO_RETURN(e, player, AKERR_NULLPOINTER, "missing actor");
    FAIL_ZERO_RETURN(e, menupointer, AKERR_NULLPOINTER, "missing actor");
    AKGL_BITMASK_DEL(game.state.flags, AKGLTEST_STATE_WAITFORINPUT);
    AKGL_BITMASK_DEL(game.state.flags, AKGLTEST_STATE_RUNNING);
    // Reset all the actors back to empty (except the player and menupointer)
    for ( int i = 0 ; i < AKGL_MAX_HEAP_ACTOR; i++ ) {
	if ( &HEAP_ACTOR[i] != player && &HEAP_ACTOR[i] != menupointer ) {
	    PASS(e, akgl_heap_release_actor(&HEAP_ACTOR[i]));
	    //SDL_ClearProperty(AKGL_REGISTRY_ACTOR, (char *)&HEAP_ACTOR[i].name);
	    //memset(&HEAP_ACTOR[i], 0x00, sizeof(akgl_Actor));
	} else {
	    SDL_Log("Preserving menupointer and actor (%p)", &HEAP_ACTOR[i]);
	}
    }
    GAME_ControlMaps[AKGLTEST_CONTROLMAP_INGAMEACTOR].target = NULL;
    GAME_ControlMaps[AKGLTEST_CONTROLMAP_MENU].target = menupointer;
    menupointer->visible = true;
    AKGL_BITMASK_ADD(game.state.flags, AKGLTEST_STATE_MAPMENU);
    SUCCEED_RETURN(e);
}    

akerr_ErrorContext AKERR_NOIGNORE *akgltest_set_gamemode_running(akgl_Actor *appstate, SDL_Event *event)
{
    akgl_Actor *menupointer = SDL_GetPointerProperty(AKGL_REGISTRY_ACTOR, "menupointer", NULL);
    akgl_Actor *actorptr = SDL_GetPointerProperty(AKGL_REGISTRY_ACTOR, "player", NULL);
    PREPARE_ERROR(e);
    FAIL_ZERO_RETURN(e, menupointer, AKERR_NULLPOINTER, "missing actor");
    FAIL_ZERO_RETURN(e, actorptr, AKERR_NULLPOINTER, "missing actor");
    AKGL_BITMASK_DEL(game.state.flags, AKGLTEST_STATE_WAITFORINPUT);
    AKGL_BITMASK_ADD(game.state.flags, AKGLTEST_STATE_MAPMENU);
    GAME_ControlMaps[AKGLTEST_CONTROLMAP_INGAMEACTOR].target = actorptr;
    GAME_ControlMaps[AKGLTEST_CONTROLMAP_MENU].target = NULL;
    menupointer->visible = false;
    actorptr->visible = true;

    AKGL_BITMASK_DEL(game.state.flags, AKGLTEST_STATE_MAPMENU);
    if ( menuselection == 0 ) {
	strcpy((char *)&dirnamebuf, "assets/imagemap.tmj");
	PASS(e, akgl_tilemap_load((char *)&dirnamebuf, (akgl_Tilemap *)&gamemap));
    } else if ( menuselection == 1 ) {
	strcpy((char *)&dirnamebuf, "assets/tilemap.tmj");
	PASS(e, akgl_tilemap_load((char *)&dirnamebuf, (akgl_Tilemap *)&gamemap));
    } else if ( menuselection == 2 ) {
	AKGL_BITMASK_DEL(game.state.flags, AKGLTEST_STATE_RUNNING);
	AKGL_BITMASK_ADD(game.state.flags, AKGLTEST_STATE_QUIT);
    }
    AKGL_BITMASK_ADD(game.state.flags, AKGLTEST_STATE_RUNNING);
    SUCCEED_RETURN(e);
}    

akerr_ErrorContext AKERR_NOIGNORE *akgltest_menupointer_logic_movement(akgl_Actor *obj, SDL_Time curtime)
{
    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));
    SUCCEED_RETURN(e);
}

akerr_ErrorContext AKERR_NOIGNORE *music_toggle(akgl_Actor *obj, SDL_Event *event)
{
    SDL_PropertiesID bgmprops = 0;
    PREPARE_ERROR(errctx);
    if ( MIX_TrackPlaying(akgl_tracks[AKGL_GAME_AUDIO_TRACK_BGM]) ) {
	FAIL_ZERO_RETURN(
	    errctx,
	    MIX_StopTrack(akgl_tracks[AKGL_GAME_AUDIO_TRACK_BGM], 0),
	    AKGL_ERR_SDL,
	    "%s",
	    SDL_GetError());
    } else {
	SDL_SetNumberProperty(bgmprops, MIX_PROP_PLAY_LOOPS_NUMBER, -1);
	FAIL_ZERO_RETURN(
	    errctx,
	    MIX_PlayTrack(akgl_tracks[AKGL_GAME_AUDIO_TRACK_BGM], bgmprops),
	    AKGL_ERR_SDL,
	    "%s",
	    SDL_GetError());
    }
    SUCCEED_RETURN(errctx);
}

akerr_ErrorContext AKERR_NOIGNORE *savegame(akgl_Actor *obj, SDL_Event *event)
{
    return akgl_game_save("assets/savegame.bin");
}

akerr_ErrorContext AKERR_NOIGNORE *quit_game(akgl_Actor *obj, SDL_Event *event)
{
    PREPARE_ERROR(errctx);
    AKGL_BITMASK_ADD(game.state.flags, AKGLTEST_STATE_QUIT);
    SDL_Log("Quitting game");
    SUCCEED_RETURN(errctx);
}

akerr_ErrorContext AKERR_NOIGNORE *akgltest_load_assets()
{
    PREPARE_ERROR(errctx);
    for ( int i = 0; i < numsprites ; i++) {
	strcpy((char *)&dirnamebuf, spritepaths[i]);
	SDL_Log("Loading sprite %s (%d of %d)....", (char *)&dirnamebuf, i, numsprites);
	PASS(errctx, akgl_sprite_load_json((char *)&dirnamebuf));
    }
    for ( int i = 0; i < numchars ; i++ ) {
	strcpy((char *)&dirnamebuf, characterpaths[i]);
	SDL_Log("Loading character %s (%d of %d)....", (char *)&dirnamebuf, i, numchars);
	PASS(errctx, akgl_character_load_json((char *)&dirnamebuf));
    }
    SUCCEED_RETURN(errctx);
}

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);
}

SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
    akgl_Actor *actorptr = NULL;
    *appstate = (void *)&game.state;
    PREPARE_ERROR(errctx);

    AKGL_BITMASK_ADD(game.state.flags, AKGLTEST_STATE_LOADING);
    
    ATTEMPT {
	FAIL_ZERO_BREAK(errctx, appstate, AKERR_NULLPOINTER, "NULL appstate pointer");

	strcpy((char *)&game.name, "sdl3-gametest");
	strcpy((char *)&game.version, "0.0.1");
	strcpy((char *)&game.uri, "net.aklabs.games.sdl3-gametest");

	//CATCH(errctx, akgl_game_load("assets/savegame.bin"));
	CATCH(errctx, akgl_game_init());
	CATCH(errctx, akgl_registry_load_properties("assets/properties.json"));
	CATCH(errctx, akgl_game_init_screen());
	CATCH(errctx, akgl_controller_list_keyboards());
	
	CATCH(errctx, akgltest_load_assets());
	
	CATCH(errctx, akgl_heap_next_actor(&actorptr));
	CATCH(errctx, akgl_actor_initialize((akgl_Actor *)actorptr, "menupointer"));
	actorptr->basechar = SDL_GetPointerProperty(
	    AKGL_REGISTRY_CHARACTER,
	    "menupointer",
	    NULL);
	FAIL_ZERO_BREAK(errctx, actorptr->basechar, AKERR_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->visible = false;
	
	CATCH(errctx, akgl_heap_next_actor(&actorptr));
	CATCH(errctx, akgl_actor_initialize((akgl_Actor *)actorptr, "player"));
	actorptr->basechar = SDL_GetPointerProperty(
	    AKGL_REGISTRY_CHARACTER,
	    "little guy",
	    NULL);
	FAIL_ZERO_BREAK(errctx, actorptr->basechar, AKERR_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 = false;

	strcpy((char *)&dirnamebuf, "assets/memories.mp3");
	CATCH(errctx, akgl_load_start_bgm((char *)&dirnamebuf));
	CATCH(errctx, music_toggle(NULL, NULL));
	
	CATCH(errctx, akgl_text_loadfont("C64Pro", "assets/C64_Pro-STYLE.ttf", 18));

	
    } CLEANUP {
    } PROCESS(errctx) {
    } HANDLE_DEFAULT(errctx) {
	LOG_ERROR(errctx);
	return SDL_APP_FAILURE;
    } FINISH_NORETURN(errctx);
      
    AKGL_BITMASK_DEL(game.state.flags, AKGLTEST_STATE_LOADING);
    AKGL_BITMASK_ADD(game.state.flags, AKGLTEST_STATE_WAITFORINPUT);

    return SDL_APP_CONTINUE;
}

akerr_ErrorContext AKERR_NOIGNORE *akgltest_controller_bind_universal(char *actorname, int mapid, int kbid, int jsid)
{
    akgl_Control control;
    PREPARE_ERROR(e);
    
    PASS(e, akgl_controller_default(mapid, actorname, kbid, jsid));
    // set custom control maps */
    memset((void *)&control, 0x00, sizeof(akgl_Control));
    
    // Quit
    control.key = SDLK_ESCAPE;
    control.event_on = SDL_EVENT_KEY_DOWN;
    control.handler_on = &akgltest_set_gamemode_menu;
    PASS(e, akgl_controller_pushmap(mapid, &control));
    
    // Toggle the music
    control.key = SDLK_M;
    control.event_on = SDL_EVENT_KEY_DOWN;
    control.handler_on = &music_toggle;
    PASS(e, akgl_controller_pushmap(mapid, &control));

    // Save the game
    control.key = SDLK_S;
    control.event_on = SDL_EVENT_KEY_DOWN;
    control.handler_on = &savegame;
    PASS(e, akgl_controller_pushmap(AKGLTEST_CONTROLMAP_INGAMEACTOR, &control));

    // Menu selection
    control.key = SDLK_RETURN;
    control.event_on = SDL_EVENT_KEY_DOWN;
    control.handler_on = &akgltest_set_gamemode_running;
    PASS(e, akgl_controller_pushmap(AKGLTEST_CONTROLMAP_MENU, &control));

    if ( jsid != 0 ) {
	// Gamepad Menu selection. Use whatever button they pushed as the action
	control.button = SDL_GAMEPAD_BUTTON_START;
	control.event_on = SDL_EVENT_GAMEPAD_BUTTON_UP;
	control.handler_on = &akgltest_set_gamemode_running;
	PASS(e, akgl_controller_pushmap(AKGLTEST_CONTROLMAP_MENU, &control));

	// Toggle the music
	control.button = SDL_GAMEPAD_BUTTON_SOUTH;
	control.event_on = SDL_EVENT_GAMEPAD_BUTTON_UP;
	control.handler_on = &music_toggle;
	PASS(e, akgl_controller_pushmap(mapid, &control));
	
	// Quit
	control.button = SDL_GAMEPAD_BUTTON_EAST;
	control.event_on = SDL_EVENT_GAMEPAD_BUTTON_UP;
	control.handler_on = &akgltest_set_gamemode_menu;
	PASS(e, akgl_controller_pushmap(mapid, &control));
    }
}


akerr_ErrorContext AKERR_NOIGNORE *akgltest_controller_get_defaults(void *appstate, SDL_Event *event)
{
    akgl_Control control;
    int kbid = 0;
    int jsid = 0;

    PREPARE_ERROR(e);

    if (event->type == SDL_EVENT_GAMEPAD_BUTTON_DOWN || event->type == SDL_EVENT_GAMEPAD_BUTTON_UP) {
	jsid = event->gbutton.which;
    } else if (event->type == SDL_EVENT_KEY_DOWN || event->type == SDL_EVENT_KEY_UP) {
	kbid = event->key.which;
    }

    PASS(e, akgltest_controller_bind_universal("player", AKGLTEST_CONTROLMAP_MENU, kbid, jsid));
    PASS(e, akgltest_controller_bind_universal("menupointer", AKGLTEST_CONTROLMAP_INGAMEACTOR, kbid, jsid));
        
    SUCCEED_RETURN(e);
}

SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
    PREPARE_ERROR(errctx);

    ATTEMPT {
	FAIL_ZERO_BREAK(errctx, appstate, AKERR_NULLPOINTER, "NULL appstate pointer");
	FAIL_ZERO_BREAK(errctx, event, AKERR_NULLPOINTER, "NULL event pointer");
	
	if ( event->type == SDL_EVENT_GAMEPAD_BUTTON_DOWN ||
	     event->type == SDL_EVENT_GAMEPAD_BUTTON_UP ||
	     event->type == SDL_EVENT_KEY_DOWN ||
	     event->type == SDL_EVENT_KEY_UP ) {
	    if ( AKGL_BITMASK_HAS(game.state.flags, AKGLTEST_STATE_WAITFORINPUT) ) {
		// get the player's default keyboard or gamepad id from the event
		CATCH(errctx, akgltest_controller_get_defaults(appstate, event));
		CATCH(errctx, akgltest_set_gamemode_menu(appstate, event));
	    } else {
		CATCH(errctx, akgl_controller_handle_event(appstate, event));
	    }
	}
	if (event->type == SDL_EVENT_QUIT) {
	    return SDL_APP_SUCCESS;  /* end the program, reporting success to the OS. */
	}
    } CLEANUP {
    } PROCESS(errctx) {
    } FINISH_NORETURN(errctx);
    return SDL_APP_CONTINUE;  /* carry on with the program! */
}

akerr_ErrorContext AKERR_NOIGNORE *akgltest_iterate_mapmenu(void)
{
    akgl_Actor *menupointer = NULL;
    PREPARE_ERROR(e);
    ATTEMPT {
	SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
	SDL_RenderClear(renderer);
	PASS(e, akgl_text_rendertextat(
		 SDL_GetPointerProperty(
		     AKGL_REGISTRY_FONT,
		     "C64Pro",
		     NULL),
		 "IMAGE MAP DEMO",
		 (SDL_Color){255, 255, 255, 255},
		 0,
		 150,
		 100)
	    );
	PASS(e, akgl_text_rendertextat(
		 SDL_GetPointerProperty(
		     AKGL_REGISTRY_FONT,
		     "C64Pro",
		     NULL),
		 "TILE MAP DEMO",
		 (SDL_Color){255, 255, 255, 255},
		 0,
		 150,
		 200)
	    );
	PASS(e, akgl_text_rendertextat(
		 SDL_GetPointerProperty(
		     AKGL_REGISTRY_FONT,
		     "C64Pro",
		     NULL),
		 "QUIT GAME",
		 (SDL_Color){255, 255, 255, 255},
		 0,
		 150,
		 300)
	    );
	menupointer = SDL_GetPointerProperty(AKGL_REGISTRY_ACTOR, "menupointer", NULL);
	FAIL_ZERO_BREAK(e, menupointer, AKERR_NULLPOINTER, "actor missing from registry");
	menupointer->visible = true;
	CATCH(e, menupointer->updatefunc(menupointer));
	CATCH(e, menupointer->movementlogicfunc(menupointer, 0));
	CATCH(e, menupointer->renderfunc(menupointer, renderer));
    } CLEANUP {
	SDL_RenderPresent(renderer);	
    } PROCESS(e) {
    } FINISH(e, true);
    
    SUCCEED_RETURN(e);
}

akerr_ErrorContext AKERR_NOIGNORE *akgltest_iterate_waitforinput(void)
{

    PREPARE_ERROR(errctx);

    akgl_game_updateFPS();
    
    ATTEMPT {
	PASS(errctx, akgl_text_rendertextat(
		  SDL_GetPointerProperty(
		      AKGL_REGISTRY_FONT,
		      "C64Pro",
		      NULL),
		  "PRESS ANY KEY OR JOYSTICK BUTTON",
		  (SDL_Color){255, 255, 255, 255},
		  0,
		  40,
		  120)
	    );
    } CLEANUP {
	SDL_RenderPresent(renderer);
    } PROCESS(errctx) {
    } HANDLE_DEFAULT(errctx) {
    } FINISH(errctx, true);
    SUCCEED_RETURN(errctx);
}

akerr_ErrorContext AKERR_NOIGNORE *akgltest_iterate_running(void)
{
    int i = 0;
    akgl_Iterator opflags;
    char fpsText[32];

    PREPARE_ERROR(errctx);

    akgl_game_updateFPS();
    
    AKGL_BITMASK_CLEAR(opflags.flags);
    AKGL_BITMASK_ADD(opflags.flags, AKGL_ITERATOR_OP_UPDATE);
    AKGL_BITMASK_ADD(opflags.flags, AKGL_ITERATOR_OP_RENDER);
    AKGL_BITMASK_ADD(opflags.flags, AKGL_ITERATOR_OP_LAYERMASK);
    AKGL_BITMASK_ADD(opflags.flags, AKGL_ITERATOR_OP_TILEMAPSCALE);

    for ( i = 0; i < AKGL_TILEMAP_MAX_LAYERS; i++ ) {
	opflags.layerid = i;
	PASS(errctx, akgl_tilemap_draw(renderer, (akgl_Tilemap *)&gamemap, &camera, i));
	SDL_EnumerateProperties(AKGL_REGISTRY_ACTOR, &akgl_registry_iterate_actor, (void *)&opflags);
    }
    ATTEMPT {
	memset((char *)&fpsText, 0x00, 32);
	snprintf((char *)&fpsText, 31, "FPS : %d", game.fps);
	PASS(errctx, akgl_text_rendertextat(
		  SDL_GetPointerProperty(
		      AKGL_REGISTRY_FONT,
		      "C64Pro",
		      NULL),
		  (char *)&fpsText,
		  (SDL_Color){255, 255, 255, 255},
		  0,
		  450,
		  10)
	    );
    } CLEANUP {
	SDL_RenderPresent(renderer);
    } PROCESS(errctx) {
    } HANDLE_DEFAULT(errctx) {
    } FINISH(errctx, true);
    SUCCEED_RETURN(errctx);
}

SDL_AppResult SDL_AppIterate(void *appstate)
{
    //SDL_FRect dest;
    //b2Vec2 position;

    PREPARE_ERROR(e);

    ATTEMPT {
	if ( AKGL_BITMASK_HAS(game.state.flags, AKGLTEST_STATE_QUIT) ) {
	    return SDL_APP_FAILURE;
	} else if ( AKGL_BITMASK_HAS(game.state.flags, AKGLTEST_STATE_RUNNING) ) {
	    CATCH(e, akgltest_iterate_running());
	} else if ( AKGL_BITMASK_HAS(game.state.flags, AKGLTEST_STATE_WAITFORINPUT) ) {
	    CATCH(e, akgltest_iterate_waitforinput());
	} else if ( AKGL_BITMASK_HAS(game.state.flags, AKGLTEST_STATE_MAPMENU) ) {
	    CATCH(e, akgltest_iterate_mapmenu());
	} 
    } CLEANUP {
    } PROCESS(e) {
    } HANDLE_DEFAULT(e) {
	LOG_ERROR(e);
	return SDL_APP_FAILURE;
    } FINISH_NORETURN(e);
    return SDL_APP_CONTINUE;
}

void SDL_AppQuit(void *appstate, SDL_AppResult result)
{
    /* SDL will clean up the window/renderer for us. */
    // SDL_DestroyTexture(ball.texture);
    //b2DestroyWorld(physicsWorldId);
    SDL_Log("Freeing music resources");
    if ( bgm != NULL ) {
	//Mix_FreeMusic(bgm);
    }
    SDL_Log("Quitting mixer");
    // Mix_Quit();
}
