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

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

static void
	ft_free_some_extras(t_cub *clist)
{
	uint8_t	i;

	i = 0;
	while (i < 8)
	{
		ft_memdel((void*)&clist->sprites[i]);
		i++;
	}
	ft_memdel((void*)&clist->sprites);
	ft_memdel((void*)&clist);
}

static void
	ft_free_lists(t_cub *clist)
{
	ft_memdel((void*)&clist->mlist.filename);
	ft_memdel((void*)&clist->mlist.no_tex_path);
	ft_memdel((void*)&clist->mlist.so_tex_path);
	ft_memdel((void*)&clist->mlist.ea_tex_path);
	ft_memdel((void*)&clist->mlist.we_tex_path);
	ft_memdel((void*)&clist->mlist.nl_tex_path);
	ft_memdel((void*)&clist->mlist.fl_tex_path);
	ft_memdel((void*)&clist->mlist.ce_tex_path);
	ft_memdel((void*)&clist->mlist.nlevel_path);
	ft_memdel((void*)&clist->mlist.skybox_path);
	ft_memdel((void*)&clist->mlist.traps_path);
	ft_memdel((void*)&clist->mlist.music_path);
	ft_memdel((void*)&clist->mlist.music_cmd);
	ft_memdel((void*)&clist->mlist.mapl);
	ft_free_words(clist->mlist.map);
	ft_memdel((void*)&clist->sfx.death);
	ft_memdel((void*)&clist->sfx.footstep_one);
	ft_memdel((void*)&clist->sfx.footstep_two);
	ft_memdel((void*)&clist->sfx.new_lvl);
	ft_memdel((void*)&clist->sfx.pain_one);
	ft_memdel((void*)&clist->sfx.pain_two);
	ft_memdel((void*)&clist->sfx.trap);
	ft_free_sprites(clist->mlist.sprite_path);
	if (!clist->wlist.inited)
		ft_memdel((void*)&clist->wlist.winptr);
}

static void
	ft_del_tex(t_cub *clist)
{
	if (clist->tlist[0].img)
		mlx_destroy_image(clist->wlist.wlx, clist->tlist[0].img);
	if (clist->tlist[1].img)
		mlx_destroy_image(clist->wlist.wlx, clist->tlist[1].img);
	if (clist->tlist[2].img)
		mlx_destroy_image(clist->wlist.wlx, clist->tlist[2].img);
	if (clist->tlist[3].img)
		mlx_destroy_image(clist->wlist.wlx, clist->tlist[3].img);
	if (clist->tlist[4].img)
		mlx_destroy_image(clist->wlist.wlx, clist->tlist[4].img);
	if (clist->mlist.isnlvl && clist->tlist[5].img)
		mlx_destroy_image(clist->wlist.wlx, clist->tlist[5].img);
	if (clist->mlist.isftex && clist->tlist[6].img)
		mlx_destroy_image(clist->wlist.wlx, clist->tlist[6].img);
	if (clist->mlist.isctex && clist->tlist[7].img)
		mlx_destroy_image(clist->wlist.wlx, clist->tlist[7].img);
	if (clist->mlist.istraps && clist->tlist[15].img)
		mlx_destroy_image(clist->wlist.wlx, clist->tlist[15].img);
	if (clist->tlist[16].img)
		mlx_destroy_image(clist->wlist.wlx, clist->tlist[16].img);
	ft_del_extra_sprites(clist);
}

static void
	ft_cancel_threads(t_cub *clist)
{
	if (clist->isoldmus && clist->wlist.inited)
	{
		pthread_cancel(clist->mtid);
		pthread_join(clist->mtid, NULL);
	}
	pthread_mutex_unlock(&clist->sfx.death_mutex);
	pthread_cancel(clist->sfx.death_tid);
	pthread_join(clist->sfx.death_tid, NULL);
	pthread_mutex_unlock(&clist->sfx.footstep_mutex);
	pthread_cancel(clist->sfx.footstep_tid);
	pthread_join(clist->sfx.footstep_tid, NULL);
	pthread_mutex_unlock(&clist->sfx.new_lvl_mutex);
	pthread_cancel(clist->sfx.new_lvl_tid);
	pthread_join(clist->sfx.new_lvl_tid, NULL);
	pthread_mutex_unlock(&clist->sfx.pain_mutex);
	pthread_cancel(clist->sfx.pain_tid);
	pthread_join(clist->sfx.pain_tid, NULL);
	pthread_mutex_unlock(&clist->sfx.trap_mutex);
	pthread_cancel(clist->sfx.trap_tid);
	pthread_join(clist->sfx.trap_tid, NULL);
}

int
	ft_exit(uint8_t exit_code, t_cub *clist)
{
	if (clist->walltexgood)
		ft_del_tex(clist);
	if (clist->wlist.inited)
	{
		if (FT_OS == 1)
			mlx_destroy_image(clist->wlist.wlx, clist->img.img);
		mlx_destroy_window(clist->wlist.wlx, clist->wlist.winptr);
	}
	ft_cancel_threads(clist);
	ft_free_lists(clist);
	ft_free_some_extras(clist);
	ft_printf("Exiting program\n");
	exit(exit_code);
	return (0);
}