/*
** 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 (*music_action[2])(sfMusic*) = {&sfMusic_pause, &sfMusic_play};

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 = my_print_set_option(3, 0x000000ff, 0xffffffff, 0);
	int pos[2];

	option[3] = (!*locate) ? ASCII_CENTER_X | ASCII_REVERSE
: ASCII_CENTER_X;
	my_print_set_position(pos, 0,
	(sfml->height >> 1) - (sfml->height >> 4));
	my_print(sfml, pos, (sfml->sound->active & SOUND_ON)
	? "Sound: ON" : "Sound:OFF", option);
	my_print_set_position(pos, 0, (sfml->height >> 1));
	option[3] = (*locate == 1) ? ASCII_CENTER_X | ASCII_REVERSE
: ASCII_CENTER_X;
	my_print(sfml, pos, "Volume: 0", option);
	my_print_set_position(pos, 128, (sfml->height >> 1));
	my_print_nbr(sfml, sfml->sound->volume, option, pos);
	my_print_set_position(pos, 0,
	(sfml->height >> 1) + (sfml->height >> 4));
	option[3] = (*locate == 2) ? ASCII_CENTER_X | ASCII_REVERSE
: ASCII_CENTER_X;
	my_print(sfml, pos, "Exit", option);
	free(option);
}

static void key_action(sfml_t *sfml, int *locate, int *exit)
{
	if (sfml->key & KEY_LEFT && !(sfml->key & PRES_LEFT) && *locate != 2){
		if (*locate == 0){
			sfml->sound->active ^= SOUND_ON;
			music_action[sfml->sound->active
			& SOUND_ON](sfml->sound->music);
		} else if (*locate == 1 && sfml->sound->volume + 5 <= 100)
			sfml->sound->volume += 5;
		sfml->key &= ~KEY_LEFT;
	}
	if (sfml->key & KEY_RIGHT && !(sfml->key & PRES_RIGHT)){
		if (*locate == 0){
			sfml->sound->active ^= SOUND_ON;
			music_action[sfml->sound->active
			& SOUND_ON](sfml->sound->music);
		} else if (*locate == 1 && sfml->sound->volume - 5 >= 0)
			sfml->sound->volume -= 5;
		sfml->key &= ~KEY_RIGHT;
	}
	sfMusic_setVolume(sfml->sound->music, sfml->sound->volume);
}

static void key_management(sfml_t *sfml, int *locate, int *exit)
{
	if (sfml->key & KEY_DOWN && !(sfml->key & PRES_DOWN)){
		*locate = (*locate + 1) % 3;
		sfml->key &= ~KEY_DOWN;
	}
	if (sfml->key & KEY_UP && !(sfml->key & PRES_UP)){
		*locate = (*locate + 2) % 3;
		sfml->key &= ~KEY_UP;
	}
	if ((sfml->key & KEY_ENTER && !(sfml->key & PRES_ENTER))
	|| (sfml->key & KEY_ESC && !(sfml->key & PRES_ESC))){
		sfml->key &= ~(KEY_ENTER | KEY_ESC);
		*exit = 0;
	}
	key_action(sfml, locate, exit);
}

int menu_settings(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, -(sfml->height >> 3));
		my_print(sfml, position, "Settings", option);
		text_management(sfml, &locate);
		my_display_vram(sfml);
		key_management(sfml, &locate, &exit);
		get_key(sfml);
		set_exit(sfml);
		free(option);
	}
	locate = (exit == -1) ? 1 : locate;
	return (locate);
}
