class HennaListWndLive extends UICommonAPI;

//////////////////////////////////////////////////////////////////////////////
// 라이브용 염료 리스트
//////////////////////////////////////////////////////////////////////////////
//const FEE_OFFSET_Y_EQUIP = -13;
//const FEE_OFFSET_Y_UNEQUIP = -12;
const FEE_OFFSET_Y_EQUIP = -23;
const FEE_OFFSET_Y_UNEQUIP = -21;

// 문양 새기기 윈도우의 상태
const HENNA_EQUIP=1;		// 문양새기기
const HENNA_UNEQUIP=2;		// 문양지우기

var string m_WindowName;
var WindowHandle Me;
var ButtonHandle btnHenna;

var RichListCtrlHandle listCtrl;

var int m_iState;
var int m_iRootNameLength;
var bool m_bDrawBg;

var L2Util util;

struct AddHennaStruct
{
	var String  Name ;
	var String  Description;
	var String  IconName ;
	var int     HennaID;
	var int     ClassID;	
	var int64   NumberOfItem ;
	var int64   Fee;
	// EV_HennaListWndAddHennaEquip 용
	var int		NeedCount;
	// EV_HennaListWndAddHennaUnEquip 용
	// 돌려 받는 값
	var int		CancelCount;
};

function OnRegisterEvent()
{
	RegisterEvent( EV_HennaListWndShowHideEquip );
	RegisterEvent( EV_HennaListWndAddHennaEquip );

	RegisterEvent( EV_HennaListWndShowHideUnEquip );
	RegisterEvent( EV_HennaListWndAddHennaUnEquip );

	RegisterEvent( EV_HennaListWndClose ); //branch GD35_0828 2014-5-23 luciper3 - 서버에서 특정상황에 문양 목록창을 닫기위해 -1을 보내면 목록창을 닫는다.
}

function OnLoad()
{
	SetClosingOnESC();
	
	m_bDrawBg = true;
	
	Me = GetWindowHandle ( m_WindowName ) ;

	btnHenna = GetButtonHandle( m_WindowName $ ".btnHenna") ;

	listCtrl = GetRichListCtrlHandle( m_WindowName $ ".HennaListLc");
	listCtrl.SetUseHorizontalScrollBar(true);
	// listCtrl.SetColumnMinimumWidth( true ) ;

	util = L2Util ( GetScript ( "L2Util" ));

	listCtrl.SetSelectedSelTooltip(false);	
	listCtrl.SetAppearTooltipAtMouseX(true);

	
}

// 트리 비우기
function Clear()
{
	listCtrl.DeleteAllItem();
	btnHenna.DisableWindow();
	//listCtrl.InitListCtrl();
	// class'UIAPI_TREECTRL'.static.Clear("HennaListWnd.HennaListTree");
}

function OnEvent(int Event_ID, string param)
{
	local INT64 iAdena;

	//Debug ( "HennaListWnd" @ Event_ID @ param ) ;

	if ( getInstanceUIData().getIsClassicServer() ) return;

	switch(Event_ID)
	{
	// 1640
	case EV_HennaListWndShowHideEquip :
		m_iState=HENNA_EQUIP;
		Clear();
		ParseINT64(param, "Adena", iAdena);
		ShowHennaListWnd(iAdena);
		break;
	case EV_HennaListWndAddHennaEquip :
	case EV_HennaListWndAddHennaUnEquip :
		AddHennaListItem ( param ) ;
		// AddHennaListItem(strName, strIconName, strDescription, iFee, iHennaID);
		break;
	case EV_HennaListWndShowHideUnEquip :
		m_iState=HENNA_UNEQUIP;
		Clear();
		ParseINT64(param, "Adena", iAdena);
		ShowHennaListWnd(iAdena);
		break;

	//branch GD35_0828 2014-5-23 luciper3 - 서버에서 특정상황에 문양 목록창을 닫기위해 -1을 보내면 목록창을 닫는다.
	case EV_HennaListWndClose :
		OnReceivedCloseUI();
		break;
	//end of branch
	}
}

function OnDBClickListCtrlRecord( string ListCtrlID )
{
	if(ListCtrlID == "HennaListLc")
	{
		 RequestSelectedHennaItemInfo();
	}
}

function OnClickListCtrlRecord( String strID )
{
	local int Idx;
	local RichListCtrlRowData rowData;	
	
	if( strID == "HennaListLc")
	{
		Idx = listCtrl.GetSelectedIndex();

		if (Idx <= -1)
		{
			btnHenna.DisableWindow();
			return;
		}
		
		listCtrl.GetRec( Idx, rowData );
		
		// 정보 창을 확인 할 수 있어야 하므로, 정보창에서 거른다.
		btnHenna.EnableWindow();
		//if ( rowData.nReserved3 == 1 ) 
		//	btnHenna.EnableWindow();
		//else 
		//	btnHenna.DisableWindow();
		

		// Debug ( "OnClickListCtrlRecord" @ Idx  @ rowData.nReserved3 ) ;
		// Debug("API CALL ---> RequestGoodsInventoryItemDesc(" @ Idx @ ")");

		//selectedProductListIndex = Idx;
	}
}

function OnClickButton( string Name )
{
	switch ( Name ) 
	{
		case "btnHenna": 
			// Debug ( "OnClickButton" @ name ) ;
			RequestSelectedHennaItemInfo ();
		break;
	}
}

function OnShow () 
{
	GetWindowHandle ( "HennaInfoWndLive" ).HideWindow() ;
}

// 선택 된 값 요청
function RequestSelectedHennaItemInfo () 
{
	local int Idx;
	local RichListCtrlRowData rowData;
	
	Idx = listCtrl.GetSelectedIndex();

	if (Idx <= -1) return;
	
	listCtrl.GetRec( Idx, rowData );
	 
	// Debug ( "RequestSelectedHennaItemInfo" @ m_iState );
	if(m_iState==HENNA_EQUIP)
	{	
		HennaInfoWndLive (GetScript("HennaInfoWndLive")).needCount = rowData.nReserved2 ;
		RequestHennaItemInfo( int( rowData.nReserved1 ));
	}
	else if(m_iState==HENNA_UNEQUIP)
		RequestHennaUnequipInfo( int( rowData.nReserved1)) ;
	
}

// 문양 새기기 윈도우로 초기화 시킴
function ShowHennaListWnd(INT64 iAdena)
{	
	if(m_iState==HENNA_EQUIP)		// 문양새기기 상태일경우
	{
		// 타이틀 변경
		setWindowTitleByString(GetSystemString(651));
		// 타이틀 아래 제목 변경 - 염료목록
		class'UIAPI_TEXTBOX'.static.SetText( m_WindowName $".txtList", GetSystemString(659));
		// 버튼 이름 변경 
		btnHenna.SetButtonName( 651 ) ;		
	}
	else if(m_iState==HENNA_UNEQUIP)	// 문양 지우기 상태일 경우
	{
		// 타이틀 - "문양지우기"
		setWindowTitleByString(GetSystemString(652));
		// 타이틀 아래 제목 변경 - "문양목록"
		class'UIAPI_TEXTBOX'.static.SetText( m_WindowName$".txtList", GetSystemString(660));
		// 버튼 이름 변경 
		btnHenna.SetButtonName( 652 ) ;
	}

	//소지 아데나 
	class'UIAPI_TEXTBOX'.static.SetText( m_WindowName$".txtAdena", MakeCostString(string(iAdena)));
	class'UIAPI_TEXTBOX'.static.SetTooltipString( m_WindowName$".txtAdena", ConvertNumToText(string(iAdena)));

	
	Me.ShowWindow();
	//ShowWindow("HennaListWnd");
	Me.SetFocus( );
}

function AddHennaStruct ParmaToStruct ( string param )  
{
	local AddHennaStruct hennaData ; 

	ParseString(param, "Name", hennaData.Name);				    // 이름
	ParseString(param, "Description", hennaData.Description);	// 
	ParseString(param, "IconName", hennaData.IconName);		    // IconName;
	ParseInt(param, "HennaID", hennaData.HennaID);				//
	ParseInt(param, "ClassID", hennaData.ClassID ) ;    		//
	ParseINT64(param, "NumberOfItem", hennaData.NumberOfItem );	// 수량
	ParseINT64(param, "Fee", hennaData.Fee);					// 요금

	if ( m_iState==HENNA_EQUIP ) 
		ParseInt(param, "NeedCount", hennaData.NeedCount);			// 필요 아이템 수량

	else if ( m_iState==HENNA_UNEQUIP ) 
		ParseInt(param, "CancelCount", hennaData.CancelCount);		// 회수 아이템 수량
	return hennaData ;
}

function String GetItemTooltipString ( int classID )  
{
	local string tooltipParam;
	local itemInfo Info;

	info = GetItemInfoByClassID ( classID ) ;
	ItemInfoToParam(info, tooltipParam);

//	Debug ( "GetItemTooltipString:" @ tooltipParam ) ;

	return tooltipParam ; 
}

function AddHennaListItem(string param)
{
	local ItemInfo iInfo;
	local AddHennaStruct hennaData;
	local RichListCtrlRowData rowData;
	local string strAdenaComma;
	local Color itemNumColor;
	local bool canBuy;
	local int gabTextY;
	local string Desc;

	rowData.cellDataList.Length = 2;

	hennaData = ParmaToStruct(param);	

	canBuy = ( hennaData.NeedCount <= hennaData.NumberOfItem ) ; 

	rowData.szReserved = GetItemTooltipString ( hennaData.ClassID ) ;
	rowData.nReserved1 = hennaData.HennaID;
	rowData.nReserved2 = hennaData.NeedCount;	
	if ( canBuy ) 
	{
		rowData.nReserved3 = 1;
	}
	else 
	{
		rowData.nReserved3 = 0;
	}
	class'UIDATA_ITEM'.static.GetItemInfo(GetItemID(hennaData.ClassID), iInfo);
	// 아이템 아이콘
	addRichListCtrlTexture(rowData.cellDataList[0].drawitems, "L2UI_ct1.ItemWindow.ItemWindow_df_slotbox_2x2", 36,36, 0, 0);	
	AddRichListCtrlItem(rowData.cellDataList[0].drawitems, iInfo, 32,32, -34, 2 );	

	// 지우기 부가 정보
	if(m_iState==HENNA_UNEQUIP)
	{
		if(GetLanguage() == LANG_English)
		{
			gabTextY = 3;			
		}
		else
		{
			gabTextY = 1;
		}
		// 아이템 이름 
		AddEllipsisString(rowData.cellDataList[0].drawitems, hennaData.Name, 236, util.BrightWhite, false, true, 5, -7);
		Desc = hennaData.Description;
		class'L2Util'.static.GetEllipsisString(Desc, 200);
		addRichListCtrlString(rowData.cellDataList[0].drawitems, Desc, util.BrightWhite, true, 45, gabTextY );
	}
	else
	{
		gabTextY = 5;
		AddEllipsisString(rowData.cellDataList[0].drawitems, hennaData.Name, 236, util.BrightWhite, false, true, 5, 0);
	}
	
	// 수수료 : 
	addRichListCtrlString( rowData.cellDataList[0].drawitems, GetSystemString ( 637 ) @ ":", GetColor( 163, 163, 163, 255 ) ,true ,45, gabTextY);

	// "제작비(아데나)"
	strAdenaComma = MakeCostStringINT64(hennaData.Fee);
	addRichListCtrlString( rowData.cellDataList[0].drawitems, strAdenaComma, GetNumericColor(strAdenaComma),false,5,0);
	addRichListCtrlString( rowData.cellDataList[0].drawitems, GetSystemString(469), util.Yellow ,false,5,0);
	
	/** 두번째 쎌 ***/ 
	if ( canBuy ) 
		itemNumColor = GetColor( 0, 176, 255, 255 );        // 넘음		
	else
		itemNumColor = GetColor( 255, 0, 0, 255 );          // 부족		

	if(m_iState==HENNA_UNEQUIP)
	{
		// 돌려 받는 수
		addRichListCtrlString( rowData.cellDataList[1].drawitems, GetSystemString(3386), util.White ,false,0,0);
		addRichListCtrlString( rowData.cellDataList[1].drawitems, String( hennaData.CancelCount ), itemNumColor ,true ,0,5) ;
	}
	else 
	{
		// 필요 수
		addRichListCtrlString( rowData.cellDataList[1].drawitems, GetSystemString(2380), util.White ,false,0,0);
		addRichListCtrlString( rowData.cellDataList[1].drawitems, "/"$ hennaData.NeedCount, util.White ,true, 0, 5) ;
		addRichListCtrlString( rowData.cellDataList[1].drawitems, String(hennaData.NumberOfItem), itemNumColor ,false ,0,0) ;
	}
	
	listCtrl.InsertRecord(rowData);
}


/** 칼라 를 리턴한다. */
function Color GetColor (int r, int g, int b, int a  )
{
	local Color tColor;
	tColor.R = r;
	tColor.G = g;
	tColor.B = b;	
	tColor.A = a;	

	return tColor;
}


/**
 * 윈도우 ESC 키로 닫기 처리 
 * "Esc" Key
 ***/
function OnReceivedCloseUI()
{
	PlayConsoleSound(IFST_WINDOW_CLOSE);
	Me.HideWindow();
}

defaultproperties
{
	m_WindowName="HennaListWndLive"
}
