#include "resource_shader.h"

typedef struct ShaderData
{
	char vertex[SHADER_BUFFER_SIZE];
	char fragment[SHADER_BUFFER_SIZE];
} ShaderData;

static ShaderData shaderData[SHADER_COUNT];

/* Reads in shader data. */
void
resource_shader_read(Resources* self)
{
	for (s32 i = 0; i < SHADER_COUNT; i++)
	{
		file_read
		(
			SHADER_PATHS[i].vertex, 
			(void*)shaderData[i].vertex, 
			SHADER_BUFFER_SIZE, 
			"r"
		); 
		
		file_read
		(
			SHADER_PATHS[i].fragment, 
			(void*)shaderData[i].fragment, 
			SHADER_BUFFER_SIZE, 
			"r"
		);
	}
}

/* Initializes shader resource. */
bool
resource_shader_init(Resources* self, ShaderType type)
{
	if 
	(
		!shader_init
		(
			&self->shaders[type], 
			shaderData[type].vertex,
			shaderData[type].fragment
		)
	)
	{
		printf(STRING_RESOURCE_SHADER_ERROR, SHADER_PATHS[type].vertex, SHADER_PATHS[type].fragment);
		return false;
	}

	printf(STRING_RESOURCE_SHADER_INIT, SHADER_PATHS[type].vertex, SHADER_PATHS[type].fragment);
	return true;
}

/* Frees shader resources. */
void
resource_shader_free(Resources* self, ShaderType type)
{
	if (self->shaders[type].isInit)
	{
		shader_free(&self->shaders[type]);
		printf(STRING_RESOURCE_SHADER_FREE, SHADER_PATHS[type].vertex, SHADER_PATHS[type].fragment);
	}
}


/* Given an array of shader types, intializes each. */
void
resource_shader_state_init(Resources* self, const ShaderType* types, u32 count)
{
	for (s32 i = 0; i < (s32)count; i++)
		resource_shader_init(self, types[i]);
}

/* Given an array of shader types, frees each. */
void
resource_shader_state_free(Resources* self, const ShaderType* types, u32 count)
{
	for (s32 i = 0; i < (s32)count; i++)
		resource_shader_free(self, types[i]);
}
