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

static void clear_menu(sfml_t *sfml)
{
	int rectangle[5];

	rectangle[0] = (sfml->width >> 1) - (sfml->width >> 2);
	rectangle[1] = (sfml->height >> 1) - (sfml->height >> 2);
	rectangle[2] = (sfml->width >> 1) + (sfml->width >> 2);
	rectangle[3] = (sfml->height >> 1) + (sfml->height >> 2);
	rectangle[4] = 4;
	my_rectangle(sfml, rectangle, 0x000000ff, 0xffffffff);
}

static void text_management(sfml_t *sfml, int *locate)
{
	uint32_t *option;
	int pos[2];

	option = my_print_set_option(3, 0x000000ff, 0xffffffff, 0);
	option[3] = (!*locate) ? ASCII_CENTER_X |
ASCII_REVERSE : ASCII_CENTER_X;
	my_print_set_position(pos, -(sfml->width >> 3) - 50,
	(sfml->height >> 1) + (sfml->height >> 4));
	my_print(sfml, pos, "Exit", option);
	my_print_set_position(pos, 0, (sfml->height >> 1) +
	(sfml->height >> 4));
	option[3] = (*locate == 1) ? ASCII_CENTER_X |
ASCII_REVERSE : ASCII_CENTER_X;
	my_print(sfml, pos, "Return", option);
	my_print_set_position(pos, (sfml->width >> 3) + 56,
	(sfml->height >> 1) + (sfml->height >> 4));
	option[3] = (*locate == 2) ? ASCII_CENTER_X |
ASCII_REVERSE : ASCII_CENTER_X;
	my_print(sfml, pos, "Settings", option);
	my_display_vram(sfml);
	free(option);
}

static void key_management(sfml_t *sfml, int *locate, int *exit)
{
	get_key(sfml);
	if (sfml->key & KEY_LEFT && !(sfml->key & PRES_LEFT)){
		*locate = (*locate + 1) % 3;
		sfml->key &= ~KEY_LEFT;
	}
	if (sfml->key & KEY_RIGHT && !(sfml->key & PRES_RIGHT)){
		*locate = (*locate + 2) % 3;
		sfml->key &= ~KEY_RIGHT;
	}
	if (sfml->key & KEY_ENTER && !(sfml->key & PRES_ENTER)){
		sfml->key &= ~KEY_ENTER;
		*exit = 0;
	}
	if (sfml->key & KEY_ESC && !(sfml->key & PRES_ESC)){
		sfml->key &= ~KEY_ESC;
		*exit = -1;
	}
}

int menu_pause(sfml_t *sfml)
{
	int position[2];
	uint32_t *option;
	int locate = 1;
	int exit = 1;

	sfml->key &= ~(KEY_ENTER | KEY_LEFT | KEY_RIGHT);
	while (exit > 0 && sfRenderWindow_isOpen(sfml->window)){
		clear_menu(sfml);
		option = my_print_set_option(4, 0x000000ff,
		0xffffffff, ASCII_CENTER_X | ASCII_CENTER_Y);
		my_print_set_position(position, 0, -50);
		my_print(sfml, position, "Pause", option);
		text_management(sfml, &locate);
		key_management(sfml, &locate, &exit);
		set_exit(sfml);
		free(option);
		if (!exit && locate == 2)
			menu_settings(sfml);
		exit = (!exit && locate == 2) ? 1 : exit;
	}
	return ((exit == -1) ? 1 : locate);
}
