#include "datatypes.h"
#include "util.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

static size_t EDL_internal_append_offset(const char* input, size_t offset, size_t length) {
	size_t s = 0;

	s += strchr(&input[offset + s], ';') - &input[offset];
	if (s + offset > length)
		return s;

	s += strspn(&input[offset + s], ";\t ");
	if (s + offset > length)
		return s;

	return s;
}

size_t EDL_internal_parse_int(const char* input, size_t offset, size_t length, int* int_return) {
	if (offset > length)
		return 0;

	{
		char* ptr_return;
		*int_return = strtol(&input[offset], &ptr_return, 10);

		if (!ptr_return)
			return 0;
	}

	return EDL_internal_append_offset(input, offset, length);
}

size_t EDL_internal_parse_double(const char* input, size_t offset, size_t length, double* double_return) {
	if (offset > length)
		return 0;

	{
		char* ptr_return;
		*double_return = strtod(&input[offset], &ptr_return);

		if (!ptr_return)
			return 0;
	}

	return EDL_internal_append_offset(input, offset, length);
}

size_t EDL_internal_parse_bool(const char* input, size_t offset, size_t length, bool* bool_return) {
	if (offset > length)
		return 0;

	if (!strncmp(&input[offset], "TRUE", 4))
		*bool_return = true;
	else if (!strncmp(&input[offset], "FALSE", 5))
		*bool_return = false;

	return EDL_internal_append_offset(input, offset, length);
}

size_t EDL_internal_parse_media_type(const char* input, size_t offset, size_t length, EDL_media_type_t* media_return) {
	if (offset > length)
		return 0;

	if (!strncmp(&input[offset], "VIDEO", 5))
		*media_return = MEDIATYPE_VIDEO;
	else if (!strncmp(&input[offset], "AUDIO", 5))
		*media_return = MEDIATYPE_AUDIO;
	else
		*media_return = MEDIATYPE_UNKNOWN;

	return EDL_internal_append_offset(input, offset, length);
}

size_t EDL_internal_parse_string(const char* input, size_t offset, size_t length, char** string_return) {
	size_t start, s_length;

	/* will most definitely break on good filesystems */
	if (offset > length)
		return 0;

	start = strchr(&input[offset], '\"') - &input[offset] + 1;
	if (start + offset > length)
		return 0;

	s_length = strchr(&input[offset + start], '\"') - &input[offset] - start;
	if (start + s_length + offset > length)
		return 0;

	*string_return = malloc((s_length + 1) * sizeof(char));
	if (!*string_return)
		return 0;

	memcpy(*string_return, &input[offset + start], s_length);
	(*string_return)[s_length] = '\0';

	return EDL_internal_append_offset(input, offset, length);
}

char* EDL_internal_integer_to_string(int value) {
	char out[256] = {0}; /* this ought to be enough. */
	snprintf(out, 256, "%d", value);
	out[255] = '\0';
	return EDL_internal_strdup(out);
}

char* EDL_internal_double_to_string(double value) {
	char out[256] = {0};
	snprintf(out, 256, "%.6f", value);
	out[255] = '\0';
	return EDL_internal_strdup(out);
}

char* EDL_internal_bool_to_string(bool value) {
	return EDL_internal_strdup(value ? "TRUE" : "FALSE");
}

char* EDL_internal_media_type_to_string(EDL_media_type_t value) {
	switch (value) {
		case MEDIATYPE_AUDIO:
			return EDL_internal_strdup("AUDIO");
		case MEDIATYPE_VIDEO:
		case MEDIATYPE_UNKNOWN:
		default:
			return EDL_internal_strdup("VIDEO");
	}
}
