#pragma once

#include <list>
#include "uiframe.h"

using namespace std;

// --------------------------------------------------------------------------------------
class CUiLuaEditBox
{
public:
	static	void			OpenLib(lua_State* L);
	static	int				SetMetaTable(lua_State* L);
};

// --------------------------------------------------------------------------------------
class CUiEditBox : public CUiFrame
{
public:
	CUiEditBox(const char* name, CUiLayout* pParent, CUi* pUi);
	virtual ~CUiEditBox();
	
	virtual void			SetMouseEnable(bool isEnable)			{ m_state.isMouseEnable = true; }	
	virtual void			SetKeyboardEnable(bool isEnable)		{ m_state.isKeyboardEnable = true; }
	virtual	void			SetFontString(CUiFontString* fontString);
	virtual	void			SetLetters(int letters);
	virtual	void			SetText(const WCHAR* text);
	virtual	void			SetTextA(const char* text);
	virtual	void			SetHistoryLines(int value);
	virtual	void			AddHistoryLine(const WCHAR* text);
	virtual	void			AddHistoryLineA(const char* text);
	virtual	const WCHAR*	GetHistoryLine(int line);
	virtual	const WCHAR*	GetText()								{ return m_text; }
	virtual	void			SetNumeric(bool isNumeric)				{ m_isNumeric = isNumeric; }
	virtual	bool			IsNumeric()								{ return m_isNumeric; }
	virtual	void			SetPassword(bool isPassword)			{ m_isPassword = isPassword; }
	virtual	bool			IsPassword()							{ return m_isPassword; }
	virtual	void			SetMultiMode(bool isMulti)				{ m_multiMode = isMulti; }
	virtual	bool			GetMultiMode()							{ return m_multiMode; }	
	virtual	void			SetHighlightColor(DWORD color)			{ m_blockColor = color; }
	virtual	DWORD			GetHighlightColor()						{ return m_blockColor; }
	virtual	int				GetTextSize()							{ return m_textSize; }
	virtual	int				GetLetters()							{ return m_letters; }	
	virtual void			SetTextInset(CUiRectangle& inset)		{ m_textInset = inset; }
	virtual	void			SetLuaMetaTable(lua_State* l)			{ CUiLuaEditBox::SetMetaTable(l); }
	virtual	void			SetTextColor(CUiColor& color);	

protected:
	virtual	void			PlaceCaret(int caretPos);
	virtual	void			PasteFromClipboard();
	virtual	void			CopyToClipboard();
	virtual	void			DeleteSelectionText();
	virtual	void			ResetCaretBlink();
	virtual	void			AdjustHistory();

	virtual	bool			InsertChar(int index, WCHAR ch);
	virtual	bool			RemoveChar(int index);
	virtual	int				InsertString(int index, const WCHAR *text, int size=-1);
	
	virtual	void			OnUpdate(float elapsedTime);
	virtual	void			OnPaint(float alpha);
	virtual	void			OnMouseDown(CUiPoint pt, int key);
	virtual	void			OnMouseMove(CUiPoint pt);
	virtual	void			OnChar(WPARAM wParam);
	virtual	void			OnPressed(WPARAM wParam);
	virtual	void			OnTextChanged();

protected:
	list<WCHAR*>			m_history;
	int						m_historyLast;
	int						m_historyLines;
	CUiFontString*			m_fontString;
	WCHAR*					m_text;
	CUiRectangle			m_textInset;				// 文字顯示區域	
	DWORD					m_blockColor;				// 選取文字背景顏色
	int 					m_textSize;
	int						m_letters;					// 輸入字元個數		
	bool					m_multiMode;				// 單行或多行模式
	bool					m_isNumeric;				// 只能輸入數字
	bool					m_isPassword;				// 密碼模式
	bool					m_isInsertMode;				// 插入或取代模式

	float					m_blinkSpeed;
	float					m_lastBlinkTime;
	int						m_firstVisible;
	int						m_selStart;
	int						m_caretPos;	
	bool					m_caretBool;	
};