/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   init.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: erey-bet <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/12/29 18:06:18 by erey-bet          #+#    #+#             */
/*   Updated: 2023/01/04 17:07:05 by erey-bet         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "../so_long.h"

int	key_hook(int key, t_data *data)
{
	if (key == K_ESC)
		quit(data);
	else if (key == K_LEFT)
		move(data, -1, 0);
	else if (key == K_RIGHT)
		move(data, 1, 0);
	else if (key == K_UP)
		move(data, 0, -1);
	else if (key == K_DOWN)
		move(data, 0, 1);
	data->update = 1;
	return (0);
}

static void	init_data(t_data *data)
{
	data->update = 1;
	data->size = 128;
	data->x_player = 1;
	data->y_player = 1;
	data->x_ene = -1;
	data->y_ene = -1;
	data->move = 1;
	data->tick = 0;
	data->mlx = mlx_init();
	mlx_get_screen_size(data->mlx, &data->w_screen, &data->h_screen);
	data->imgs.ply = (void**)malloc(sizeof(void*) * 8);
	data->imgs.ply[0] = mlx_xpm_file_to_image(data->mlx, "assets/player_0.xpm", &data->size, &data->size);
	data->imgs.ply[1] = mlx_xpm_file_to_image(data->mlx, "assets/player_1.xpm", &data->size, &data->size);
	data->imgs.ply[2] = mlx_xpm_file_to_image(data->mlx, "assets/player_2.xpm", &data->size, &data->size);
	data->imgs.ply[3] = mlx_xpm_file_to_image(data->mlx, "assets/player_3.xpm", &data->size, &data->size);
	data->imgs.ply[4] = mlx_xpm_file_to_image(data->mlx, "assets/player_0*.xpm", &data->size, &data->size);
	data->imgs.ply[5] = mlx_xpm_file_to_image(data->mlx, "assets/player_1*.xpm", &data->size, &data->size);
	data->imgs.ply[6] = mlx_xpm_file_to_image(data->mlx, "assets/player_2*.xpm", &data->size, &data->size);
	data->imgs.ply[7] = mlx_xpm_file_to_image(data->mlx, "assets/player_3*.xpm", &data->size, &data->size);
	data->imgs.bg = mlx_xpm_file_to_image(data->mlx, "assets/background.xpm", &data->size, &data->size);
	data->imgs.wall = (void**)malloc(sizeof(void*) * 3);
	data->imgs.wall[0] = mlx_xpm_file_to_image(data->mlx, "assets/wall_0.xpm", &data->size, &data->size);
	data->imgs.wall[1] = mlx_xpm_file_to_image(data->mlx, "assets/wall_1.xpm", &data->size, &data->size);
	data->imgs.wall[2] = mlx_xpm_file_to_image(data->mlx, "assets/wall_2.xpm", &data->size, &data->size);
	data->imgs.col = (void**)malloc(sizeof(void*) * 3);
	data->imgs.col[0] = mlx_xpm_file_to_image(data->mlx, "assets/collectible_0.xpm", &data->size, &data->size);
	data->imgs.col[1] = mlx_xpm_file_to_image(data->mlx, "assets/collectible_1.xpm", &data->size, &data->size);
	data->imgs.col[2] = mlx_xpm_file_to_image(data->mlx, "assets/collectible_2.xpm", &data->size, &data->size);
	data->imgs.exit = (void**)malloc(sizeof(void*) * 3);
	data->imgs.exit[0] = mlx_xpm_file_to_image(data->mlx, "assets/exit_0.xpm", &data->size, &data->size);
	data->imgs.exit[1] = mlx_xpm_file_to_image(data->mlx, "assets/exit_1.xpm", &data->size, &data->size);
	data->imgs.exit[2] = mlx_xpm_file_to_image(data->mlx, "assets/exit_2.xpm", &data->size, &data->size);
	data->imgs.ene = mlx_xpm_file_to_image(data->mlx, "assets/enemy.xpm", &data->size, &data->size);	
}

int	trgb(int t, int r, int g, int b)
{
		return (t << 24 | r << 16 | g << 8 | b);
}


int	render(t_data *data)
{
	char	*str_move;
	t_xy	*pos;

	if (data->tick >= 75000 && data->x_ene != -1 && data->y_ene != -1)
	{
		data->tick = 0;
		move_enemy(data);
		draw(data);
	}
	else if (data->x_ene != -1 && data->y_ene != -1)
		data->tick++;
	if (data->update == 1)
	{
		if (data->x_ene != -1 && has_element(data->map, '3'))
		{
			pos = get_position(data->map, '3');
			if (data->x_player != pos->x || data->y_player != pos->y)
			{
				set_element(data, pos->x, pos->y, 'e');
				data->x_ene = pos->y;
				data->y_ene = pos->x;
			}
			free(pos);
		}
		draw(data);
		mlx_put_image_to_window(data->mlx, data->mlx_win, data->imgs.bg, 0, 0);
		str_move = ft_itoa(data->move);
		mlx_string_put(data->mlx, data->mlx_win, 100, 100, trgb(0, 150, 0, 0), str_move);
		free(str_move);
		data->update = 0;
	}
	return (0);
}

int	quit(t_data *data)
{
	int	i;

	mlx_destroy_image(data->mlx, data->imgs.bg);
	i = 0;
	while (i < 8)
		mlx_destroy_image(data->mlx, data->imgs.ply[i++]);
	free(data->imgs.ply);
	i = 0;
	while (i < 3)
		mlx_destroy_image(data->mlx, data->imgs.wall[i++]);
	free(data->imgs.wall);
	i = 0;
	while (i < 3)
		mlx_destroy_image(data->mlx, data->imgs.col[i++]);
	free(data->imgs.col);
	i = 0;
	while (i < 3)
		mlx_destroy_image(data->mlx, data->imgs.exit[i++]);
	free(data->imgs.exit);
	mlx_destroy_image(data->mlx, data->imgs.ene);
	mlx_clear_window(data->mlx, data->mlx_win);
	mlx_destroy_window(data->mlx, data->mlx_win);
	mlx_destroy_display(data->mlx);
	mlx_loop_end(data->mlx);
	i = 0;
	while (data->map[i])
		free(data->map[i++]);
	free(data->map);
	free(data->mlx);
	exit(0);
	return (0);
}
	
int	main(int argc, char *argv[])
{
	t_data	data;

	if (argc != 2)
		return (0);
	(void)argv;
	init_data(&data);
	if (get_map(argv, &data) == NULL)
		return (1);
	data.mlx_win = mlx_new_window(data.mlx, data.w_screen, data.h_screen, "lili meli");
	mlx_hook(data.mlx_win, 17, 0, quit, &data); 
	mlx_key_hook(data.mlx_win, key_hook, &data); 
	mlx_loop_hook(data.mlx, render, &data);
	mlx_loop(data.mlx);
	return (0);
}
