/*
    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 "wbst_headers.h"
#include "question.h"


WBSTQuestion::WBSTQuestion(const std::string &q, const std::string &a, const std::set<std::string> &decoys)
        : m_sQuestion(q),
        m_sCorrectAnwser(a),
        m_setDecoys(decoys)
{
}

WBSTQuestion::WBSTQuestion(const std::string &line)
{
	ParseLine(line);
}
WBSTQuestion& WBSTQuestion::operator=(const WBSTQuestion&q)
                                     {
	m_sQuestion = q.m_sQuestion;
	m_sCorrectAnwser = q.m_sCorrectAnwser;
	m_setDecoys = q.m_setDecoys;
	return *this;
}
WBSTQuestion::WBSTQuestion(const WBSTQuestion &q)
{
	m_sQuestion = q.m_sQuestion;
	m_sCorrectAnwser = q.m_sCorrectAnwser;
	m_setDecoys = q.m_setDecoys;
}

void WBSTQuestion::ParseLine(const std::string &line)
{
	using namespace std;
	vector<string> tokens;
	boost::split(tokens, line, boost::is_any_of(";") );
	if( tokens.size() < 3 ) // need at least question, correct anwser and one decoy
		throw std::runtime_error("Wrong wbst line: " + line);
	std::string temp;
	temp = tokens[0];
	boost::trim(temp);
	if( temp.length() == 0 )
		throw std::runtime_error("Wrong wbst line: " + line);
	m_sQuestion = temp;
	temp = tokens[1];
	boost::trim(temp);
	if( temp.length() == 0 )
		throw std::runtime_error("Wrong wbst line: " + line);
	m_sCorrectAnwser = temp;
	for(size_t i = 2; i < tokens.size(); i++)
	{
		temp = tokens[i];
		boost::trim(temp);
		if( temp.length() == 0 )
			throw std::runtime_error("Wrong wbst line: " + line);
		m_setDecoys.insert( temp );
	}

	if(m_setDecoys.find(m_sCorrectAnwser) == m_setDecoys.end() )
		throw std::runtime_error( "No correct anwser in decoys in: " + line );
}

const std::string& WBSTQuestion::GetQuestion() const
{
	return m_sQuestion;
}
const std::string& WBSTQuestion::GetAnwser() const
{
	return m_sCorrectAnwser;
}
const std::set<std::string>& WBSTQuestion::GetDecoys() const
{
	return m_setDecoys;
}

std::string WBSTQuestion::ToString() const
{
	std::stringstream str;
	str << m_sQuestion << ";" << m_sCorrectAnwser << ";";
	std::copy(m_setDecoys.begin(), m_setDecoys.end(), std::ostream_iterator<std::string>(str, ";") );
	std::string ret = str.str();
	return ret.substr(0, ret.length()-1);
}
