#ifndef IZ_JOYSTICK_H
#define IZ_JOYSTICK_H

#include <SDL_stdinc.h>
#include <SDL_joystick.h>
#include <SDL_gamecontroller.h>
#include <SDL_events.h>
#include <minIni.h>
#include "../../config/IZ_config.h"
#include "IZ_action.h"

typedef u8 IZ_PadButton;

static const u16 IZ_DEFAULT_AXIS_THRESHOLD = 8000;

typedef enum {
	IZ_JOY_AXIS_DIRECTION_HORIZONTAL1 = 0,
	IZ_JOY_AXIS_DIRECTION_VERTICAL1 = 1,
	IZ_JOY_AXIS_DIRECTION_HORIZONTAL2 = 2,
	IZ_JOY_AXIS_DIRECTION_VERTICAL2 = 3,
	IZ_JOY_AXIS_DIRECTION_LEFT_SHOULDER = 4,
	IZ_JOY_AXIS_DIRECTION_RIGHT_SHOULDER = 5,
} IZ_JoyAxisDirection;

typedef struct {
	u16 axis_threshold;
	SDL_JoystickID device_id;
	SDL_GUID guid;
	IZ_PadButton control_mapping[IZ_CONTROLS];
} IZ_JoystickConfig;

typedef struct {
	SDL_Joystick* device;
	IZ_JoystickConfig config;
} IZ_JoystickState;

static const IZ_JoystickState IZ_JOYSTICK_DEFAULT_STATE[IZ_PLAYERS] = {
	{
		.config = {
			.control_mapping = {
				255,
				255,
				255,
				255,
				11,
				10,
				1,
				0,
				4,
				3,
				6,
				7,
				8,
				9,
				13,
				14,
			},
			.axis_threshold = 8000u,
			.device_id = 0,
			.guid = {
				.data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
			}
		},
		.device = NULL,
	},
#if IZ_PLAYERS > 1
	{
		.config = {
			.control_mapping = {
				255,
				255,
				255,
				255,
				11,
				10,
				1,
				0,
				4,
				3,
				6,
				7,
				8,
				9,
				13,
				14,
			},
			.axis_threshold = 8000u,
			.device_id = 1,
		},
		.device = NULL,
	},
#endif
};

IZ_ProcedureResult IZ_JoystickSaveConfig(IZ_JoystickState(*)[IZ_PLAYERS], const char*);

void IZ_JoystickHandleEvents(IZ_JoystickState(*)[IZ_PLAYERS], IZ_Action(*)[IZ_PLAYERS], SDL_Event);

IZ_ProcedureResult IZ_JoystickInitialize(IZ_JoystickState(*)[IZ_PLAYERS], const char*, u8, const char*[]);

void IZ_JoystickTeardown(IZ_JoystickState(*)[IZ_PLAYERS]);

#endif
