/*
    Copyright (C) 2012 Bartosz Broda, Jacek Bochenek, Łukasz Biłeńkij, 
    Damian Jaworski, Wojciech Mazur, Dominik Piasecki, Maciej Piasecki, 
    Radosław Ramocki
    
    This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option)
any later version.

    This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. 

    See the LICENSE and COPYING files for more details.
*/

#include "options.h"
#include <iostream>
#include <fstream>
#include <boost/program_options.hpp>
#include <boost/algorithm/string.hpp>

namespace tuplesproc{

void parse_options(int ac, char**av, options & opts)
{
	namespace po = boost::program_options;

	po::options_description basic("Basic options", 100);
	basic.add_options()
		("input", po::value<std::string>(& opts.input_path),
			"Directory or file with tuples. Exclusive with input-list.")
		("include", po::value<std::string>(& opts.include),
			"File name filter, when using input option only files matching this pattern will be parsed.")
		("file-list", po::value<std::string>(& opts.input_list),
			"Path to a file which contains files list to parse. Exclusive with input option.")
		("out", po::value<std::string>(& opts.output_path),
				"Output directory. Required!")
		("matrix-name", po::value<std::string>(& opts.matrix_name),
					"Matrix name. Required!")
		("matrix-description", po::value<std::string>(&opts.matrix_description)->default_value("No description given"),
					"Optional description of a matrix")
		("tokens", po::value<long>(& opts.tokens)->default_value(0),
				"Number of tokens in a corpus.")
		("dict", po::value<std::string>(&opts.dict_file),
				"File with (global) term frequency dictionary")
		("row-file", po::value<std::string>(& opts.row_path),
				"File which contains set of rows to write in SuperMatrix")
		("column-file", po::value<std::string>(& opts.col_path),
		 "File which contains set of columns to write in SuperMatrix")
		("relation-file", po::value<std::string>(& opts.relation_path)->default_value(""),
				"File which contains set of relation to write in SuperMatrix")
		("relations", po::value<std::vector<std::string> >(&opts.relations),
				"Relations to write in SuperMatrix")
		("row-min-tf", po::value<long>(&opts.row_min_tf)->default_value(0),
				"Minimum value of term frequency in rows features")
		("col-min-tf", po::value<long>(&opts.col_min_tf)->default_value(0),
				"Minimum value of term frequency in columns features")
		("interval", po::value<size_t>(&opts.raport_interval)->default_value(1000000),
			"how often to print progress (in lines) during reading of tulpes files")
		("help", "Prints this message")

		;

	po::variables_map vm;
	po::store(po::parse_command_line(ac, av, basic), vm);
	po::notify(vm);

	if (vm.count("help"))
	{
		std::cout << "tuplesproc by Bartosz Broda, bartosz.broda@pwr.wroc.pl\n" << basic<< std::endl;
		exit(0);
	}

	if (vm.count("input") && vm.count("file-list"))
	{
		std::cout << "Options input and file-list are exclusive!\n"
			<< basic << std::endl;
		exit(0);
	}

	if (!vm.count("input") && !vm.count("file-list"))
	{
		std::cout << "You need to specify input or file-list!\n"
				  << basic<< std::endl;
		exit(0);
	}

	if (!vm.count("out"))
	{
		std::cout << "You need to specify output directory!\n"
				  << basic<< std::endl;
		exit(0);
	}

	if (!vm.count("matrix-name"))
	{
		std::cout << "You need to specify name of a matrix!\n"
				  << basic<< std::endl;
		exit(0);
	}
}

void load_dict(const std::string &filename, std::map<std::string, size_t> &dict)
{
	std::ifstream file(filename.c_str());
	if(!file)
		return;
	std::string line;
	while( (std::getline(file, line)) ){
		boost::algorithm::trim(line);
		std::vector<std::string> tokens;
		boost::algorithm::split(tokens, line, boost::is_any_of(";"));
		if(tokens.size() == 2)
			dict[tokens[0] ] = boost::lexical_cast<size_t>(tokens[1]);

	}
}

} // namespace takipid

