class UIControlTextInput extends UICommonAPI;

var EditBoxHandle inputTextBox;
var TextBoxHandle defaultTextBox;
var TextureHandle enableTex;
var TextureHandle disableTex;
var TextureHandle edtiableTex;
var ButtonHandle clearBtn;
var int _max;
var string _defaultString;
var string _string;
var bool _bDisable;
var bool _bEdtiable;
var private bool bFocused;
var L2UITimerObject timerObject;

function string GetString()
{
	return inputTextBox.GetString();
}

function SetString(string Str, optional bool bFormatString)
{
	_string = Str;
	if(bFormatString)
	{
		inputTextBox.SetFormatString(Str);		
	}
	else
	{
		inputTextBox.SetString(Str);
	}
	CheckDefaultString();
}

function AddEmojiIcon(int nEmojiIconIndex)
{
	inputTextBox.AddEmojiIcon(nEmojiIconIndex);
	_string = inputTextBox.GetString();
	CheckDefaultString();
}

function Clear()
{
	SetString("");
}

function SetHighLight(bool isHighlight)
{
	inputTextBox.SetHighLight(isHighlight);
}

function AllSelect()
{
	if(IsEdtiable())
	{
		inputTextBox.AllSelect();
		inputTextBox.SetFocus();
	}
}

function Focus()
{
	if(IsEdtiable())
	{
		inputTextBox.SetFocus();
	}
}

function bool IsEmpty()
{
	return inputTextBox.IsEmpty();
}

function SetDefaultString(string Str)
{
	defaultTextBox.SetText(Str);
	CheckDefaultString();
}

function SetMaxLength(int Max)
{
	inputTextBox.SetMaxLength(Max);
}

function SetDisable(bool bDisable)
{
	_bDisable = bDisable;
	if(bDisable)
	{
		disableTex.ShowWindow();
		enableTex.HideWindow();
		inputTextBox.ReleaseFocus();
		inputTextBox.DisableWindow();
		clearBtn.DisableWindow();		
	}
	else
	{
		disableTex.HideWindow();
		SetEdtiable(_bEdtiable);
	}
}

function SetEdtiable(bool bEdtiable)
{
	_bEdtiable = bEdtiable;
	if(_bDisable)
	{
		return;
	}
	if(bEdtiable)
	{
		inputTextBox.EnableWindow();
		enableTex.ShowWindow();
		edtiableTex.HideWindow();
		clearBtn.EnableWindow();		
	}
	else
	{
		enableTex.HideWindow();
		edtiableTex.ShowWindow();
		inputTextBox.ReleaseFocus();
		inputTextBox.DisableWindow();
		clearBtn.DisableWindow();
	}
}

function bool IsEdtiable()
{
	return _bEdtiable && ! _bDisable;
}

function UseClearBtn(bool bUse)
{
	local Rect wndRect;

	wndRect = m_hOwnerWnd.GetRect();

	if(bUse)
	{
		clearBtn.ShowWindow();
		inputTextBox.SetWindowSize(wndRect.nWidth, wndRect.nHeight - 16);
	}
	else
	{
		clearBtn.HideWindow();
		inputTextBox.SetWindowSize(wndRect.nWidth, wndRect.nHeight);
	}
}

delegate DelegateOnChangeEdited(string Text)
{}

delegate DelegateOnCompleteEditBox(string Text)
{}

delegate DelegateOnClear()
{}

delegate DelegateOnKeyUP(EInputKey nKey);

delegate DelegateESCKey()
{}

static function UIControlTextInput InitScript(WindowHandle wnd)
{
	local UIControlTextInput scr;

	wnd.SetScript("UIControlTextInput");
	scr = UIControlTextInput(wnd.GetScript());
	scr.InitWnd(wnd);
	return scr;
}

function InitWnd(WindowHandle wnd)
{
	m_hOwnerWnd = wnd;
	inputTextBox = GetEditBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".inputTextBox");
	defaultTextBox = GetTextBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".defaultTextBox");
	enableTex = GetTextureHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".enableTex");
	disableTex = GetTextureHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".disableTex");
	edtiableTex = GetTextureHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".edtiableTex");
	clearBtn = GetButtonHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".clearBtn");
	_bEdtiable = false;
	SetDisable(false);
	m_hOwnerWnd.ShowWindow();
	timerObject = class'L2UITimer'.static.Inst()._AddNewTimerObject(100, -1);
	timerObject._DelegateOnTime = OnTimerTester;
}

function OnTimerTester(int Time)
{
	if(m_hOwnerWnd.GetTopFrameWnd().IsShowWindow())
	{
		Debug("OnTimerTester" @ m_hOwnerWnd.GetTopFrameWnd().GetWindowName() @ string(inputTextBox.IsFocused()));
	}	
}

event OnChangeEditBox(string strID)
{
	switch(strID)
	{
		case "inputTextBox":
			DelegateOnChangeEdited(GetString());
			CheckDefaultString();
			break;
	}
}

event OnCompleteEditBox(string strID)
{
	DelegateOnCompleteEditBox(GetString());
}

event OnKeyUp(WindowHandle a_WindowHandle, EInputKey nKey)
{
	switch(nKey)
	{
		case IK_Escape:
			DelegateESCKey();
			if(GetString() != _string)
			{
				SetString(_string);
				return;
			}
			inputTextBox.ReleaseFocus();
			break;
	}
	DelegateOnKeyUP(nKey);	
}

event OnClickButton(string strID)
{
	if(! IsEdtiable())
	{
		return;
	}
	Clear();
	inputTextBox.SetFocus();
	DelegateOnClear();
}

event OnSetFocus(WindowHandle a_WindowHandle, bool focused)
{
	if(focused)
	{
		m_hOwnerWnd.GetTopFrameWnd().BringToFront();
	}
	if(a_WindowHandle == inputTextBox)
	{
		bFocused = focused;
	}
	CheckDefaultString();
}

private function CheckDefaultString()
{
	if(bFocused)
	{
		defaultTextBox.HideWindow();		
	}
	else if((IsEmpty()) == true)
	{
		defaultTextBox.ShowWindow();		
	}
	else
	{
		defaultTextBox.HideWindow();
	}
}

function _Hide()
{
	m_hOwnerWnd.HideWindow();	
}

function _Show()
{
	m_hOwnerWnd.ShowWindow();	
}

function bool _IsFocused()
{
	return bFocused;	
}
