/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_init_lists.c                                    :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:28:53 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:28:53 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <mlx.h>
#include <cub3d.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <pthread.h>

/*
** sfx[] index summary
** -------------------
**  0: death
**  1: footstep | double
**  2: new level
**  3: pain | double
**  4: trap
**  5: heal
**  6: weapon one load
**  7: weapon one fire
**  8: weapon two load
**  9: weapon two fire
** 10: weapon three load
** 11: weapon three fire
** 12: out of ammunitions
** 13: enemy death
** 14: enemy scream | double
** 15: enemy fire
** 16: weapon two fire overdub
** 17: weapon two fire overdub x2
*/

static int8_t
	ft_init_sfx_cmd(char **target, const char *path)
{
	uint8_t	len;

	len = ft_strlen(path);
	len += ft_strlen(FT_SND_CMD) - 2;
	if (!(*target = (char *)malloc((len + 1) * sizeof(char))))
		return (-1);
	ft_sprintf(*target, FT_SND_CMD, path);
	return (0);
}

static void
	ft_init_sfx_pthreads(t_cub *cl)
{
	pthread_create(&cl->sfx[0].tid, NULL, ft_sfx_death_thread, &cl->sfx);
	pthread_create(&cl->sfx[1].tid, NULL, ft_sfx_footstep_thread, &cl->sfx);
	pthread_create(&cl->sfx[2].tid, NULL, ft_sfx_new_lvl_thread, &cl->sfx);
	pthread_create(&cl->sfx[3].tid, NULL, ft_sfx_pain_thread, &cl->sfx);
	pthread_create(&cl->sfx[4].tid, NULL, ft_sfx_trap_thread, &cl->sfx);
	pthread_create(&cl->sfx[5].tid, NULL, ft_sfx_heal_thread, &cl->sfx);
	pthread_create(&cl->sfx[6].tid, NULL, ft_sfx_w_one_load_thread, &cl->sfx);
	pthread_create(&cl->sfx[7].tid, NULL, ft_sfx_w_one_fire_thread, &cl->sfx);
	pthread_create(&cl->sfx[8].tid, NULL, ft_sfx_w_two_load_thread, &cl->sfx);
	pthread_create(&cl->sfx[9].tid, NULL, ft_sfx_w_two_fire_thread, &cl->sfx);
	pthread_create(&cl->sfx[10].tid, NULL,
		ft_sfx_weapon_three_load_thread, &cl->sfx);
	pthread_create(&cl->sfx[11].tid, NULL,
		ft_sfx_weapon_three_fire_thread, &cl->sfx);
	pthread_create(&cl->sfx[12].tid, NULL, ft_sfx_ooa_thread, &cl->sfx);
	pthread_create(&cl->sfx[13].tid, NULL, ft_sfx_bb_death_thread, &cl->sfx);
	pthread_create(&cl->sfx[14].tid, NULL, ft_sfx_bb_scream_thread, &cl->sfx);
	pthread_create(&cl->sfx[15].tid, NULL, ft_sfx_bb_fire_thread, &cl->sfx);
	pthread_create(&cl->sfx[16].tid, NULL,
		ft_sfx_weapon_two_fire_thread_alt, &cl->sfx);
	pthread_create(&cl->sfx[17].tid, NULL,
		ft_sfx_weapon_two_fire_thread_alt_alt, &cl->sfx);
}

static void
	ft_init_sfx_funptr(t_cub *cl)
{
	cl->sfx[0].sfx_play = ft_sfx_death;
	cl->sfx[1].sfx_play = ft_sfx_footstep;
	cl->sfx[2].sfx_play = ft_sfx_new_level;
	cl->sfx[3].sfx_play = ft_sfx_pain;
	cl->sfx[4].sfx_play = ft_sfx_trap;
	cl->sfx[5].sfx_play = ft_sfx_heal;
	cl->sfx[6].sfx_play = ft_sfx_weapon_one_load;
	cl->sfx[7].sfx_play = ft_sfx_weapon_one_fire;
	cl->sfx[8].sfx_play = ft_sfx_weapon_two_load;
	cl->sfx[9].sfx_play = ft_sfx_weapon_two_fire;
	cl->sfx[10].sfx_play = ft_sfx_weapon_three_load;
	cl->sfx[11].sfx_play = ft_sfx_weapon_three_fire;
	cl->sfx[12].sfx_play = ft_sfx_ooa;
	cl->sfx[13].sfx_play = ft_sfx_bb_death;
	cl->sfx[14].sfx_play = ft_sfx_bb_scream;
	cl->sfx[15].sfx_play = ft_sfx_bb_fire;
	cl->sfx[16].sfx_play = ft_sfx_weapon_two_fire_alt;
	cl->sfx[17].sfx_play = ft_sfx_weapon_two_fire_alt_alt;
}

static int8_t
	ft_init_sfx_cmds(t_cub *cl)
{
	if (ft_init_sfx_cmd(&cl->sfx[0].cmd, FT_SFX_DEATH_PATH) < 0 ||
		ft_init_sfx_cmd(&cl->sfx[1].cmd, FT_SFX_FS_ONE_PATH) < 0 ||
		ft_init_sfx_cmd(&cl->sfx[1].cmd_alt, FT_SFX_FS_TWO_PATH) < 0 ||
		ft_init_sfx_cmd(&cl->sfx[2].cmd, FT_SFX_N_LVL_PATH) < 0 ||
		ft_init_sfx_cmd(&cl->sfx[3].cmd, FT_SFX_SCR_ONE_PATH) < 0 ||
		ft_init_sfx_cmd(&cl->sfx[3].cmd_alt, FT_SFX_SCR_TWO_PATH) < 0 ||
		ft_init_sfx_cmd(&cl->sfx[4].cmd, FT_SFX_TRAP_PATH) < 0 ||
		ft_init_sfx_cmd(&cl->sfx[5].cmd, FT_SFX_HEAL_PATH) < 0 ||
		ft_init_sfx_cmd(&cl->sfx[6].cmd, FT_SFX_W_ONE_LOAD_PATH) < 0 ||
		ft_init_sfx_cmd(&cl->sfx[7].cmd, FT_SFX_W_ONE_FIRE_PATH) < 0 ||
		ft_init_sfx_cmd(&cl->sfx[8].cmd, FT_SFX_W_TWO_LOAD_PATH) < 0 ||
		ft_init_sfx_cmd(&cl->sfx[9].cmd, FT_SFX_W_TWO_FIRE_PATH) < 0 ||
		ft_init_sfx_cmd(&cl->sfx[10].cmd, FT_SFX_W_THREE_LOAD_PATH) < 0 ||
		ft_init_sfx_cmd(&cl->sfx[11].cmd, FT_SFX_W_THREE_FIRE_PATH) < 0 ||
		ft_init_sfx_cmd(&cl->sfx[12].cmd, FT_SFX_OOA_PATH) < 0 ||
		ft_init_sfx_cmd(&cl->sfx[13].cmd, FT_SFX_ENMY_DEATH_PATH) < 0 ||
		ft_init_sfx_cmd(&cl->sfx[14].cmd, FT_SFX_ENMY_SCR_ONE_PATH) < 0 ||
		ft_init_sfx_cmd(&cl->sfx[14].cmd_alt, FT_SFX_ENMY_SCR_TWO_PATH) < 0 ||
		ft_init_sfx_cmd(&cl->sfx[15].cmd, FT_SFX_ENMY_FIRE_PATH) < 0 ||
		ft_init_sfx_cmd(&cl->sfx[16].cmd, FT_SFX_W_TWO_FIRE_PATH) < 0 ||
		ft_init_sfx_cmd(&cl->sfx[17].cmd, FT_SFX_W_TWO_FIRE_PATH) < 0)
		return (-1);
	return (0);
}

int8_t
	ft_init_sfx(t_cub *cl)
{
	uint8_t	i;

	if (ft_init_sfx_cmds(cl) < 0)
		return (-1);
	i = 0;
	while (i < FT_TOTAL_SFX)
	{
		pthread_mutex_init(&cl->sfx[i].mutex, NULL);
		pthread_mutex_lock(&cl->sfx[i].mutex);
		i++;
	}
	ft_init_sfx_pthreads(cl);
	ft_init_sfx_funptr(cl);
	return (0);
}