#include <stdio.h>
#include <stdbool.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_keycode.h>

#define STB_IMAGE_IMPLEMENTATION 1
#include <stb_image.h>

#define STB_RECT_PACK_IMPLEMENTATION 1
#include <stb_rect_pack.h>

#define STB_TRUETYPE_IMPLEMENTATION 1
#include <stb_truetype.h>

#include "core/core.h"
#include "core/types.h"
#include "os/core.h"

#include "vulkan/core.h"
#include "draw/core.h"

#include "game/core.h"

#include "game/impl/world.c"
#include "game/impl/npc.c"
#include "game/impl/bandit.c"
#include "game/testnavmesh.h"

int main(int argc, char **argv)
{
	(void) argc;
	(void) argv;

	if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO))
	{
		printf("[Error] :: Failed to initialise SDL3 (%s)\n", SDL_GetError());
		return 1;
	}

	SDL_Window *window = SDL_CreateWindow("Ludum", 1280, 720, SDL_WINDOW_HIGH_PIXEL_DENSITY);
	if (!window)
	{
		printf("[Error] :: Failed to create window (%s)\n", SDL_GetError());
		return 1;
	}

	SDL_AudioStream *austream;
	Str8 audio_data;
	M_TempScope(0, 0) {
		SDL_AudioSpec spec;
		spec.format   = SDL_AUDIO_S16LE;
		spec.channels = 2;
		spec.freq     = 44100;

		austream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, 0, 0);

		if (!austream) {
			printf("Failed to open audio stream (%s)\n", SDL_GetError());
		}

		Str8 exec = FS_SystemPath(temp.arena, FS_SYSTEM_PATH_EXE);
		Str8 path = Sf(temp.arena, "%.*s/assets/outside_ambience.wav", Sv(exec));

		SDL_AudioSpec wav_spec;
		U32 count;

		SDL_LoadWAV((const char *) path.data, &wav_spec, &audio_data.data, &count);
		audio_data.count = count;
	}

	Vk_Setup(window);

	G_State *game = 0;
	{
		M_Arena *arena = M_ArenaAlloc(GB(64), .initial = MB(4));
		game = M_ArenaPush(arena, G_State);

		game->arena      = arena;
		game->draw.arena = arena;

		G_ImagesLoad(game);
		D_FontLoad(&game->draw, S("ubuntu"), 60);

		G_PipelinesLoad(game);
		G_AudioLoad(game);

		G_Camera *camera = &game->camera;

		camera->x = V3F(1, 0, 0);
		camera->y = V3F(0, 1, 0);
		camera->z = V3F(0, 0, 1);
		camera->p = V3F(0, 0, 16);

		camera->fov = 60.0f;

		camera->nearp = 0.01f;
		camera->farp = 1000.0f;

		game->draw.camera = camera;
		World *world = M_ArenaPush(arena, World);//LoadWorld(arena);
    game->world = world;
    world->arena = arena;
    world->navMesh = &TestNavMesh;
    world->random = Random_Seed(29237489723847);
    world->npcCount = 2;
    for(U32 i = 0; i < world->npcCount; i++) {
    NPC *npc1 = &world->npcs[i];
      npc1->collision.pos.x = 15;
      npc1->collision.pos.y = 15;
      npc1->collision.size.x = 1;
      npc1->collision.size.y = 2;
      npc1->name = S("Matt");
      npc1->mode = NPC_ACTION_WAITING;
      npc1->currentArea = i;
      npc1->waitTime = 0;
      npc1->maxWaitTime = 5;
      npc1->currentNavNode = 0;
    }

    Bandit *badman = &world->bandit;
		badman->collision.pos.x = 15;
		badman->collision.pos.y = 15;
		badman->collision.size.x = 1;
		badman->collision.size.y = 2;
		badman->name = S("Leroy Brown");
		badman->mode = BANDIT_WAITING;
		badman->waitTime = 0;
		badman->maxWaitTime = 2;
		badman->poiCount = 2;
    badman->pointsOfInterest[0] = 937;
    badman->pointsOfInterest[1] = 12;

		world->npcPOI[0] = 100;

		PlayerInit(game, &world->player);

    for(int i =0; i< 4200; i++) {
      world->map[i] = map[i];
    }

    world->tileTypes[0].rotation=0;
    world->tileTypes[0].collision=false;
    world->tileTypes[0].tile=D_ImageHandle(&game->draw, S("tile_dirt_0"));
    world->tileTypes[1].rotation=0,
    world->tileTypes[1].collision=false,
    world->tileTypes[1].tile=D_ImageHandle(&game->draw, S("path_middle"));
    world->tileTypes[2].rotation=0;
    world->tileTypes[2].collision=false;
    world->tileTypes[2].tile=D_ImageHandle(&game->draw, S("path_middle_edge"));
    world->tileTypes[3].rotation=PI_F32/2;
    world->tileTypes[3].collision=false;
    world->tileTypes[3].tile=D_ImageHandle(&game->draw, S("path_middle_edge"));
    world->tileTypes[4].rotation=PI_F32;
    world->tileTypes[4].collision=false;
    world->tileTypes[4].tile=D_ImageHandle(&game->draw, S("path_middle_edge"));
    world->tileTypes[5].rotation=-PI_F32/2;
    world->tileTypes[5].collision=false;
    world->tileTypes[5].tile=D_ImageHandle(&game->draw, S("path_middle_edge"));
    world->tileTypes[6].rotation=0;
    world->tileTypes[6].collision=false;
    world->tileTypes[6].tile=D_ImageHandle(&game->draw, S("path_middle"));
    world->tileTypes[7].rotation=PI_F32/2;
    world->tileTypes[7].collision=false;
    world->tileTypes[7].tile=D_ImageHandle(&game->draw, S("path_middle"));
    world->tileTypes[8].rotation=-PI_F32;
    world->tileTypes[8].collision=false;
    world->tileTypes[8].tile=D_ImageHandle(&game->draw, S("path_middle"));
    world->tileTypes[9].rotation=-PI_F32/2;
    world->tileTypes[9].collision=false;
    world->tileTypes[9].tile=D_ImageHandle(&game->draw, S("path_middle"));
    world->tileTypes[10].rotation=0;
    world->tileTypes[10].collision=false;
    world->tileTypes[10].tile=D_ImageHandle(&game->draw, S("path_corner"));
    world->tileTypes[11].rotation=PI_F32/2;
    world->tileTypes[11].collision=false;
    world->tileTypes[11].tile=D_ImageHandle(&game->draw, S("path_corner"));
    world->tileTypes[12].rotation=-PI_F32/2;
    world->tileTypes[12].collision=false;
    world->tileTypes[12].tile=D_ImageHandle(&game->draw, S("path_corner"));
    world->tileTypes[13].rotation=PI_F32;
    world->tileTypes[13].collision=false;
    world->tileTypes[13].tile=D_ImageHandle(&game->draw, S("path_corner"));
    world->tileTypes[14].rotation=0;
    world->tileTypes[14].collision=false;
    world->tileTypes[14].tile=D_ImageHandle(&game->draw, S("tile_dirt_1"));

    world->propTypes[0].assetHandle=D_ImageHandle(&game->draw, S("rug0"));
    world->propTypes[0].scale=1;
    world->propTypes[1].assetHandle=D_ImageHandle(&game->draw, S("rug1"));
    world->propTypes[1].scale=1;
    world->propTypes[2].assetHandle=D_ImageHandle(&game->draw, S("skull"));
    world->propTypes[2].scale=1;
    world->propTypes[3].assetHandle=D_ImageHandle(&game->draw, S("table"));
    world->propTypes[3].scale=1;
    world->propTypes[4].assetHandle=D_ImageHandle(&game->draw, S("barrel"));
    world->propTypes[4].scale=1;
    world->propTypes[5].assetHandle=D_ImageHandle(&game->draw, S("can"));
    world->propTypes[5].scale=1;
    world->propTypes[6].assetHandle=D_ImageHandle(&game->draw, S("candle"));
    world->propTypes[6].scale=1;
    world->propTypes[7].assetHandle=D_ImageHandle(&game->draw, S("clock"));
    world->propTypes[7].scale=1;
    world->propTypes[8].assetHandle=D_ImageHandle(&game->draw, S("log_pile"));
    world->propTypes[8].scale=1;
    world->propTypes[9].assetHandle=D_ImageHandle(&game->draw, S("nightstand"));
    world->propTypes[9].scale=1;
    world->propTypes[10].assetHandle=D_ImageHandle(&game->draw, S("pool_table"));
    world->propTypes[10].scale=1;
    world->propTypes[11].assetHandle=D_ImageHandle(&game->draw, S("saloon_ext"));
    world->propTypes[11].scale=6.875f;
    world->propTypes[12].assetHandle=D_ImageHandle(&game->draw, S("saloon_int"));
    world->propTypes[12].scale=6.875f;
    world->propTypes[11].assetHandle=D_ImageHandle(&game->draw, S("house"));
    world->propTypes[11].scale=6.875f;
    world->propTypes[12].assetHandle=D_ImageHandle(&game->draw, S("house_int"));
    world->propTypes[12].scale=6.875f;
	}
  game->editor.enabled = true;
  game->editor.mode = G_EDITOR_MODE_TILE;
  game->editor.currentLevel = WORLD_AREA_OUTSIDE;

	bool running = true;

	const int width  = 1280;
	const int height = 720;

	while (running)
	{
		SDL_Event e;
		while (SDL_PollEvent(&e))
		{
			if (e.type == SDL_EVENT_QUIT)
			{
				running = false;
			}
			else if (e.type == SDL_EVENT_KEY_DOWN) {
				Player *player = &game->world->player;
				switch (e.key.key) {
					case SDLK_R:  { PlayerInit(game, player); } break;
				}
			}

      V3f projection = G_CameraUnproject(&game->camera, V2f_Clip(
        V2F(e.button.x, e.button.y),
        V2F((F32) width, (F32) height)
      ));
      game->world->mouseProjected = V2F(projection.x, projection.y);
      if(e.type==SDL_EVENT_MOUSE_MOTION) {
        game->editor.cursor = V2F(projection.x, projection.y);
      }
      if (!game->editor.enabled) {
        ProcessEvents(&e, game->world);
      } else {
        if(e.type==SDL_EVENT_MOUSE_BUTTON_DOWN && e.button.button == SDL_BUTTON_LEFT) {
          switch(game->editor.mode){
            case G_EDITOR_MODE_TILE: {
              F32 tilex = (F32) (S32)(game->editor.cursor.x+TILE_SIZE/2);
              F32 tiley = (F32) (S32)(game->editor.cursor.y+TILE_SIZE/2);
              game->world->map[(S32)tilex + (S32)tiley * 96] = game->editor.currentAsset;
              break;
            }
            case G_EDITOR_MODE_PROP: {
              game->world->props[game->world->propCount].propType = game->editor.currentAsset;
              game->world->props[game->world->propCount].area = game->editor.currentLevel;
              game->world->props[game->world->propCount].pos = game->editor.cursor;
              game->world->propCount++;
              break;
            }
            case G_EDITOR_MODE_HITBOX: {
              game->editor.dragStart = game->editor.cursor;
              break;
            }
          }
        } else if(e.type==SDL_EVENT_MOUSE_BUTTON_UP && e.button.button == SDL_BUTTON_LEFT && game->editor.mode == G_EDITOR_MODE_HITBOX) {
          // Add hitbox
          V2f topLeft = V2F(Min(game->editor.cursor.x, game->editor.dragStart.x), Min(game->editor.cursor.y, game->editor.dragStart.y));
          game->world->hitboxes[game->world->hitboxCount].pos = topLeft;
          game->world->hitboxes[game->world->hitboxCount].size = V2F(
            Abs(game->editor.cursor.x-game->editor.dragStart.x),
            Abs(game->editor.cursor.y-game->editor.dragStart.y)
          );
          game->world->hitboxCount++;
        }
      }

      if (e.type == SDL_EVENT_KEY_DOWN) {
        switch(e.key.key) {
          case SDLK_F10: {
            game->editor.enabled = !game->editor.enabled;
            break;
          }
          case SDLK_RIGHT: {
            game->editor.currentAsset = Min(game->editor.currentAsset+1, 64);
            break;
          }
          case SDLK_LEFT: {
            game->editor.currentAsset = Max(game->editor.currentAsset-1, 0);
            break;
          }
          case SDLK_A: {
            game->camera.p.x -= 5;
            break;
          }
          case SDLK_D: {
            game->camera.p.x += 5;
            break;
          }
          case SDLK_W: {
            game->camera.p.y -= 5;
            break;
          }
          case SDLK_S: {
            game->camera.p.y += 5;
            break;
          }
          case SDLK_UP: {
            game->editor.currentLevel++;
            game->world->player.currentArea++;
            break;
          }
          case SDLK_DOWN: {
            game->editor.currentLevel--;
            game->world->player.currentArea--;
            break;
          }
          case SDLK_SPACE: {
            game->editor.mode = (game->editor.mode + 1) % 4;
            break;
          }
          case SDLK_U: {
            switch(game->editor.mode) {
              case G_EDITOR_MODE_PROP: {game->world->propCount--;}
              case G_EDITOR_MODE_HITBOX: {game->world->hitboxCount--;}
            }
            break;
          }
        }
      }
		}

    if(!game->editor.enabled) {
		  UpdateWorld(1.0f / 60.0f, game->world);
      game->camera.p.x = game->world->player.pos.x;
      game->camera.p.y = game->world->player.pos.y;
    }

		int w, h;
		SDL_GetWindowSizeInPixels(window, &w, &h);

		game->draw.window_width = w;
		game->draw.window_height = h;

		G_CalculateCamera(&game->camera, (F32)w / (F32)h);

		Vk_Frame *frame = Vk_FrameBegin(window);
		VkCommandBuffer cmd = frame->cmd;

		VkClearValue clear_colour;
		clear_colour.color.float32[0] = 0.0f;
		clear_colour.color.float32[1] = 0.0f;
		clear_colour.color.float32[2] = 0.0f;
		clear_colour.color.float32[3] = 1.0f;

		VkRenderingAttachmentInfo colour_attachment = {0};
		colour_attachment.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO;
		colour_attachment.imageView = vk.swapchain.views[frame->image];
		colour_attachment.imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
		colour_attachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
		colour_attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
		colour_attachment.clearValue = clear_colour;

		VkRenderingInfo rendering_info = {0};
		rendering_info.sType = VK_STRUCTURE_TYPE_RENDERING_INFO;
		rendering_info.renderArea = (VkRect2D){0, 0, w, h};
		rendering_info.layerCount = 1;
		rendering_info.colorAttachmentCount = 1;
		rendering_info.pColorAttachments = &colour_attachment;

		vk.CmdBeginRendering(cmd, &rendering_info);

		D_Begin(&game->draw, frame, D_MAX_RECTS);

    RenderWorld(game->world, &game->draw);

		D_Text(&game->draw, game->draw.fonts, S("Small Test"), 0, 0);

    if(game->editor.enabled) {
      G_Editor editor = game->editor;
      F32 tilex = cast(F32) floor(editor.cursor.x+TILE_SIZE/2);
      F32 tiley = cast(F32) floor(editor.cursor.y+TILE_SIZE/2);
      switch(game->editor.mode) {
        case G_EDITOR_MODE_TILE: {
          World_Tile asset = game->world->tileTypes[editor.currentAsset];
		      D_Rect(&game->draw, tilex, tiley, .texture=asset.tile, .angle=(F32) asset.rotation);
          break;
        }
        case G_EDITOR_MODE_PROP: {
          World_PropType prop = game->world->propTypes[editor.currentAsset];
		      D_Rect(&game->draw, editor.cursor.x, editor.cursor.y, .texture=prop.assetHandle, .scale=prop.scale);
          break;
        }
        case G_EDITOR_MODE_HITBOX: {
          for(U32 i = 0; i < game->world->hitboxCount; i++) {
            V2f centre = AABB_Centre(game->world->hitboxes[i]);
		        D_Rect(&game->draw, centre.x, centre.y, .texture=0, .dim=game->world->hitboxes[i].size, .flags=D_RECT_IGNORE_ASPECT);
          }
          break;
        }
        case G_EDITOR_MODE_POI: {
          break;
        }
      }
    }
		D_End(&game->draw, frame);

		vk.CmdEndRendering(cmd);

		Vk_FrameEnd();
	}

	SDL_DestroyWindow(window);
	SDL_Quit();

	return 0;
}

#include "core/core.c"
#include "os/core.c"
#include "vulkan/core.c"
#include "draw/core.c"
#include "game/core.c"
