class UIControlNumberInput extends UICommonAPI;

const DIALOG_ASK_PRICE = 10111;
const MAXITEMNUM = 99999;
const MAXITEMNUMSELL = 9999999999;

var string m_WindowName;
var WindowHandle Me;
var EditBoxHandle ItemCount_EditBox;
var ButtonHandle Reset_Btn;
var ButtonHandle Buy_Btn;
var ButtonHandle MultiSell_Up_Button;
var ButtonHandle MultiSell_Down_Button;
var ButtonHandle MultiSell_Input_Button;
var ButtonHandle MultiSell_Max_Button;
var private INT64 _minCountCanBuy;
var private bool is_force_disable;
var private bool is_Editor_disable;
var private bool bUseCalculator;
var private INT64 plusbasicunit;

delegate INT64 DelegateGetCountCanBuy();

delegate CustomTooltip DelegateGetBtnTooltip();

delegate DelegateESCKey();

delegate DelegateOnClickBuy();

delegate delegateOnItemCountEdited (INT64 changedNum);

delegate DelegateOnCancel();

delegate DelegateOnClickInput();

delegate DelegateOnOverInput();

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

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

function InitWnd(WindowHandle wnd)
{
	m_hOwnerWnd = wnd;
	Init(m_hOwnerWnd.m_WindowNameWithFullPath);
}

function Init(string WindowName)
{
	m_WindowName = WindowName;
	Me = GetWindowHandle(m_WindowName);
	ItemCount_EditBox = GetEditBoxHandle(m_WindowName $ ".ItemCount_EditBox");
	Reset_Btn = GetButtonHandle(m_WindowName $ ".Reset_Btn");
	Buy_Btn = GetButtonHandle(m_WindowName $ ".Buy_Btn");
	MultiSell_Up_Button = GetButtonHandle(m_WindowName $ ".MultiSell_Up_Button");
	MultiSell_Down_Button = GetButtonHandle(m_WindowName $ ".MultiSell_Down_Button");
	MultiSell_Max_Button = GetButtonHandle(m_WindowName $ ".MultiSell_Max_Button");
	MultiSell_Input_Button = GetButtonHandle(m_WindowName $ ".MultiSell_Input_Button");
	if(MultiSell_Input_Button.m_pTargetWnd != none)
	{
		MultiSell_Input_Button.HideWindow();
	}
	is_force_disable = false;
	_minCountCanBuy = 1;
	plusbasicunit = 1;	
}

event OnChangeEditBox(string strID)
{
	switch(strID)
	{
		case "ItemCount_EditBox":
			CheckZero();
			SetCount(int64(ItemCount_EditBox.GetString()));
			break;
	}
}

event OnClickButton(string Name)
{
	switch(Name)
	{
		case "MultiSell_Input_Button":
			OnPriceEditBtnHandler();
			break;
		case "MultiSell_Up_Button":
			OnMultiSell_Up_ButtonClick();
			break;
		case "MultiSell_Down_Button":
			OnMultiSell_Down_ButtonClick();
			break;
		case "Reset_Btn":
			SetCount(_minCountCanBuy);
			break;
		case "Buy_Btn":
			DelegateOnClickBuy();
			break;
		case "MultiSell_Max_Button":
			SetCount(6056184808285929474);
			break;
		case "Cancel_Btn":
			DelegateOnCancel();
			break;
	}
}

event OnKeyUp(WindowHandle a_WindowHandle, EInputKey nKey)
{
	local string mainKey;

	mainKey = class'InputAPI'.static.GetKeyString(nKey);
	if("ESCAPE" == mainKey)
	{
		DelegateESCKey();
	}
}

function CheckZero()
{
	local string EditBoxString;

	EditBoxString = ItemCount_EditBox.GetString();
	if((Left(EditBoxString, 1) == "0") && Len(EditBoxString) > 1)
	{
		ItemCount_EditBox.SetString(Right(EditBoxString, Len(EditBoxString) - 1));
	}
}

function SetCount(INT64 Num)
{
	if((DelegateGetCountCanBuy()) < Num)
	{
		DelegateOnOverInput();
	}
	Num = Min64(DelegateGetCountCanBuy(), Num);
	if(Num != int64(ItemCount_EditBox.GetString()))
	{
		ItemCount_EditBox.SetString(string(Num));
	}
	delegateOnItemCountEdited(Num);
	SetControlerBtns();
	SetBuyBtnTooltip();
}

function INT64 GetCount()
{
	return int64(ItemCount_EditBox.GetString());
}

function OnPriceEditBtnHandler()
{
	DelegateOnClickInput();
}

function OnMultiSell_Up_ButtonClick()
{
	SetCount(int64(ItemCount_EditBox.GetString()) + plusbasicunit);
}

function OnMultiSell_Down_ButtonClick()
{
	SetCount(int64(ItemCount_EditBox.GetString()) - plusbasicunit);
}

function SetBuyBtnTooltip()
{
	if(Buy_Btn.m_pTargetWnd != none)
	{
		Buy_Btn.SetTooltipCustomType(DelegateGetBtnTooltip());
	}
}

function SetControlerBtns()
{
	local INT64 Count, canBuyCount;

	if(is_force_disable == true)
	{
		if(MultiSell_Input_Button.m_pTargetWnd != none)
		{
			MultiSell_Input_Button.DisableWindow();
		}
		if(MultiSell_Max_Button.m_pTargetWnd != none)
		{
			MultiSell_Max_Button.DisableWindow();
		}
		MultiSell_Up_Button.DisableWindow();
		MultiSell_Down_Button.DisableWindow();
		ItemCount_EditBox.DisableWindow();
		return;
	}
	canBuyCount = DelegateGetCountCanBuy();
	Count = int64(ItemCount_EditBox.GetString());
	if(Reset_Btn.m_pTargetWnd != none)
	{
		if(Count != _minCountCanBuy && canBuyCount >= _minCountCanBuy)
		{
			Reset_Btn.EnableWindow();			
		}
		else
		{
			Reset_Btn.DisableWindow();
		}
	}
	if(MultiSell_Max_Button.m_pTargetWnd != none)
	{
		if((Count < canBuyCount) && canBuyCount >= _minCountCanBuy)
		{
			MultiSell_Max_Button.EnableWindow();			
		}
		else
		{
			MultiSell_Max_Button.DisableWindow();
		}
	}
	if(canBuyCount <= _minCountCanBuy)
	{
		ItemCount_EditBox.DisableWindow();		
	}
	else
	{
		if(! is_Editor_disable)
		{
			ItemCount_EditBox.EnableWindow();
		}
	}
	if(MultiSell_Input_Button.m_pTargetWnd != none)
	{
		if(canBuyCount <= _minCountCanBuy)
		{
			MultiSell_Input_Button.DisableWindow();			
		}
		else
		{
			MultiSell_Input_Button.EnableWindow();
		}
	}
	if(canBuyCount <= Count)
	{
		MultiSell_Up_Button.DisableWindow();		
	}
	else
	{
		MultiSell_Up_Button.EnableWindow();
	}
	if(Count <= _minCountCanBuy)
	{
		MultiSell_Down_Button.DisableWindow();		
	}
	else
	{
		MultiSell_Down_Button.EnableWindow();
	}
	if(Buy_Btn.m_pTargetWnd != none)
	{
		if(Count >= _minCountCanBuy)
		{
			Buy_Btn.EnableWindow();			
		}
		else
		{
			Buy_Btn.DisableWindow();
		}
	}
}

function _SetPlusBasicUnit(INT64 Unit)
{
	plusbasicunit = Unit;
}

function _SetForceDisable(bool isForceDisable)
{
	is_force_disable = isForceDisable;
	SetControlerBtns();
}

function bool _GetForceDisable()
{
	return is_force_disable;	
}

function _SetEditBoxDisable(bool IsDisable)
{
	local INT64 canBuyCount;

	is_Editor_disable = IsDisable;
	canBuyCount = DelegateGetCountCanBuy();
	if(canBuyCount <= _minCountCanBuy || is_force_disable || is_Editor_disable)
	{
		ItemCount_EditBox.DisableWindow();		
	}
	else
	{
		ItemCount_EditBox.EnableWindow();
	}	
}

function bool _GetEditBoxDisable()
{
	return is_Editor_disable;	
}

function _SetEditBoxFontColor(Color Color)
{
	ItemCount_EditBox.SetFontColor(Color);
}

function _SetUseCaculator(bool bUse)
{
	bUseCalculator = bUse;
	if(MultiSell_Input_Button.m_pTargetWnd == none)
	{
		return;
	}
	if(bUseCalculator)
	{
		MultiSell_Input_Button.ShowWindow();		
	}
	else
	{
		MultiSell_Input_Button.HideWindow();
	}
}

function _SetMinCountCanBuy(INT64 minCOunt)
{
	_minCountCanBuy = minCOunt;
}
