/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   LocationConfig.hpp                                 :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: lperrey <lperrey@student.42.fr>            +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/07/23 16:08:00 by me                #+#    #+#             */
/*   Updated: 2022/08/04 19:32:40 by erlazo           ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#ifndef LOCATIONCONFIG_HPP
# define LOCATIONCONFIG_HPP

# include <map>
# include <vector>
# include <string>
# include <iostream>
# include <sys/stat.h>	// stat()

# include <stdio.h>		// printf(), gotta go


# include "utils.hpp"

// again, struct instead?
class LocationConfig
{
public:
	// canonic stuff?

	std::string					path;	// /path and /path/ are fine
									// i add trailing / if a dir
	std::string					root;
	std::vector<std::string>	index;
	unsigned int				allow_methods;
	std::vector<std::string>	cgi_ext;	// php not .php
	bool						autoindex;

	std::string					upload_dir;

	int							redirect_status; // only in location
	std::string					redirect_uri;	// only 1 per location
	// au pire you do location / { return 301 http://location; }
	// and that's how you get the redirect from the root.

	void	print_all()
	{
		std::cout << "\nPRINTING A LOCATION\n";

		std::cout << "Path: " << path << '\n';
		std::cout << "root: " << root << '\n';
		std::cout << "autoindex: " << autoindex << '\n';

		std::cout << "Skipping index...\n";

		std::cout << "Location allow_methods: ";
		std::cout << ::http_methods_to_str(allow_methods) << "\n";

		std::cout << "Skipping redirect status etc...\n";		

		std::cout << "------\n";
	}

// works a lot better than  using a compare function...
	bool operator<(const LocationConfig& rhs) const
    {
        int	comp_lhs = 0;
		int	comp_rhs = 0;
		size_t	tmp = 0;

		while ((tmp = this->path.find_first_of("/", tmp)) != std::string::npos)
		{
			++tmp;
			++comp_lhs;
		}
		if (path[path.find_last_of("/") + 1] != '\0')
			++comp_lhs; 
		tmp = 0;
		while ((tmp = rhs.path.find_first_of("/", tmp)) != std::string::npos)
		{
			++tmp;
			++comp_rhs;
		}
		if (rhs.path[rhs.path.find_last_of("/") + 1] != '\0')
			++comp_rhs; 

		return (comp_lhs < comp_rhs);	// right comparison ? not <= ?
    };


};


#endif








