using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Collections::Generic;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Text;
using namespace System::Globalization;

namespace ObjDataEdit
{
	namespace CustomRichTextBox
	{
#pragma region Class CTextBlock
		/// <summary>
		/// Text block
		/// </summary>
		__gc class CTextBlock
		{
		public:
			static const int PB_NORMAL     = 0;
			static const int PB_TRANSLATED = 1;
			static const int PB_NEWLINE    = 2;

			// iType, 0 找不到, 1 任務 NPC Name, 2 ItemName, 3 ZoneName
		public:
			static const int PBT_KIND_NOT_FOUND = 0;
			static const int PBT_KIND_QUEST_NPC = 1;
			static const int PBT_KIND_ITEM = 2;
			static const int PBT_KIND_ZONE = 3;

		private:
			int mKind;
			int mPBTKind;
			String* mOriginalText;
			String* mTranslatedText;

		public:
			CTextBlock()
			{
				mKind = 0;
				mPBTKind = 0;
				mOriginalText = NULL;
				mTranslatedText = NULL;
			}

		public:
			__property int get_Kind() { return mKind; }
			__property void set_Kind(int val) { mKind = val; }

			__property int get_PBTKind() { return mPBTKind; }
			__property void set_PBTKind(int val) { mPBTKind = val; }

			__property String* get_OriginalText() { return mOriginalText; }
			__property void set_OriginalText(String* val) { mOriginalText = val; }

			__property String* get_TranslatedText() { return mTranslatedText; }
			__property void set_TranslatedText(String* val) { mTranslatedText = val; }

		};
#pragma endregion

#pragma region class CMirrorRichTextBox
		/// <summary>
		/// Text origin backup for custom textbox
		/// </summary>
		__gc class CMirrorRichTextBox : public System::Windows::Forms::RichTextBox
		{           
#pragma region Members
		public:
			static const int RETURN_SYMBOL_LEN = 1;  //Length of char '\n'
			//Members exposed via properties
		private:
			bool mCaseSesitive;
			bool mParsing;
			String* mTranslatedText;
			String* mStartToken;
			String* mCloseToken;
			List<CTextBlock*>* mBlockList;
			List<Color>* mColors;
			System::Windows::Forms::RichTextBox* mParent;

		public:
			CMirrorRichTextBox(void);

#pragma endregion

#pragma region Property
		public:
			__property String* get_TranslatedText() { return mTranslatedText; }

			__property bool get_CaseSensitive() { return mCaseSesitive; }
			__property void set_CaseSensitive(bool val) { mCaseSesitive = val; }

			__property String* get_StartToken() { return mStartToken; }
			__property void set_StartToken(String* val) { mStartToken = val; }

			__property String* get_CloseToken() { return mCloseToken; }
			__property void set_CloseToken(String* val) { mCloseToken = val; }

			__property System::Windows::Forms::RichTextBox* get_Parent() { return mParent; }
			__property void set_Parent(System::Windows::Forms::RichTextBox* val) { mParent = val; }
#pragma endregion

#pragma region Members function
		public:
			CTextBlock* AddPragraphBlock(int Kind, int PBTKind, String* Original, String* Translated);

		public:
			void WndProcCaller(Message* m);
			int GetCorrectSelectionStart();
			void AdjustCaretPos(int ParentSelectionStart, int ParentSelectionLength);
			void TransletText();
#pragma endregion

#pragma region Overriden methods
		protected:
			void OnTextChanged(EventArgs* e);
			void WndProc(Message* m);
#pragma endregion

#pragma region Rtf building helper functions

		private:
			//設定段落顏色字型
			void SetSettings(StringBuilder* sb, System::Drawing::Font* font, Color color, Hashtable* colors, Hashtable* fonts);

			//輸出Rtf設定顏色字串
			void SetColor(StringBuilder* sb, Color color, Hashtable* colors);

			//輸出Rtf設定背景顏色字串
			void SetBackColor(StringBuilder* sb, Color color, Hashtable* colors);

			//輸出Rtf設定字型字串
			void SetFont(StringBuilder* sb, System::Drawing::Font* font, Hashtable* fonts);

			//輸出Rtf設定字型大小
			void SetFontSize(StringBuilder* sb, int size);

			//輸出Rtf換行字串
			void AddNewLine(StringBuilder* sb);

			//輸出Rtf空白
			void EndTags(StringBuilder* sb);

			//輸出Rtf字型加入FontTable
			void AddFontToTable(StringBuilder* sb, System::Drawing::Font* font, int& counter, Hashtable* fonts);

			//輸出Rtf顏色加入ColorTable
			void AddColorToTable(StringBuilder* sb, Color color, int& counter, Hashtable* colors);

			//雙字元字符轉Rtf格式
			String* StrToRtf(String* str);

#pragma endregion
		};
#pragma endregion
	}
}