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

static void print_message(sfml_t *sfml, message_t *message)
{
	uint32_t *option;
	int pos[2];

	option = my_print_set_option(3, 0x000000ff,
	0xffffffff, ASCII_CENTER_X | ASCII_REVERSE);
	pos[0] = message->x;
	pos[1] = message->y + 16;
	my_print(sfml, pos, message->message, option);
	message->timer_message--;
	free(option);
}

static void update_active(sfml_t *sfml)
{
	uint8_t *map = sfml->scene->level->map;
	intmax_t width = sfml->scene->level->width;
	intmax_t x = sfml->scene->player->x;
	intmax_t y = sfml->scene->player->y;

	if (map[(x + 9 >> 6) + (y >> 6) * width] == ID_FAKE_GROUND
		|| map[(x + 53 >> 6) + (y >> 6) * width] == ID_FAKE_GROUND
		|| map[(x + 53 >> 6) + (y + 64 >> 6) * width] == ID_FAKE_GROUND
		|| map[(x + 9 >> 6) + (y + 64 >> 6) * width] == ID_FAKE_GROUND)
		sfml->scene->message_active |= MESSAGE_FAKE_GROUND;
}

void check_message(sfml_t *sfml, message_t **message)
{
	int counter = 0;
	int exit = 1;
	int i = -1;

	update_active(sfml);
	while (++i < sfml->scene->nb_message)
		if (message[i]->timer_message > 0
			&& (message[i]->timer_message != MESSAGE_DEFAULT_TIME
			|| (message[i]->action == MESSAGE_DEAD
			&& message[i]->trigger_data ==
			sfml->scene->player->nb_dead)
			|| (message[i]->action == MESSAGE_X_TRIGGER
			&& sfml->scene->player->x >= message[i]->trigger_data)
			|| (message[i]->action == MESSAGE_FAKE_GROUND
			&& sfml->scene->message_active & MESSAGE_FAKE_GROUND)
			|| (message[i]->action == MESSAGE_FAKE_PRINCESS
			&& sfml->scene->message_active
			& MESSAGE_FAKE_PRINCESS)))
			print_message(sfml, message[i]);
}
