/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_draw_ammo_bar.c                                 :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/22 20:02:47 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/22 20:02:48 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <cub3d.h>
#include <stdint.h>

static void
	ft_draw_tnum(int8_t id, int16_t y, int16_t x, t_cub *cl)
{
	const int16_t	scl = cl->mlist.scale;

	cl->img.ptr[(x + 10) * 4 +
		(cl->img.sizeline * (int)(y + ((cl->wlist.y_size - 10) -
		(2 * ((cl->mlist.map_h * scl) / 1.3)) - 40)))] =
		(uint8_t)cl->tnum[id].ptr[cl->tnum[id].tex_x_alt * 4 + 4 *
		cl->tnum[id].img_w * cl->tnum[id].tex_y_alt];
	cl->img.ptr[(x + 10) * 4 +
		(cl->img.sizeline * (int)(y + ((cl->wlist.y_size - 10) -
		(2 * ((cl->mlist.map_h * scl) / 1.3)) - 40))) + 1] =
		(uint8_t)cl->tnum[id].ptr[cl->tnum[id].tex_x_alt * 4 + 4 *
		cl->tnum[id].img_w * cl->tnum[id].tex_y_alt + 1];
	cl->img.ptr[(x + 10) * 4 +
		(cl->img.sizeline * (int)(y + ((cl->wlist.y_size - 10) -
		(2 * ((cl->mlist.map_h * scl) / 1.3)) - 40))) + 2] =
		(uint8_t)cl->tnum[id].ptr[cl->tnum[id].tex_x_alt * 4 + 4 *
		cl->tnum[id].img_w * cl->tnum[id].tex_y_alt + 2];
}

static void
	ft_put_next_number(char n, uint16_t x_offset, t_cub *cl)
{
	int32_t	x_ratio;
	int32_t	y_ratio;
	int16_t	x;
	int16_t	y;

	x_ratio = (int)(((cl->tnum[n - 48].img_w) << 16) / cl->ammo_num_w) + 1;
	y_ratio = (int)(((cl->tnum[n - 48].img_h) << 16) / cl->ammo_num_h) + 1;
	y = 0;
	while (y < cl->ammo_num_h)
	{
		cl->tnum[n - 48].tex_y_alt = ((y * y_ratio) >> 16);
		x = 0;
		while (x < cl->ammo_num_w)
		{
			cl->tnum[n - 48].tex_x_alt = ((x * x_ratio) >> 16);
			if (cl->tnum[n - 48].ptr[cl->tnum[n - 48].tex_x_alt * 4 + 4 *
				cl->tnum[n - 48].img_h * cl->tnum[n - 48].tex_y_alt])
				ft_draw_tnum(n - 48, y, x + x_offset, cl);
			x++;
		}
		y++;
	}
}

static void
	ft_put_image_from_number(char *num, t_cub *cl)
{
	const size_t	len = ft_strlen(num);
	const int16_t	scl = cl->mlist.scale;

	if (len == 4)
	{
		ft_put_next_number(num[0], 0, cl);
		ft_put_next_number(num[1], (1 * (cl->mlist.map_w * scl) / 4), cl);
		ft_put_next_number(num[2], (2 * (cl->mlist.map_w * scl) / 4), cl);
		ft_put_next_number(num[3], (3 * (cl->mlist.map_w * scl) / 4), cl);
	}
	else if (len == 3)
	{
		ft_put_next_number(num[0], (1 * (cl->mlist.map_w * scl) / 4), cl);
		ft_put_next_number(num[1], (2 * (cl->mlist.map_w * scl) / 4), cl);
		ft_put_next_number(num[2], (3 * (cl->mlist.map_w * scl) / 4), cl);
	}
	else if (len == 2)
	{
		ft_put_next_number(num[0], (1 * (cl->mlist.map_w * scl) / 4), cl);
		ft_put_next_number(num[1], (2 * (cl->mlist.map_w * scl) / 4), cl);
	}
	else
		ft_put_next_number(num[0], (2 * (cl->mlist.map_w * scl) / 4), cl);
}

static void
	ft_get_hw(t_cub *cl)
{
	int16_t			x;
	int16_t			y;
	const int16_t	scl = cl->mlist.scale;

	x = 10;
	y = (cl->wlist.y_size - 10) - (2 * ((cl->mlist.map_h * scl) / 1.3)) - 40;
	while (y < (int16_t)(cl->wlist.y_size - (cl->mlist.map_h * scl)) - 30)
	{
		while (x < (int16_t)((cl->mlist.map_w * scl) / 4) + 20)
			x++;
		y++;
	}
	y -= (cl->wlist.y_size - 10) - (2 * ((cl->mlist.map_h * scl) / 1.3)) - 40;
	x -= 10;
	y = (y <= 0) ? (1) : (y);
	x = (x <= 0) ? (1) : (x);
	cl->ammo_num_h = y;
	cl->ammo_num_w = x;
}

void
	ft_draw_ammo_bar(t_cub *cl)
{
	char			num[5];
	uint8_t			w_id;

	w_id = cl->plist.handles_weapon;
	w_id = (w_id == 2) ? (1) : (w_id);
	w_id = (w_id == 4) ? (2) : (w_id);
	ft_get_hw(cl);
	if (cl->plist.ammo[w_id] >= 0)
	{
		ft_sprintf(num, "%hd", cl->plist.ammo[w_id]);
		ft_put_image_from_number(num, cl);
	}
	else if (cl->plist.ammo[w_id] == -4)
	{
		ft_put_next_number(59,
			(2 * (cl->mlist.map_w * cl->mlist.scale) / 4), cl);
	}
}