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

int get_line_size(const char *str, int pos)
{
	int i;

	i = pos - 1;
	while (str[++i] != '\n' && str[i] != '\0');
	return (i - pos);
}

ascii_t *init_set_ascii(const char *str, uint32_t *option)
{
	ascii_t *ascii;
	int size_max;
	int i;

	i = -1;
	size_max = 0;
	ascii = (ascii_t*)malloc(sizeof(ascii_t));
	ascii->nb_line = 1;
	ascii->size_str = 0;
	while (str[++i] != '\0'){
		ascii->nb_line += (str[i] == '\n') ? 1 : 0;
		size_max = (str[i] == '\n') ? 0 : size_max + 1;
		ascii->size_str += (size_max > ascii->size_str) ? 1 : 0;
	}
	ascii->height = (ASCII_DEFAULT_HEIGHT - 1) << option[0];
	ascii->width = ASCII_DEFAULT_WIDTH << option[0];
	ascii->offset = 1 << option[0];
	return (ascii);
}

void message_set_start(sfml_t *sfml, ascii_t *ascii,
uint32_t *option, int size_str)
{
	ascii->x = 0;
	ascii->y = 0;
	if (option[3] & ASCII_CENTER_X)
		ascii->x = (sfml->width >> 1) -
		((size_str * ascii->width) >> 1);
	if (option[3] & ASCII_CENTER_Y){
		ascii->y = (sfml->height >> 1) -
		((ascii->nb_line * ascii->height) >> 1);
		option[3] ^= ASCII_CENTER_Y;
	}
}

void reverse_update(void *pass[2], int position[2], uint32_t *option)
{
	ascii_t *ascii;
	sfml_t *sfml;
	int rect[4];

	if (!(option[3] & ASCII_REVERSE))
		return;
	sfml = (sfml_t*)pass[1];
	ascii = (ascii_t*)pass[0];
	option[3] &= ~ASCII_ALPHA;
	rect[0] = position[0] - ascii->offset;
	rect[1] = position[1] - (2 << option[0]) + 2;
	rect[2] = position[0] + (ascii->size_str * ascii->width);
	rect[3] = position[1] + (ascii->nb_line * ascii->height) - 1
	+ (ascii->nb_line * (ascii->offset << 1));
	my_filled_rectangle(sfml, rect, option[1]);
}
