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

#include "../so_long.h"

int	key_hook(int key, t_data *data)
{
	if (key == K_ESC)
		quit(data);
	if (data->update < 2)
	{
		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);
	}
	if (data->update == 0)
		data->update = 1;
	return (0);
}

static int	init_img(t_data *data)
{
	data->imgs.ply = (void **)malloc(sizeof(void *) * 8);
	if (data->imgs.ply == NULL)
		return (0);
	data->imgs.ply[0] = mxfi(data, "assets/player_0.xpm");
	data->imgs.ply[1] = mxfi(data, "assets/player_1.xpm");
	data->imgs.ply[2] = mxfi(data, "assets/player_2.xpm");
	data->imgs.ply[3] = mxfi(data, "assets/player_3.xpm");
	data->imgs.ply[4] = mxfi(data, "assets/player_0*.xpm");
	data->imgs.ply[5] = mxfi(data, "assets/player_1*.xpm");
	data->imgs.ply[6] = mxfi(data, "assets/player_2*.xpm");
	data->imgs.ply[7] = mxfi(data, "assets/player_3*.xpm");
	data->imgs.wall = (void **)malloc(sizeof(void *) * 3);
	if (data->imgs.ply == NULL)
		return (0);
	data->imgs.wall[0] = mxfi(data, "assets/wall_0.xpm");
	data->imgs.wall[1] = mxfi(data, "assets/wall_1.xpm");
	data->imgs.wall[2] = mxfi(data, "assets/wall_2.xpm");
	data->imgs.col = (void **)malloc(sizeof(void *) * 3);
	if (data->imgs.ply == NULL)
		return (0);
	data->imgs.col[0] = mxfi(data, "assets/collectible_0.xpm");
	data->imgs.col[1] = mxfi(data, "assets/collectible_1.xpm");
	data->imgs.col[2] = mxfi(data, "assets/collectible_2.xpm");
	return (1);
}

static void	init_data(t_data *data)
{
	data->update = 1;
	data->size = 128;
	data->x_ply = 1;
	data->y_ply = 1;
	data->move = 0;
	data->map = NULL;
	data->mlx = mlx_init();
	data->mlx_win = NULL;
	mlx_get_screen_size(data->mlx, &data->w_scr, &data->h_scr);
	if (!init_img(data))
		return ;
	data->imgs.exit = (void **)malloc(sizeof(void *) * 3);
	if (data->imgs.ply == NULL)
		return ;
	data->imgs.exit[0] = mxfi(data, "assets/exit_0.xpm");
	data->imgs.exit[1] = mxfi(data, "assets/exit_1.xpm");
	data->imgs.exit[2] = mxfi(data, "assets/exit_2.xpm");
	data->imgs.bg = mxfi(data, "assets/background.xpm");
}

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

	if (data->update == 1)
	{
		pre_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	main(int argc, char *argv[])
{
	char	*error;
	t_data	data;

	if (argc != 2)
		return (0);
	init_data(&data);
	error = get_map(argv, &data);
	if (error)
	{
		ft_putstr_fd(error, 1);
		quit(&data);
	}
	data.mlx_win = mlx_new_window(data.mlx, data.w_scr,
			data.h_scr, "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);
}
