#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()
		: availableForGenitive(false)
		, availableForDative(false)
		, availableForAccusative(false)
		, availableForInstrumental(false)
		, availableForPrepositional(false)
	{
	}

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

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

		word = lineArr[1];

		availableForGenitive = lineArr[2] == L"1" ? true : false;
		availableForDative = lineArr[3] == L"1" ? true : false;
		availableForAccusative = lineArr[4] == L"1" ? true : false;
		availableForInstrumental = lineArr[5] == L"1" ? true : false;
		availableForPrepositional = lineArr[6] == L"1" ? true : false;

		for (int i = 0; i < 3; i++)
		{
			TranslationUnit translationUnit;

			translationUnit.meaning = lineArr[7 + i * 3];
			translationUnit.comment = lineArr[7 + i * 3 + 1];
			translationUnit.example = lineArr[7 + i * 3 + 2];

			if (translationUnit.meaning != L"")
			{
				translationUnitArr.push_back(translationUnit);
			}
		}
		
	}


	std::set<PrepositionRecord> RecognizeWord(std::wstring word)
	{
		std::set<PrepositionRecord> result;

		for (auto& wordRecord : PrepositionRecordArr)
		{
			if (wordRecord.word == word)
			{
				result.insert(wordRecord);
			}
		}

		return result;
	}



	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;
		}


	}

}