#include "other.h"

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

#include "utf8utf16.h"

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


namespace OT
{

	std::vector<OtherWordRecord> OtherWordRecordArr;

	OtherWordRecord::OtherWordRecord()
	{
	}

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

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

		word = lineArr[1];
		type = lineArr[2];

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

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

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


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

		for (auto& wordRecord : OtherWordRecordArr)
		{
			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);
				OtherWordRecord otherWordRecord(wline);

				OtherWordRecordArr.push_back(otherWordRecord);

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


	}

}