/*
    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 "arch_headers.h"
#include "Options.h"

// corpus file
#define CFG_CORPUS "corpus"
// corpus info
#define CFG_CORPUS_INFO "corpus-info"
// workdir
#define CFG_WORKDIR "workdir" 
// matrix name
#define CFG_MATRIX_NAME "matrix-name"
// column_operators [me lub nie - z definicji opisu]
#define CFG_COLUMNS_UNITS "column-units"
// jw wiersza
#define CFG_ROW_ME "row-me"
// slowa wiersza
#define CFG_ROW_SIMPLE_WORDS "row-simple-words"
// typ korpusu - 'poliqarp' lub 'xml'
#define CFG_CORPUS_TYPE "corpus-type"
// czy filtrowac disamby
#define CFG_FILTER_DISAMB "filter-disamb"

#define CFG_HELP "help"
#define CFG_CONFIG_FILE "cfg-file"

namespace me_architect
{
	Options::Options(int argc, char *argv[], Config *config)
		: m_pConfig(config)
	{
		Initalize(argc, argv);
	}

	void Options::Initalize(int argc, char *argv[])
	{
		namespace po= boost::program_options;
		std::string configfile, corpus_type_str, filter_disamb_str;
		
		po::options_description cmdline("Command line only options", 100);
		po::options_description basic("Basic options", 100);

		cmdline.add_options()
			(CFG_HELP, "Prints this message")
			(CFG_CONFIG_FILE, po::value<std::string>(&configfile)->default_value(std::string("me_architect.cfg")), "Config file")
			;
		basic.add_options()
			(CFG_CORPUS, po::value<std::string>(& m_pConfig->m_sCorpusFile), "Coprus path (file with paths to documents or poliqarp path)")
			(CFG_CORPUS_TYPE, po::value<std::string>(&corpus_type_str)->default_value("poliqarp"), "Type of corpus: poliqarp or xml")
			(CFG_CORPUS_INFO, po::value<std::string>(& m_pConfig->m_sCorpusInfo), "Corpus description")
			(CFG_WORKDIR, po::value<std::string>(& m_pConfig->m_sWorkdir), "Workdir (output dir for matrix)")
			(CFG_MATRIX_NAME, po::value<std::string>(& m_pConfig->m_sMatrixName), "Matrix name" )
			(CFG_ROW_SIMPLE_WORDS, po::value<std::vector<std::string> >(& m_pConfig->m_vRowTermFiles ), "Single word lexical units for rows, format:\n"
			 "file_with_units|flex_class")
			(CFG_ROW_ME, po::value<std::vector<std::string> >(& m_pConfig->m_vRowUnits), "Files with multiwords expresions for rows")
			(CFG_COLUMNS_UNITS, po::value<std::vector<std::string> >(& m_pConfig->m_vOperatorDefines), "Column description, format:\n"
			 "file_with_joskipi_operator|file_with_words|flex_class OR:\n"
			 "file_with_joskipi_operator|file_with_mulitowrds_expresions")
			(CFG_FILTER_DISAMB, po::value<std::string>(&filter_disamb_str)->default_value("true"), "If true only non-disambs will be filtered out")
			;

		po::variables_map vm;
		po::options_description all("",100);
		
		all.add(cmdline).add(basic);

		po::store(po::parse_command_line(argc, argv, all), vm);
		po::notify(vm); 

		if( vm.count(CFG_HELP) )
		{
			std::cout << "ME_ARCHITECT, by Bartosz Broda, bartosz.broda@pwr.wroc.pl\n";
			std::cout << all << std::endl;
			exit(0);
		}
		std::ifstream cfgfile( configfile.c_str() );

		if(cfgfile)
		{
			po::store(po::parse_config_file(cfgfile, basic), vm);
			po::notify(vm);
		}
		else if( vm.count(CFG_CONFIG_FILE) )
			std::cerr << "Cannot open config file " << configfile << ". Proceeding with defaults...\n";

		if(corpus_type_str == "xml")
			m_pConfig->m_CorpusType = XML;
		else
			m_pConfig->m_CorpusType = POLIQARP;
		boost::to_lower	(filter_disamb_str);
		if( filter_disamb_str == "true" )
			m_pConfig->m_bFilterDisamb = true;
		else
			m_pConfig->m_bFilterDisamb = false;
	}
}

