#ifndef PLATFORM_H
#define PLATFORM_H

v2 GlobalWinDim = {};
rect32 GlobalWinRect = {};
SDL_Window* GlobalWindow = 0;
const uint64 PerformanceFrequency = SDL_GetPerformanceFrequency();
const real32 SecPerCount = 1.0f / (real32)PerformanceFrequency;

using namespace std;

enum lib_type
{
	LIB_SDL = 0x1,
	LIB_SDL_FULL = 0x2,
	LIB_OPENGL = 0x4,
};

enum keys
{
	KEY_0,
	KEY_1,
	KEY_2,
	KEY_3,
	KEY_4,
	KEY_5,
	KEY_6,
	KEY_7,
	KEY_8,
	KEY_9,
	KEY_A,
	KEY_AC_BACK,
	KEY_AC_BOOKMARKS,
	KEY_AC_FORWARD,
	KEY_AC_HOME,
	KEY_AC_REFRESH,
	KEY_AC_SEARCH,
	KEY_AC_STOP,
	KEY_AGAIN,
	KEY_ALTERASE,
	KEY_QUOTE,
	KEY_APPLICATION,
	KEY_AUDIOMUTE,
	KEY_AUDIONEXT,
	KEY_AUDIOPLAY,
	KEY_AUDIOPREV,
	KEY_AUDIOSTOP,
	KEY_B,
	KEY_BACKSLASH,
	KEY_BACKSPACE,
	KEY_BRIGHTNESSDOWN,
	KEY_BRIGHTNESSUP,
	KEY_C,
	KEY_CALCULATOR,
	KEY_CANCEL,
	KEY_CAPSLOCK,
	KEY_CLEAR,
	KEY_CLEARAGAIN,
	KEY_COMMA,
	KEY_COMPUTER,
	KEY_COPY,
	KEY_CRSEL,
	KEY_CURRENCYSUBUNIT,
	KEY_CURRENCYUNIT,
	KEY_CUT,
	KEY_D,
	KEY_DECIMALSEPARATOR,
	KEY_DELETE,
	KEY_DISPLAYSWITCH,
	KEY_DOWN,
	KEY_E,
	KEY_EJECT,
	KEY_END,
	KEY_EQUALS,
	KEY_ESC,
	KEY_EXE,
	KEY_EXSEL,
	KEY_F,
	KEY_F1,
	KEY_F10,
	KEY_F11,
	KEY_F12,
	KEY_F13,
	KEY_F14,
	KEY_F15,
	KEY_F16,
	KEY_F17,
	KEY_F18,
	KEY_F19,
	K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UOTEDBL,
	KEY_RIGHTPAREN,
	KEY_UNDERSCORE,
	KEY_MAX,
};

enum mouse_button
{
	BUTTON_LEFT,
	BUTTON_MIDDLE,
	BUTTON_RIGHT,
	BUTTON_MAX,
};

enum render_flip
{
	FLIP_NONE,
	FLIP_HORIZONTAL,
	FLIP_VERTICAL,
};

struct display
{
	int RefreshRate;
	real32 GameUpdateHZ;
	real32 MsPerFrame;
	real32 TargetSecPerFrame;
	real32 FPS;
};

struct render
{
	SDL_Window* Window;
	SDL_Renderer* Renderer;
	v2i Dim;
	v2 WinDim;
	rect32 WinRect;
	display Display;
	bool MissedFrame;
	v2 Scale;
};

struct debug_file
{
    void* Data;
    uint64 Size;
};

struct image_data
{
	v2i Dim;
	SDL_Rect Rect;
	SDL_Texture* Texture;
	
	uint8 a;
	uint8 r;
	uint8 g;
	uint8 b;
	bool Transparency;
	bool AlphaSwitch;
};

struct glyph_character
{
	int MinX;
	int MaxX;
	int MinY;
	int MaxY;
	int Advance;
	
	int Width;
	int Height;
	
	uint32 Texture;
};

struct game_button_state
{
	bool IsDown;
	bool EndedDown;
	bool Up;
	int HalfTransitions;
};

struct game_controller_input
{
	game_button_state Keys[KEY_MAX];
};

#include "timer.h"

struct text_editing
{
	uint32 Cursor;
	uint32 Length;
	string Text;
};

struct game_text_input
{
	bool Active;
	timer Timer;
	timer TextTimer;
	timer TextTimerHold;
	timer BackSpaceTimer;
};

struct game_input
{
	game_controller_input Keyboard;
	game_controller_input Mouse;
	
	v2i MouseState;
	v2i Scroll;
	bool Scrolling;
	
	bool KeyDown;
	bool KeyWasDown;
	bool KeyEndedDown;
	const Uint8* KeyState;
	
	game_text_input TextInput;
};

#if 1
struct stack
{
	uint32 Size;
	uint32 Capacity;
	int* MyStack;
};
#endif

//Init libraries and graphics
bool InitLib(uint8 Flags);
render InitGraphics(char* Title, v2i Dim, Uint32 Flags);
display InitDisplay(SDL_Window* Window);

//Window
int Win32GetWindowRefreshRate(SDL_Window *Window);
void PrintFPS(real32 FPS);
bool HandleEvents(SDL_Event* Event, game_controller_input* KeyboardInput);

//Inputs
void Win32ProcessKeyPress(game_button_state* NewState, bool IsDown, bool WasDown = false);

//Render
void LoadTexture(image_data* Image, char* Path, SDL_Renderer* Renderer, uint8 Alpha = 255);
void LoadAndUpdateTexture(image_data* Image, char* Path, SDL_Rect* Rect = 0);

//Rects
SDL_Rect SetRectRounded(v2 Pos, v2 Dim);

//Utility

#endif