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

static int check_flags(const char *bin_name, const char *str)
{
	if (*str == '-' && (*(str + 1) == 'h' && *(str + 2) == '\0')
	|| (str[1] == '-' && str[2] == 'h' && str[3] == 'e'
	&& str[4] == 'l' && str[5] == 'p' && str[6] == '\0')){
		man_help(bin_name);
		return (1);
	}
	if (*str == '-' && str[1] == '-' && str[2] == 'v' && str[3] == 'e'
	&& str[4] == 'r' && str[5] == 's' && str[6] == 'i' && str[7] == 'o'
	&& str[8] == 'n' && str[9] == '\0'){
		my_printf("%s version 1.0.\n", bin_name);
		return (1);
	}
	return (0);
}

static void main_loop(sfml_t *sfml, char *map_name)
{
	void *window;
	int exit;
	int y;

	exit = 1;
	y = -(BMP_HEIGHT << 3);
	sfMusic_play(sfml->sound->music);
	sfMusic_setVolume(sfml->sound->music, sfml->sound->volume);
	while (sfml != NULL && exit != 2
	&& sfRenderWindow_isOpen(sfml->window)){
		while (sfml->key & PRES_ENTER)
			get_key(sfml);
		exit = menu_start(sfml, &y);
		if (!exit){
			intro(sfml);
			game_main(sfml, map_name);
		}
	}
}

int main(int argc, char **argv)
{
	sfml_t *sfml;
	int error = 0;
	int exit;

	if (argc != 2 || check_flags(argv[0], argv[1]))
		return ((argc != 2) ? 84 : 0);
	sfml = init_sfml(SCREEN_WIDTH, SCREEN_HEIGHT);
	if (sfml != NULL)
		sfml->scene = new_scene(argv[1], SCENE_LOAD_ALL_GAME);
	if (sfml == NULL || sfml->scene == NULL){
		if (sfml != NULL)
			free_scene(sfml->scene);
		free_sfml(sfml);
		return (84);
	}
	free_scene(sfml->scene);
	srand(time(NULL));
	main_loop(sfml, argv[1]);
	free_sfml(sfml);
	return (0);
}
