/*
** EPITECH PROJECT, 2018
** task01
** File description:
** I do task
*/
#include <stdlib.h>
#include "lib/my_graphics.h"
#include "lib/s_sfml.h"
#include "lib/my_stdio.h"
#include "lib/my_memory.h"
#include "game/s_game.h"
#include "game/menu.h"
#include "game/memory.h"
#include "game/game.h"
#include "game/draw.h"
#include "game/core.h"
#include "game/ascii.h"
#include "game/kinematic.h"

static void key_management(sfml_t *sfml, int *locate, int *exit)
{
	get_key(sfml);
	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_ENTER;
		if (*locate == 1)
			menu_settings(sfml);
		else
			*exit = 0;

	}
}

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

	my_print_set_position(pos, 0,
	(sfml->height >> 1) + (sfml->height >> 4));
	option[3] = (!*locate) ? ASCII_CENTER_X |
ASCII_REVERSE : ASCII_CENTER_X;
	my_print(sfml, pos, "Play", option);
	my_print_set_position(pos, 0,
	(sfml->height >> 1) + 2 * (sfml->height >> 4));
	option[3] = (*locate == 1) ? ASCII_CENTER_X |
ASCII_REVERSE : ASCII_CENTER_X;
	my_print(sfml, pos, "Settings", option);
	my_print_set_position(pos, 0,
	(sfml->height >> 1) + 3 * (sfml->height >> 4));
	option[3] = (*locate == 2) ? ASCII_CENTER_X |
ASCII_REVERSE : ASCII_CENTER_X;
	my_print(sfml, pos, "Exit", option);
	key_management(sfml, locate, exit);
}

static void display_scene(sfml_t *sfml, int y, int *fade_active)
{
	draw_logo_bmp(sfml, y);
	if (!*fade_active){
		fade(sfml, FADE_OPEN);
		*fade_active = 1;
	}
	else
		my_display_vram(sfml);

}

static void rendering(sfml_t *sfml, uint32_t *option, int *locate, int *exit)
{
	my_clear_vram(sfml, 0xffffffff);
	map_rendering(sfml);
	player_rendering(sfml);
	draw_thrower_object(sfml);
	update_thrower(sfml, sfml->scene->thrower, sfml->scene->level);
	choice_management(sfml, option, locate, exit);
}

int menu_start(sfml_t *sfml, int *y)
{
	uint32_t *option = my_print_set_option(3, 0x000000ff, 0xffffffff, 0);
	int end = (SCREEN_HEIGHT >> 2) - (BMP_HEIGHT << 3 >> 1);
	int fade_active = 0;
	int locate = 0;
	int exit = 1;

	sfml->key = 0x00;
	sfml->scene = new_scene("src/scene_map/map_menu_start.txt",
	SCENE_LOAD_PLAYER | SCENE_LOAD_THROWER);
	if (sfml->scene == NULL)
		return (0);
	while (sfRenderWindow_isOpen(sfml->window) && exit){
		rendering(sfml, option, &locate, &exit);
		display_scene(sfml, *y, &fade_active);
		*y += (*y < end) ? BMP_SPEED : 0;
		set_exit(sfml);
	}
	fade(sfml, FADE_CLOSE);
	free(option);
	free_scene(sfml->scene);
	return (locate);
}
