//
// request_handler.cpp
// ~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2014 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include "request_handler.hpp"
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include "mime_types.hpp"
#include "reply.hpp"
#include "request.hpp"

#include "boost/algorithm/string.hpp"
#include "boost/property_tree/json_parser.hpp"
#include "../utf8utf16.h"
#include "../noun.h"
#include "../adjective.h"
#include "../verb.h"
#include "../other.h"
#include "../preposition.h"

namespace http {
	namespace server {

		request_handler::request_handler()
		{
		}

		void request_handler::handle_request(const request& req, reply& rep)
		{
			// Decode url to path.
			std::string request_path;
			if (!url_decode(req.uri, request_path))
			{
				rep = reply::stock_reply(reply::bad_request);
				return;
			}

			// Request path must be absolute and not contain "..".
			if (request_path.empty() || request_path[0] != '/'
				|| request_path.find("..") != std::wstring::npos)
			{
				rep = reply::stock_reply(reply::bad_request);
				return;
			}

			// If path ends in slash (i.e. is a directory) then add "index.html".
			if (request_path[request_path.size() - 1] == '/')
			{
				request_path += "index.html";
			}

			//Prepare request
			if (request_path[0] == '/')
			{
				request_path = std::string(request_path.begin() + 1, request_path.end());
			}

			boost::to_lower(request_path);

			
			std::wstring requestedStr = string_to_wstring(request_path);
			/*
			requestedStr = L"Вы запросили: " + requestedStr;

			rep.content = UTF16to8(requestedStr.c_str());

			rep.content = "<html><body>" + rep.content + "</body></html>";
			*/

			boost::property_tree::wptree propertyTree = PrepareReport(requestedStr);

			std::wstringstream output_stream;

			boost::property_tree::write_json(output_stream, propertyTree);

			std::string outputJsonCode = wstring_to_string(output_stream.str());

			rep.status = reply::ok;

			rep.content = outputJsonCode;

			rep.headers.resize(2);
			rep.headers[0].name = "Content-Length";
			rep.headers[0].value = std::to_string(rep.content.size());
			rep.headers[1].name = "Content-Type";
			rep.headers[1].value = "application/json; charset=utf-8";
		}

		bool request_handler::url_decode(const std::string& in, std::string& out)
		{

			out.clear();
			out.reserve(in.size());

			for (std::size_t i = 0; i < in.size(); ++i)
			{
				if (in[i] == '%')
				{
					if (i + 3 <= in.size())
					{
						int value = 0;
						std::istringstream is(in.substr(i + 1, 2));
						if (is >> std::hex >> value)
						{
							out += static_cast<char>(value);
							i += 2;
						}
						else
						{
							return false;
						}
					}
					else
					{
						return false;
					}
				}
				else if (in[i] == '+')
				{
					out += ' ';
				}
				else
				{
					out += in[i];
				}
			}


			return true;
		}

		void RemoveDuplicates(std::vector<std::shared_ptr<WordPairInterface>>& wordPairSet)
		{
			size_t i = 0;

			while (i < wordPairSet.size())
			{
				size_t j = i + 1;

				while (j < wordPairSet.size())
				{
					if (*wordPairSet[i] == *wordPairSet[j])
					{
						wordPairSet.erase(wordPairSet.begin() + j);
						j--;
					}

					j++;
				}

				i++;
			}
		}


		boost::property_tree::wptree PrepareReport(std::wstring request)
		{
			boost::property_tree::wptree result;

			std::cout <<"PrepareReport" << std::endl;

			if (request.size() < 2)
			{
				result.put(L"error", L"String is too short");
			}

			int id = 0;

			std::vector<std::shared_ptr<WordPairInterface>> wordPairSet;
			
			NN::RecognizeNoun(request, wordPairSet);
			AJ::RecognizeAdjective(request, wordPairSet);
			VB::RecognizeVerb(request, wordPairSet);
			OT::RecognizeWord(request, wordPairSet);
			PP::RecognizeWord(request, wordPairSet);

			RemoveDuplicates(wordPairSet);
			
			boost::property_tree::wptree wordArr;

			for (auto& wordPair : wordPairSet)
			{
				boost::property_tree::wptree nounTree;

				nounTree.put(L"id", id);
				nounTree.put(L"type", wordPair->getType());
				nounTree.put(L"word", wordPair->word().GetWord());

				nounTree.put_child(L"properties", wordPair->word().GetProperties());

				nounTree.put_child(L"modificators", wordPair->wordModificator().GetModificators());
			
				nounTree.put_child(L"translations", wordPair->word().CreateTranslationPropertyTree());

				wordArr.push_back(std::make_pair(L"", nounTree));
		
				id++;
			}

			result.put_child(L"words", wordArr);



			return result;

		}
		
	} // namespace server
} // namespace http
