/*
** EPITECH PROJECT, 2018
** task01
** File description:
** I do task
*/
#include <stdlib.h>
#include "game/ascii_table.h"
#include "lib/my_graphics.h"
#include "lib/my_string.h"
#include "game/ascii.h"

static void draw_ascii_pixel(sfml_t *sfml, int pos[2],
uint8_t bit, uint32_t *option)
{
	bit ^= (option[3] & ASCII_REVERSE) ? 0xff : 0x00;
	if (option[3] & ASCII_ALPHA){
		if (!(bit & 0x01))
			return;
	}
	my_pixel(sfml, pos[0], pos[1], (bit & 0x01) ? option[1] : option[2]);
}

static void set_ascii(sfml_t *sfml, int position[2],
uint16_t buffer, uint32_t *option)
{
	int pos[2];
	int i;
	int j;

	i = -1;
	while (++i < ASCII_DEFAULT_WIDTH << option[0]){
		j = -1;
		while (++j < ASCII_DEFAULT_HEIGHT << option[0]){
			pos[0] = position[0] + i;
			pos[1] = position[1] +
			(ASCII_DEFAULT_WIDTH << option[0]) - j;
			draw_ascii_pixel(sfml, pos, ((buffer >>
			((j >> option[0]) + ((i >> option[0]))
			* ASCII_DEFAULT_HEIGHT)) & 0x01), option);
		}
	}
}

void my_print_set_position(int position[2], int x, int y)
{
	position[0] = x;
	position[1] = y;
}

uint32_t *my_print_set_option(int size, uint32_t color_char,
uint32_t color_alpha, uint8_t action)
{
	uint32_t *option;

	option = (uint32_t*)malloc(4 * sizeof(uint32_t));
	option[0] = (uint32_t)size;
	option[1] = color_char;
	option[2] = color_alpha;
	option[3] = (uint32_t)action;
	return (option);
}

void my_print(sfml_t *sfml, int pos1[2], const char *str, uint32_t *option)
{
	ascii_t *ascii = init_set_ascii(str, option);
	void *pass[2] = {ascii, sfml};
	int i = -1;
	int pos2[2];

	message_set_start(sfml, ascii, option, ascii->size_str);
	my_print_set_position(pos2, pos1[0] + ascii->x, pos1[1] + ascii->y);
	reverse_update(pass, pos2, option);
	message_set_start(sfml, ascii, option, get_line_size(str, 0));
	while (str[++i] != '\0'){
		pos2[0] = pos1[0] + ascii->x;
		ascii->x += ascii->width;
		if (str[i] == '\n'){
			pos2[1] += ascii->height + (ascii->offset << 1);
			message_set_start(sfml, ascii, option,
			get_line_size(str, i + 1));
		}
		else
			set_ascii(sfml, pos2,
			ascii_tab[(uint8_t)str[i]], option);
	}
}
