/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   main.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: tchampio <tchampio@student.42lehavre.      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/05/06 13:16:11 by tchampio          #+#    #+#             */
/*   Updated: 2025/06/25 18:07:40 by tchampio         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "../includes/libft.h"
#include "../includes/structs.h"
#include "../mlx/mlx.h"
#include "../includes/maputils.h"
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>

void	my_mlx_pixel_put(t_mlx_data *data, int x, int y, int color)
{
	char	*dst;

	dst = data->addr + (y * data->line_length + x * (data->bits_per_pixel / 8));
	*(unsigned int *)dst = color;
}

void	draw_2d_wall(unsigned int color, t_mlx_data *data,
					int size, int x, int y)
{
	int	i;
	int	j;
	
	i = 0;
	while (i < size)
	{
		j = 0;
		while (j < size)
		{
			my_mlx_pixel_put(data, x + i, y + j, color);
			j++;
		}
		i++;
	}
}

int	destroy(t_cub3d_data *data)
{
	free_map(data->map);
	if (data->mlx_win)
		mlx_destroy_window(data->mlx, data->mlx_win);
	if (data->mlx_data)
		mlx_destroy_image(data->mlx, data->mlx_data->img);
	free(data->mlx_data);
	if (data->mlx)
		mlx_destroy_display(data->mlx);
	free(data->mlx);
	exit(0);
	return 0;
}

int	key_destroy(int keycode, t_cub3d_data *data)
{
	if (keycode == 65307)
		destroy(data);
	return (0);
}

void	draw_map(t_mapdata *map, t_mlx_data *data)
{
	int	i;
	int	j;

	i = 0;
	while (map->map[i])
	{
		j = 0;
		while (map->map[i][j])
		{
			if (map->map[i][j] == '1')
				draw_2d_wall(map->f_color, data, 20, 20 * j, 20 * i);
			else if (ft_strchr("NSEW", map->map[i][j]))
				draw_2d_wall(0x00FF0000, data, 20, 20 * j, 20 * i);
			j++;
		}
		i++;
	}
}

int	main(int argc, char **argv)
{
	t_cub3d_data data;

	data.map = ft_calloc(sizeof(t_mapdata), 1);
	if (argc < 2)
		return (ft_printf("Error: Missing cub3d file\n"), 1);
	if (!check_cubfile(argv[1], data.map))
		return (ft_printf("Error: Wrong map file. Reason: %s\n", data.map->error), 1);
	data.mlx = mlx_init();
	data.mlx_win = mlx_new_window(data.mlx, 800, 600, "Cub3d");
	data.mlx_data = ft_calloc(sizeof(t_mlx_data), 1);
	data.mlx_data->img = mlx_new_image(data.mlx, 800, 600);
	data.mlx_data->addr = mlx_get_data_addr(data.mlx_data->img, &data.mlx_data->bits_per_pixel, &data.mlx_data->line_length, &data.mlx_data->endian);
	mlx_hook(data.mlx_win, 17, 0L, destroy, &data);
	draw_map(data.map, data.mlx_data);
	mlx_key_hook(data.mlx_win, key_destroy, &data);
	mlx_put_image_to_window(data.mlx, data.mlx_win, data.mlx_data->img, 0, 0);
	mlx_loop(data.mlx);
}
