/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   parse_file.c                                       :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: gbrochar <gbrochar@student.42.fr>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/12/22 12:38:27 by gbrochar          #+#    #+#             */
/*   Updated: 2020/12/22 18:59:30 by gbrochar         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "scop.h"

int					parse_file(t_env *e)
{
	FILE			*fp;
	char			*buffer;

	fp = fopen(e->file_name, "r");
	if (!fp)
		return (FAILURE);
	buffer = (char *)malloc(sizeof(char) * 4096);
	while (fgets(buffer, 4096, fp))
	{
		if (parse_line(e, buffer) == FAILURE)
			return (FAILURE);
	}
	free(buffer);
	fclose(fp);
	return (SUCCESS);
}

void				set_params(t_env *e, int argc, char **argv)
{
	if (argc >= 2)
		e->file_name = strdup(argv[1]);
	else
		e->file_name = strdup("resources/42.obj");
	if (argc >= 3)
		e->texture_file_name = strdup(argv[2]);
	else
		e->texture_file_name = strdup("resources/poney.jpg");
	if (argc >= 4)
		e->window.width = atoi(argv[3]);
	if (argc >= 5)
		e->window.height = atoi(argv[4]);
	if (argc >= 6)
		e->camera.fov = atof(argv[5]);
	if (e->window.width > 7680) {
		ft_putstr("window width has been adjusted to 7680, the maximum supported\n");
		e->window.width = 7680;
	}
	if (e->window.width < 240) {
		ft_putstr("window width has been adjusted to 240, the minimum supported\n");
		e->window.width = 240;
	}
	if (e->window.height > 7680) {
		ft_putstr("window height has been adjusted to 7680, the maximum supported\n");
		e->window.height = 7680;
	}
	if (e->window.height < 240) {
		ft_putstr("window height has been adjusted to 240, the minimum supported\n");
		e->window.height = 240;
	}
	e->camera.aspect = (double)e->window.width / (double)e->window.height;
}

int					parse(t_env *e, int argc, char **argv)
{
	set_params(e, argc, argv);
	return (parse_file(e));
}

