#include "preposition.h"

#include <iostream> //Xperimental -- for debug only

#include "utf8utf16.h"

#include "boost/regex.hpp"
#include "boost/algorithm/string/regex.hpp"


namespace PP
{

	std::vector<PrepositionRecord> PrepositionRecordArr;

	PrepositionRecord::PrepositionRecord()
	{
		SetAllFields();
	}

	PrepositionRecord::PrepositionRecord(std::wstring line)
	{
		std::vector<std::wstring> lineArr;

		boost::split_regex(lineArr, line, boost::wregex(L"	"));

		SetWord(lineArr[1]);

		SetAllFields(lineArr);

		FillTranslationUnit(lineArr, 7);
				
	}

	bool PrepositionRecord::getAvailableForGenitive() const
	{
		return properties.get<bool>(L"availableForGenitive");
	}

	bool PrepositionRecord::getAvailableForDative() const
	{
		return properties.get<bool>(L"availableForDative");
	}

	bool PrepositionRecord::getAvailableForAccusative() const
	{
		return properties.get<bool>(L"availableForAccusative");
	}

	bool PrepositionRecord::getAvailableForInstrumental() const
	{
		return properties.get<bool>(L"availableForInstrumental");
	}

	bool PrepositionRecord::getAvailableForPrepositional() const
	{
		return properties.get<bool>(L"availableForPrepositional");
	}

	void PrepositionRecord::SetAllFields()
	{
		properties.put(L"availableForGenitive", L"");
		properties.put(L"availableForDative", L"");
		properties.put(L"availableForAccusative", L"");
		properties.put(L"availableForInstrumental", L"");
		properties.put(L"availableForPrepositional", L"");
	}

	void PrepositionRecord::SetAllFields(const std::vector<std::wstring>& lineArr)
	{
		properties.put(L"availableForGenitive", lineArr[2] == L"1" ? true : false);
		properties.put(L"availableForDative", lineArr[3] == L"1" ? true : false);
		properties.put(L"availableForAccusative", lineArr[4] == L"1" ? true : false);
		properties.put(L"availableForInstrumental", lineArr[5] == L"1" ? true : false);
		properties.put(L"availableForPrepositional", lineArr[6] == L"1" ? true : false);
	}

	/*
	boost::property_tree::wptree PrepositionModificator::GetModificators() const
	{
		return boost::property_tree::wptree();
	}*/



	PrepositionPair::PrepositionPair()
	{
	}

	PrepositionPair::PrepositionPair(PrepositionModificator iModificator, PrepositionRecord iWordRecord)
		: modificator(iModificator)
		, wordRecord(iWordRecord)
	{
	}

	const WordModificatorInterface& PrepositionPair::wordModificator() const
	{
		return modificator;
	}

	const WordHolder& PrepositionPair::word() const
	{
		return wordRecord;
	}

	std::wstring PrepositionPair::getType()
	{
		return L"preposition";
	}

	void RecognizeWord(std::wstring word, std::vector<std::shared_ptr<WordPairInterface>>& wordPair)
	{

		std::vector<std::shared_ptr<PrepositionPair>> result;

		for (auto& wordRecord : PrepositionRecordArr)
		{
			if (wordRecord.GetWord() == word)
			{

				wordPair.push_back(std::shared_ptr<WordPairInterface>(new PrepositionPair(PrepositionModificator(), wordRecord)));

			}
		}

	}



	void LoadWordSet(std::string filename)
	{


#ifdef _WIN32
		std::ifstream f("C:/Workplace/ChineseJournal/rudict/" + filename);

#else
		std::ifstream f("/home/devuser/workplace/rudict/" + filename);
#endif

		std::string line;
		std::wstring wline;

		if (f.is_open())
		{

			getline(f, line); //Skip one line

			std::cout << "File found!" << std::endl;
			while (getline(f, line))
			{

				wline = string_to_wstring(line);
				PrepositionRecord prepositionRecord(wline);

				PrepositionRecordArr.push_back(prepositionRecord);

			}
			f.close();
		}
		else
		{
			std::cout << "file not found!" << std::endl;
		}


	}

}