#pragma once

namespace FancyScript {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	public ref class HighlightWordNode
{
public:	wchar_t data;
public: System::Drawing::Color color;
public: HighlightWordNode ^next,^child;
public:	HighlightWordNode(wchar_t c) {
				data = c;
				next = child = nullptr;
				color = System::Drawing::Color::Black;
			}
};

public ref class HighlightWord
{
private: HighlightWordNode ^root;
public:	HighlightWord()	{
			root = gcnew HighlightWordNode(' ');
		}
public: System::Void setColor(String ^word,System::Drawing::Color color) {
			int len=word->Length;
			if (!len) return;
			HighlightWordNode ^tmp=root;
			int i=0;
			while (i<len)
			{
				wchar_t c=word[i];
				if (!tmp->child)
				{
					tmp->child = gcnew HighlightWordNode(c);
					tmp = tmp->child;
				}
				else
				{
					tmp = tmp->child;
					while (tmp->next && tmp->data!=c)
						tmp = tmp->next;
					if (tmp->data!=c && !tmp->next)
					{
						tmp->next = gcnew HighlightWordNode(c);
						tmp = tmp->next;
					}
				}
				i++;
			}
			tmp->color = color;
		}
public: System::Drawing::Color getColor(String ^word) {
			int len=word->Length;
			if (len==0) return System::Drawing::Color::Black;
			HighlightWordNode ^tmp=root;
			int i=0;
			while (i<len)
			{
				wchar_t c=word[i];
				if (!tmp->child)
					return System::Drawing::Color::Black;
				else
				{
					tmp = tmp->child;
					while (tmp->next && tmp->data!=c)
						tmp = tmp->next;
					if (tmp->data!=c && !tmp->next)
						return System::Drawing::Color::Black;
				}
				i++;
			}
			return tmp->color;
		}
public: String ^getCompleteWord(String ^word) {
			int len=word->Length;
			if (len==0) return "";
			HighlightWordNode ^tmp=root;
			int i=0;
			while (i<len)
			{
				wchar_t c=word[i];
				if (!tmp->child)
					return "";
				else
				{
					tmp = tmp->child;
					while (tmp->next && tmp->data!=c)
						tmp = tmp->next;
					if (tmp->data!=c && !tmp->next)
						return "";
				}
				i++;
			}
			tmp = tmp->child;
			if (tmp)
			{
				String ^cur="";
				while (tmp->child)
				{
					cur += tmp->data.ToString();
					tmp = tmp->child;
				}
				cur += tmp->data.ToString();
				return cur;
			}
			else
				return "";
			

		}
};

}