/**
 * 
 *  - 자동 플레이 시스템 -
 *  
 *   # 소모품 자동 사용 시스템
 *   # 자동 타겟, 자동 줍기(서버 쪽에서 하도록 기능 변경됨)
 *  
 *  소모품 자동 사용
 *  http://l2-redmine/redmine/issues/7368
 *  
 *  자동 플레이(자동 타겟)
 *  http://l2-redmine/redmine/issues/7691
 * 
 **/
class AutoUseItemWnd extends UICommonAPI;

// 아이템 자동 사용 슬롯의 ID는 144번(12 x 12)부터 155 까지 슬롯 수(12개)만큼 부여.
// 슬롯 ID 부여는 uc에서 창이 생성 된 후 1번 진행. UpdateShortcut 함수를 호출.
// 자동 사냥용 매크로 슬롯은 AutoplayMacroSlotID == 156

//-----------------------------------------------------
// 공용
//-----------------------------------------------------
var WindowHandle Me;
var AutoUseItemWndMin AutoUseItemWndMinScript;
var YetiQuickSlotWnd YetiQuickSlotwndScript;


// 최소화 상태, 옵션 저장용
var int nMinimal;

//-----------------------------------------------------
// 소모품 자동 사용 
//-----------------------------------------------------
const MAX_ShortcutPerPage     = 12; // 페이지 당 숏컷 슬롯 수
const AUTO_ITEM_SHORTCUT_PAGE = 22; // 소모품 자동 사용 슬롯 Page

var AnimTextureHandle ToggleEffect_Anim;
var AutoUseItemInventory AutoUseItemInventoryScript;

// 각 숏컷의 활성화 상태 저장
var UIMapInt64Object ActiveSlotArrayMap;
var UIMapInt64Object ItemInfoSlotArrayMap;

// 하나라도 활성화 여부
var bool bActivateAll;

//-----------------------------------------------------
//  자동 타겟 
//-----------------------------------------------------

// 가로 활성화 애니
var AnimTextureHandle AutoTarget_ToggleMacro_Anim;

var ButtonHandle shotD_Target_BTN, Next_Target_BTN;
var ButtonHandle TargetPickupToggle_BTN;
var ButtonHandle TargetMannerToggle_BTN;

// 타겟 
var WindowHandle TargetStatusWndScript;
var WindowHandle AutoTargetAllON_Win;

var ButtonHandle MacroSelectBtn_01;
var ButtonHandle MacroSelectBtn_02;

var AnimTextureHandle AutoTargetAllON_ToggleEffect_Anim;

// 숏타겟, 롱타겟 상태인지 체크 할때 
var bool autotarget_bShortTarget;

// 오토타겟 활성화, 비활성화 
var bool autotarget_bUseAutoTarget;

// 자동 줍기 
var bool autotarget_bIsPickupOn;

// 타겟 모드(옵션에서 지정)
var EAutoNextTargetMode autotarget_nTargetMode;

var int autotarget_nHPPotionPercent;
var bool autotarget_bIsMannerModeOn;
var int nMacroSlotSelect;

//-----------------------------------------------------
event OnRegisterEvent()
{
	RegisterEvent(EV_GameStart);
	RegisterEvent(EV_Restart);
	RegisterEvent(EV_GamingStateEnter);
	// 소모품 자동 사용
	RegisterEvent(EV_ShortcutAutomaticUseActivated);
	RegisterEvent(EV_ShortcutUpdate);
	RegisterEvent(EV_ShortcutClear);

	// 오토 플레이(자동 타겟등)
	RegisterEvent(	EV_AutoplaySetting);

	RegisterEvent(	EV_NextTargetModeChange);
	RegisterEvent(	EV_OptionHasApplied);

	RegisterEvent(EV_CounterAttack);

	//requestAutoPlay
	RegisterEvent(EV_UpdatePlayerAutoAttacking);
}

event OnLoad()
{
	Initialize();
}

function Initialize()
{
	Me = GetWindowHandle("AutoUseItemWnd");

	// 소모품 자동 사용
	ToggleEffect_Anim = GetAnimTextureHandle("AutoUseItemWnd.AutoAllON_Win.ToggleEffect_Anim");
	AutoUseItemInventoryScript = AutoUseItemInventory(GetScript("AutoUseItemInventory"));
	AutoUseItemWndMinScript = AutoUseItemWndMin(GetScript("AutoUseItemWndMin"));
	YetiQuickSlotwndScript = YetiQuickSlotWnd(GetScript("YetiQuickSlotwnd"));
	AutoTarget_ToggleMacro_Anim = GetAnimTextureHandle("AutoUseItemWnd.AutoTargetWnd.ToggleMacro_Anim");
	// 자동 타겟
	shotD_Target_BTN = GetButtonHandle("AutoUseItemWnd.AutoTargetWnd.shotD_Target_BTN");
	Next_Target_BTN = GetButtonHandle("AutoUseItemWnd.AutoTargetWnd.Next_Target_BTN");
	MacroSelectBtn_01 = GetButtonHandle("AutoUseItemWnd.AutoTargetWnd.MacroSelectBtn_01");
	MacroSelectBtn_02 = GetButtonHandle("AutoUseItemWnd.AutoTargetWnd.MacroSelectBtn_02");
	TargetPickupToggle_BTN = GetButtonHandle("AutoUseItemWnd.AutoTargetWnd.TargetPickupToggle_BTN");
	TargetMannerToggle_BTN = GetButtonHandle("AutoUseItemWnd.AutoTargetWnd.TargetMannerToggle_BTN");
	TargetStatusWndScript = GetWindowHandle("TargetStatusWnd");
	AutoTargetAllON_Win = GetWindowHandle("AutoUseItemWnd.AutoTargetWnd.AutoTargetAllON_Win");
	AutoTargetAllON_ToggleEffect_Anim = GetAnimTextureHandle("AutoUseItemWnd.AutoTargetWnd.AutoTargetAllON_Win.ToggleEffect_Anim");
	ActiveSlotArrayMap = new class'UIMapInt64Object';
	ItemInfoSlotArrayMap = new class'UIMapInt64Object';	
}

event OnShow()
{
	// 소모품 자동 사용
	if(getInstanceUIData().getIsClassicServer())
	{
		Me.HideWindow();
		return;
	}
	setPlayActiveAnim();

	// 자동 타겟
	AutotargetOnShow();
	Autotarget_UpdateAutoTargetState();
	Autotarget_SetCusomTooltip();

	// 자동 줍기
	Autotarget_updatePickupButton();
	Autotarget_PickupSetCusomTooltip();

	Autotarget_updateMannerModeButton();
	Autotarget_MannerModeSetCusomTooltip();
}

function initAll()
{
	// 소모품 자동 사용
	initSlotArray();
	HandleShortcutPageUpdateAll();
	bActivateAll = false;
	YetiQuickSlotwndScript.setPlayAutoTargetActiveAnim();
	checkMinimal();	
}

function checkMinimal()
{
	Me.ShowWindow();
	// 최소화 상태 로드
	GetINIBool("AutoUseItemWnd", "l", nMinimal, "windowsInfo.ini");
	if(nMinimal > 0)
	{
		OnClickButton("WinMin_Button");
	}	
}

function showHideForYeti(bool bShow)
{
	if(bShow)
	{
		if(nMinimal > 0)
		{
			OnClickButton("WinMin_Button");
		}
		else
		{
			Me.ShowWindow();
		}
	}
	else
	{
		Me.HideWindow();
		GetWindowHandle("AutoUseItemWndMin").HideWindow();
	}
}

/**
 * OnEvent 
 **/
event OnEvent(int Event_ID, string param)
{	
	// local int nSystemMsgIndex;
	if(getInstanceUIData().getIsClassicServer())
	{
		return;
	}
	switch(Event_ID)
	{
		case EV_GameStart:
			 initAll();
			 break;
		case EV_GamingStateEnter:
			checkMinimal();
			break;
	    case EV_Restart:
			 // 자동 아이템 사용 
			 initSlotArray();
			 bActivateAll = false;
			 nMinimal = 0;

			 // 자동 타겟
			 Autotarget_Init();
			 break;
		case EV_ShortcutAutomaticUseActivated:
			 //Debug("EV_ShortcutAutomaticUseActivated:" @ param);
			ShortcutAutomaticUseActivatedHandler(param);

			YetiQuickSlotwndScript.setPlayAutoTargetActiveAnim();
			 break;

		case EV_ShortcutUpdate:
			 //Debug("EV_ShortcutUpdate:" @ param);
			  // 자동 아이템 사용
			 HandleShortcutUpdate(param);

			 // 자동 타겟
			 //Autotarget_HandleShortcutUpdate(param);
			 break;

		case EV_ShortcutClear:
			 //Debug("EV_ShortcutClear:" @ param);
			 // 자동 아이템 사용
			 initAll();

			 // 자동 타겟
			 Autotarget_Init();  Autotarget_UpdateShortCutElement();
			 break; 

		//------------- 자동 타겟 ----------------------------------
		case EV_AutoplaySetting :
			 Debug("EV_AutoplaySetting"  @ param);	 
			 AutoplaySettingHandler(param);
			 break;

	   //  CTRL + T, 옵션에서 적용 눌렀을때(타겟 모드 변경)
	   case  EV_NextTargetModeChange :  
	   case  EV_OptionHasApplied :      NextTargetModeHandler(); break; 

	   // 반격 버튼이나 액션이 실행되면, 자동사냥 정지.
	   case  EV_CounterAttack: 
			 if(autotarget_bUseAutoTarget)requestAutoPlay(false); 
			 break;
		case EV_UpdatePlayerAutoAttacking:
			HandleUpdatePlayerAutoAttacking();
			break;

	}
}

//  CTRL + T, 옵션에서 적용 눌렀을때(타겟 모드 변경)
function NextTargetModeHandler()
{
	local EAutoNextTargetMode nTargetMode;

	nTargetMode = GetNextTargetModeOption();

	// 값이 같으면 다시 보내줄 필요가 없음.
	if(autotarget_nTargetMode != nTargetMode)
	{
		if(autotarget_bUseAutoTarget)requestAutoPlay(autotarget_bUseAutoTarget);
	}
	Autotarget_NextTargetSetCusomTooltip();	
}

// 자동 타겟 이벤트 처리
function AutoplaySettingHandler(string param)
{
	local int nIsAutoPlayOn, nNextTargetMode, nIsNearTargetMode, nIsPickupOn, nHPPotionPercent, nIsMannerModeOn,
		nMacroIndex;

	ParseInt(param, "IsPickupOn", nIsPickupOn);
	ParseInt(param, "IsAutoPlayOn", nIsAutoPlayOn);
	ParseInt(param, "NextTargetMode", nNextTargetMode);
	ParseInt(param, "IsNearTargetMode", nIsNearTargetMode);
	ParseInt(param, "HPPotionPercent", nHPPotionPercent);
	ParseInt(param, "IsMannerModeOn", nIsMannerModeOn);
	ParseInt(param, "MacroIndex", nMacroIndex);

	// 서버에서 on, off 한 값을 받아서 상태를 교체 ui에서 직접 하지는 않는다. 
	autotarget_bUseAutoTarget = numToBool(nIsAutoPlayOn);
	autotarget_bShortTarget = numToBool(nIsNearTargetMode);
	autotarget_bIsPickupOn = numToBool(nIsPickupOn);
	autotarget_bIsMannerModeOn = numToBool(nIsMannerModeOn);
	autotarget_nHPPotionPercent = nHPPotionPercent;
	nMacroSlotSelect = nMacroIndex;

	// 값에 따른 상태와 모션 처리
	Autotarget_UpdateAutoTargetState();	
}

function HandleShortcutClear(string param)
{
	local int nShortcutID;
	
	ParseInt(param, "ShortcutID", nShortcutID);

	if(nShortcutID >=(AUTO_ITEM_SHORTCUT_PAGE * MAX_ShortcutPerPage)&& nShortcutID <=((AUTO_ITEM_SHORTCUT_PAGE * MAX_ShortcutPerPage)+ MAX_ShortcutPerPage))
		class'UIAPI_SHORTCUTITEMWINDOW'.static.Clear("AutoUseItemWnd.ItemGroup_Wnd" $ ".AutoUseItem" $(nShortcutID -(AUTO_ITEM_SHORTCUT_PAGE * MAX_ShortcutPerPage)+ 1)$ "_ShortcutItem");
	else if(nShortcutID >= 288 && nShortcutID <= 293)
	{
		class'UIAPI_SHORTCUTITEMWINDOW'.static.Clear("AutoUseItemWnd.ItemGroup_Wnd" $ ".AutoUseItem" $ string(nShortcutID - 275) $ "_ShortcutItem");
	}
}

function HandleShortcutUpdate(string param)
{
	local int nShortcutID, nClassID;
	
	ParseInt(param, "ShortcutID", nShortcutID);
	ParseInt(param, "ClassID", nClassID);

	// 144 ~ 155 번 까지 
	if(nShortcutID >=(AUTO_ITEM_SHORTCUT_PAGE * MAX_ShortcutPerPage)&& nShortcutID <=((AUTO_ITEM_SHORTCUT_PAGE * MAX_ShortcutPerPage)+ MAX_ShortcutPerPage)- 1 || nShortcutID >= 288 && nShortcutID <= 293)
	{
		if(nClassID <= -1)
		{
			ItemInfoSlotArrayMap.Add(nShortcutID, 0);			
		}
		else
		{
			ItemInfoSlotArrayMap.Add(nShortcutID, nClassID);
		}
		if(nShortcutID >= 288 && nShortcutID <= 293)
		{
			class'UIAPI_SHORTCUTITEMWINDOW'.static.UpdateShortcut("AutoUseItemWnd.ItemGroup_Wnd" $ ".AutoUseItem" $ string(nShortcutID - 275) $ "_ShortcutItem", nShortcutID);			
		}
		else
		{
			class'UIAPI_SHORTCUTITEMWINDOW'.static.UpdateShortcut("AutoUseItemWnd.ItemGroup_Wnd" $ ".AutoUseItem" $ string((nShortcutID - (AUTO_ITEM_SHORTCUT_PAGE * MAX_ShortcutPerPage)) + 1) $ "_ShortcutItem", nShortcutID);
		}
		// 중간에 다 사용해서 0이 되면 ClassID가 0으로 들어 오고 숏컷은 자동 삭제 된다. 이때 비활성화 설정으로 배열 값도 교체 
		if(nClassID <= 0)
		{
			ActiveSlotArrayMap.Add(nShortcutID, 0);
		}

		setCheckActivateAll();
		setPlayActiveAnim();
		YetiQuickSlotwndScript.setPlayAutoTargetActiveAnim();
	}
	else if(nShortcutID == AutoplayMacroSlotID)
	{
		class'UIAPI_SHORTCUTITEMWINDOW'.static.UpdateShortcut("AutoUseItemWnd.AutoTargetWnd.Macro1ShortcutItem", AutoplayMacroSlotID);
	}
	else if(nShortcutID == AutoplayMacroSlotID2)
	{
		class'UIAPI_SHORTCUTITEMWINDOW'.static.UpdateShortcut("AutoUseItemWnd.AutoTargetWnd.Macro2ShortcutItem", AutoplayMacroSlotID2);
	}

	if(autotarget_bUseAutoTarget && (nShortcutID == AutoplayMacroSlotID || nShortcutID == AutoplayMacroSlotID2))
	{
		requestAutoPlay(false);
	}
}	

// 각 숏컷의 상태를 액티브 상태를 초기화
function initSlotArray()
{
	local int i;

	ActiveSlotArrayMap.RemoveAll();
	ItemInfoSlotArrayMap.RemoveAll();

	for(i = 0; i < MAX_ShortcutPerPage; i++)
	{
		ItemInfoSlotArrayMap.Add(264 + i, 0);
	}
	
	for(i = 0; i < 6;i++)
	{
		ItemInfoSlotArrayMap.Add(288 + i, 0);
	}
}

// 숏컷의 활성화 상태를 서버에서 받아서 상황에 따라 처리 
function ShortcutAutomaticUseActivatedHandler(string param)
{
	local int nShortcutID, nAutomaticUseActivated;

    //"ShortcutID" : int(숏컷 슬롯 ID)
    //"AutomaticUseActivated" : int(자동 사용 0:비활성화 / 1:활성화)
	
	ParseInt(param, "ShortcutID", nShortcutID);
	ParseInt(param, "AutomaticUseActivated", nAutomaticUseActivated);

	// 144 ~ 부터 155 까지는 허용
	if(nShortcutID >=(AUTO_ITEM_SHORTCUT_PAGE * MAX_ShortcutPerPage)&& nShortcutID <=((AUTO_ITEM_SHORTCUT_PAGE * MAX_ShortcutPerPage)+ MAX_ShortcutPerPage) || nShortcutID >= 288 && nShortcutID <= 293)
	{ 
		// 배열 슬롯에 활성화 여부를 넣는다. 
		ActiveSlotArrayMap.Add(nShortcutID, nAutomaticUseActivated);

		// 슬롯 액티브 체크
		setCheckActivateAll();		

		// 액티브 모션 
		setPlayActiveAnim();
		AutoUseItemWndMinScript.setPlayActiveAnim();

		YetiQuickSlotwndScript.setPlayAutoTargetActiveAnim();
		//Debug("nShortcutID:" @ nShortcutID);
		//Debug("index" @ nShortcutID -(AUTO_ITEM_SHORTCUT_PAGE * MAX_ShortcutPerPage));
		//Debug("nAutomaticUseActivated" @ nAutomaticUseActivated);
	}
}

function int getEmptySlotNum()
{
	return int(ItemInfoSlotArrayMap.FindKeyByData(0));
}

// 현재 슬롯에 활성화 된게 하나라도 있다면 ActiveAll = true;
function setCheckActivateAll()
{
	local int i;
	local array<INT64> arr;

	arr = ActiveSlotArrayMap.ContainAll();
	for(i = 0; i < arr.Length; i++)
	{
		//debug("arr[i]" @ arr[i]);
		if(arr[i] > 0)
		{
			bActivateAll = true;
			return;
		}
	}

	bActivateAll = false;	
}

// 전체 활성화 버튼위에 활성화 애니를 재생하고 정지
function setPlayActiveAnim()
{
	if(!Me.IsShowWindow())return;

	if(bActivateAll)
	{
		ToggleEffect_Anim.ShowWindow();
		ToggleEffect_Anim.SetLoopCount(99999);
		ToggleEffect_Anim.Stop();
		ToggleEffect_Anim.Play();

		GetWindowHandle("AutoUseItemWnd.AutoAllON_Win").ShowWindow();
		GetWindowHandle("AutoUseItemWnd.AutoAllOFF_Win").HideWindow();
	}
	else
	{
		ToggleEffect_Anim.HideWindow();
		ToggleEffect_Anim.Stop();

		GetWindowHandle("AutoUseItemWnd.AutoAllON_Win").HideWindow();
		GetWindowHandle("AutoUseItemWnd.AutoAllOFF_Win").ShowWindow();
	}
}

function bool getActivateAll()
{
	return bActivateAll;
}

// 숏컷 번호 등록 
function HandleShortcutPageUpdateAll()
{
	local int i;
	local int nShortcutID;
	
	nShortcutID =(AUTO_ITEM_SHORTCUT_PAGE * MAX_ShortcutPerPage);
	for(i = 1; i <= MAX_ShortcutPerPage; ++i)
	{
		class'UIAPI_SHORTCUTITEMWINDOW'.static.UpdateShortcut("AutoUseItemWnd.ItemGroup_Wnd" $ ".AutoUseItem" $(i)$ "_ShortcutItem", nShortcutID);
		nShortcutID++;
		// Debug("숏컷 컨트롤 업데이트 " @ GetWindowHandle("AutoUseItemWnd.ItemGroup_Wnd" $ ".AutoUseItem" $(i )$ "_ShortcutItem").GetWindowName()@ ": " @ nShortcutID);
	}
	
	nShortcutID = 275;

	for(i = 13; i <= 18; i++)
	{
		class'UIAPI_SHORTCUTITEMWINDOW'.static.UpdateShortcut("AutoUseItemWnd.ItemGroup_Wnd" $ ".AutoUseItem" $ string(i) $ "_ShortcutItem", nShortcutID + i);
	}
}

function OnClickButton(string name)
{
	// Debug("onClickButton:" @name);
	switch(name)
	{
		// 보조 인벤토리
		case "Inventory_Button":
			 onInventory_ButtonClick();
			 break;
		// 자동 사용
		case "AutoAll_BTN":			
			class'ShortcutWndAPI'.static.RequestAutomaticUseItemActivateAll(!bActivateAll);
			break;
			
		// 최소화 버튼
		case "WinMin_Button":
			nMinimal = 1;
			SetInIBool("AutoUseItemWnd", "l", numToBool(nMinimal), "windowsInfo.ini");
			Me.HideWindow();
			ShowWindowWithFocus("AutoUseItemWndMin");
			getInstanceL2Util().syncWindowLoc("AutoUseItemWnd", "AutoUseItemWndMin", 115, 98);
			getInstanceL2Util().fixWindowLocOverResolution("AutoUseItemWndMin");
			break;

		//----------------  자동 타겟 ---------------------
		// 매크로 창 열기 
		case "MacroWnd_Button":
			 ExecuteEvent(EV_MacroShowListWnd);
			 break;

		// 타겟 실행 
		case "ShotD_Target_BTN":
			Autotarget_OnSwap_Target_BTNClick();
			if(autotarget_bUseAutoTarget)
			{
				requestAutoPlay(autotarget_bUseAutoTarget);
			}
			break;
		// 줍기, on off
		case "TargetPickupToggle_BTN":
			Autotarget_TargetPickupToggle_BTNClick();
			if(autotarget_bUseAutoTarget)
			{
				requestAutoPlay(autotarget_bUseAutoTarget);
			}
			break;
		case "TargetMannerToggle_BTN":
			Autotarget_TargetMannerToggle_BTNClick();
			if(autotarget_bUseAutoTarget)
			{
				requestAutoPlay(autotarget_bUseAutoTarget);
			}
			break;
		// 전체 자동타겟 온오프 버튼
		case "AutoTargetAll_BTN":
			requestAutoPlay(! autotarget_bUseAutoTarget);
			break;
		case "Next_Target_BTN":
			Autotarget_OnNext_Target_BTNClick();
			break;
		case "MacroSelectBtn_01":
		case "MacroSelectBtn_02":
			MacroSelectBtn_Click(Name);
			SetMacroSlotSelect();
			if(Name == "MacroSelectBtn_01" || Name == "MacroSelectBtn_02")
			{
				Debug("-_-");
				requestAutoPlay(false);
			}
			break;
	}
}

function MacroSelectBtn_Click(string Name)
{
	if(Name == "MacroSelectBtn_01")
	{
		GetMeWindow("AutoTargetWnd.Macro1ShortcutItem").ShowWindow();
		GetMeWindow("AutoTargetWnd.Macro2ShortcutItem").HideWindow();
		SetINIInt("AutoUseItemWnd", "v", 0, "windowsInfo.ini");
		nMacroSlotSelect = 0;		
	}
	else
	{
		GetMeWindow("AutoTargetWnd.Macro1ShortcutItem").HideWindow();
		GetMeWindow("AutoTargetWnd.Macro2ShortcutItem").ShowWindow();
		SetINIInt("AutoUseItemWnd", "v", 1, "windowsInfo.ini");
		nMacroSlotSelect = 1;
	}	
}

function Autotarget_OnNext_Target_BTNClick()
{
	local string strParam;
	local int targetMode;

	strParam = "";
	switch(GetOptionInt("CommunIcation", "NextTargetMode"))
	{
		case 0:
			targetMode = 1;
			break;
		case 1:
			targetMode = 2;
			break;
		case 2:
			targetMode = 3;
			break;
		case 3:
			targetMode = 0;
			break;
	}
	SetOptionInt("Communication", "NextTargetMode", targetMode);
	ParamAdd(strParam, "NextTargetMode", string(targetMode));
	ExecuteEvent(11030, strParam);
	Autotarget_NextTargetSetCusomTooltip();	
}

function Autotarget_NextTargetSetCusomTooltip()
{
	local int N;
	local Color b0, b1, b2, b3;
	local array<DrawItemInfo> drawListArr;
	local string toolString;

	b0 = getInstanceL2Util().Gray;
	b1 = getInstanceL2Util().Gray;
	b2 = getInstanceL2Util().Gray;
	b3 = getInstanceL2Util().Gray;
	Next_Target_BTN.ClearTooltip();
	Next_Target_BTN.SetTooltipType("text");
	N = GetOptionInt("Communication", "NextTargetMode");
	if(N == 0)
	{
		b0 = getInstanceL2Util().Yellow;		
	}
	else if(N == 1)
	{
		b1 = getInstanceL2Util().Yellow;			
	}
	else if(N == 2)
	{
		b2 = getInstanceL2Util().Yellow;				
	}
	else if(N == 3)
	{
		b3 = getInstanceL2Util().Yellow;
	}
	drawListArr[drawListArr.Length] = addDrawItemText(GetSystemString(3862), b0, "", true, true);
	drawListArr[drawListArr.Length] = addDrawItemText(GetSystemString(3863), b1, "", true, true);
	drawListArr[drawListArr.Length] = addDrawItemText(GetSystemString(3864), b2, "", true, true);
	drawListArr[drawListArr.Length] = addDrawItemText(GetSystemString(3865), b3, "", true, true);
	drawListArr[drawListArr.Length] = addDrawItemBlank(4);
	drawListArr[drawListArr.Length] = AddCrossLineForCustomToolTip(130);
	drawListArr[drawListArr.Length] = addDrawItemBlank(4);
	toolString = MenuEntireWnd(GetScript("MenuEntireWnd")).setMainShortcutString(MenuEntireWnd(GetScript("MenuEntireWnd")).getAssignedKeyGroup(), "NextTargetModeChange");
	drawListArr[drawListArr.Length] = addDrawItemText(toolString, getInstanceL2Util().White, "", true, true);
	Next_Target_BTN.SetTooltipCustomType(MakeTooltipMultiTextByArray(drawListArr));	
}

// 보조 인벤토리 열기
function onInventory_ButtonClick()
{
	if(GetWindowHandle("AutoUseItemInventory").IsShowWindow())GetWindowHandle("AutoUseItemInventory").HideWindow();
	else AutoUseItemInventoryScript.showWindowByParentWindow(Me);
}

// 강제로 보조 인벤토리 열기
function forceShowInven()
{
	AutoUseItemInventoryScript.showWindowByParentWindow(Me);
}

function OnHide()
{
	if(GetWindowHandle("AutoUseItemInventory").IsShowWindow())GetWindowHandle("AutoUseItemInventory").HideWindow();
}

//---------------------------------------------------------------------------------------------------------
// 자동 타겟
//---------------------------------------------------------------------------------------------------------
function AutotargetOnShow()
{
	local int nLongTarget, nIsPickupOn, nIsMannerModeOn;

	if(getInstanceUIData().getIsClassicServer())
	{
		Me.HideWindow();
		return;
	}
	if(! GetINIBool("AutoUseItemWnd", "a", nLongTarget, "windowsInfo.ini"))
	{
		nLongTarget = 1;
		SetINIBool("AutoUseItemWnd", "a", numToBool(nLongTarget), "windowsInfo.ini");
	}
	if(! GetINIBool("AutoUseItemWnd", "e", nIsPickupOn, "windowsInfo.ini"))
	{
		nIsPickupOn = 1;
		SetINIBool("AutoUseItemWnd", "e", numToBool(nIsPickupOn), "windowsInfo.ini");
	}
	if(! GetINIBool("AutoUseItemWnd", "p", nIsMannerModeOn, "windowsInfo.ini"))
	{
		nIsMannerModeOn = 1;
		SetINIBool("AutoUseItemWnd", "p", numToBool(nIsMannerModeOn), "windowsInfo.ini");
	}
	GetINIInt("AutoUseItemWnd", "v", nMacroSlotSelect, "windowsInfo.ini");
	autotarget_bIsPickupOn = numToBool(nIsPickupOn);
	autotarget_bShortTarget = ! numToBool(nLongTarget);
	autotarget_bIsMannerModeOn = numToBool(nIsMannerModeOn);
	Autotarget_updateShowHideNextTargetButton();
	Autotarget_SetCusomTooltip();
	Autotarget_NextTargetSetCusomTooltip();
	SetMacroSlotSelect();	
}

function SetMacroSlotSelect()
{
	if(nMacroSlotSelect == 0)
	{
		MacroSelectBtn_01.SetTexture("L2UI_CT1.AutoShotItemWnd.MacroSelectBtn", "L2UI_CT1.AutoShotItemWnd.MacroSelectBTN_normal", "L2UI_CT1.AutoShotItemWnd.MacroSelectBTN_over");
		MacroSelectBtn_02.SetTexture("L2UI_CT1.AutoShotItemWnd.MacroSelectBTN_normal", "L2UI_CT1.AutoShotItemWnd.MacroSelectBTN_normal", "L2UI_CT1.AutoShotItemWnd.MacroSelectBTN_over");
		MacroSelectBtn_Click("MacroSelectBtn_01");		
	}
	else
	{
		MacroSelectBtn_01.SetTexture("L2UI_CT1.AutoShotItemWnd.MacroSelectBTN_normal", "L2UI_CT1.AutoShotItemWnd.MacroSelectBTN_normal", "L2UI_CT1.AutoShotItemWnd.MacroSelectBTN_over");
		MacroSelectBtn_02.SetTexture("L2UI_CT1.AutoShotItemWnd.MacroSelectBtn", "L2UI_CT1.AutoShotItemWnd.MacroSelectBTN_normal", "L2UI_CT1.AutoShotItemWnd.MacroSelectBTN_over");
		MacroSelectBtn_Click("MacroSelectBtn_02");
	}	
}

function Autotarget_Init()
{
	autotarget_bUseAutoTarget = false;
}

// 숏컷 등록
function Autotarget_UpdateShortCutElement()
{
	class'UIAPI_SHORTCUTITEMWINDOW'.static.UpdateShortcut("AutoUseItemWnd.AutoTargetWnd.Macro1ShortcutItem", AutoplayMacroSlotID);
	class'UIAPI_SHORTCUTITEMWINDOW'.static.UpdateShortcut("AutoUseItemWnd.AutoTargetWnd.Macro2ShortcutItem", AutoplayMacroSlotID2);
}

//// 타겟 실행
function executeTarget()
{
	// 근거리, 원거리 
	if(autotarget_bShortTarget)
	{
		ExecuteCommand("/targetnext");
	}
	else 
	{
		ExecuteCommand("/targetnext2");
	}
}

// 버프 툴팁을 설정한다.
function Autotarget_SetCusomTooltip()
{
	local Color b0, b1;
	local Array<DrawItemInfo> drawListArr;

	b0 = getInstanceL2Util().Gray;
	b1 = getInstanceL2Util().Gray;

	shotD_Target_BTN.ClearTooltip();
	shotD_Target_BTN.SetTooltipType("text");

	//Debug("autotarget_bShortTarget" @ autotarget_bShortTarget);
	if(autotarget_bShortTarget)
	{
		b0 = getInstanceL2Util().Yellow;
	}
	else
	{
		b1 = getInstanceL2Util().Yellow;
	}

	drawListArr[drawListArr.Length] = addDrawItemText(GetSystemString(3956), b0, "", true, true);
	drawListArr[drawListArr.Length] = addDrawItemText(GetSystemString(3957), b1, "", true, true);
	shotD_Target_BTN.SetTooltipCustomType(MakeTooltipMultiTextByArray(drawListArr));
}

// 버프 툴팁을 설정한다.
function Autotarget_PickupSetCusomTooltip()
{
	local Color b0, b1;
	local Array<DrawItemInfo> drawListArr;

	b0 = getInstanceL2Util().Gray;
	b1 = getInstanceL2Util().Gray;

	TargetPickupToggle_BTN.ClearTooltip();
	TargetPickupToggle_BTN.SetTooltipType("text");

	//Debug("autotarget_bShortTarget" @ autotarget_bShortTarget);
	if(autotarget_bIsPickupOn)
	{
		// 자동줍기 켜기
		b0 = getInstanceL2Util().Yellow;		
	}
	else
	{
		//자동줍기 끄기 
		b1 = getInstanceL2Util().Yellow;
	}

	drawListArr[drawListArr.Length] = addDrawItemText(GetSystemString(3993), b0, "", true, true);
	drawListArr[drawListArr.Length] = addDrawItemText(GetSystemString(3992), b1, "", true, true);
	
	TargetPickupToggle_BTN.SetTooltipCustomType(MakeTooltipMultiTextByArray(drawListArr));
}

function Autotarget_MannerModeSetCusomTooltip()
{
	local Color b0;
	local Color b1;
	local array<DrawItemInfo> drawListArr;

	b0 = getInstanceL2Util().Gray;
	b1 = getInstanceL2Util().Gray;

	TargetMannerToggle_BTN.ClearTooltip();
	TargetMannerToggle_BTN.SetTooltipType("text");

	if(autotarget_bIsMannerModeOn)
		b0 = getInstanceL2Util().Yellow;
	else
		b1 = getInstanceL2Util().Yellow;

	drawListArr[drawListArr.Length] = addDrawItemText(GetSystemString(13080),b0,"",true,true);
	drawListArr[drawListArr.Length] = addDrawItemText(GetSystemString(13081),b1,"",true,true);

	TargetMannerToggle_BTN.SetTooltipCustomType(MakeTooltipMultiTextByArray(drawListArr));
}

// 우클릭 , 자동 타겟 on, off 
function OnRButtonUp(WindowHandle a_WindowHandle, int X, int Y)
{
	//Debug("OnRButtonUp"@a_WindowHandle.GetWindowName());
	
	switch(a_WindowHandle.GetWindowName())
	{
		case "AutoTargetAll_BTN":
			requestAutoPlay(! autotarget_bUseAutoTarget);
			break;
		case "AutoAll_BTN":
			OnClickButton(a_WindowHandle.GetWindowName());
			break;
	}
}

// 자동 타겟중 인가?
function bool getUseAutoTarget()
{
	return autotarget_bUseAutoTarget;
}

// 타겟 근거리, 원거리 교체
function Autotarget_OnSwap_Target_BTNClick()
{
	autotarget_bShortTarget = !autotarget_bShortTarget;

	SetInIBool("AutoUseItemWnd", "a", !autotarget_bShortTarget, "windowsInfo.ini");

	if(autotarget_bShortTarget)getInstanceL2Util().showGfxScreenMessage(GetSystemString(3956));
	else getInstanceL2Util().showGfxScreenMessage(GetSystemString(3957));
	Autotarget_updateShowHideNextTargetButton();
	Autotarget_SetCusomTooltip();
}

function Autotarget_TargetMannerToggle_BTNClick()
{
	autotarget_bIsMannerModeOn = !autotarget_bIsMannerModeOn;
	SetINIBool("AutoUseItemWnd","p",autotarget_bIsMannerModeOn,"windowsInfo.ini");

	if(autotarget_bIsMannerModeOn)
		getInstanceL2Util().showGfxScreenMessage(GetSystemString(13080));
	else
		getInstanceL2Util().showGfxScreenMessage(GetSystemString(13081));

	Autotarget_updateMannerModeButton();
	Autotarget_MannerModeSetCusomTooltip();
}

// 타겟 근거리, 원거리 교체
function Autotarget_TargetPickupToggle_BTNClick()
{
	autotarget_bIsPickupOn = !autotarget_bIsPickupOn;

	SetInIBool("AutoUseItemWnd", "e", autotarget_bIsPickupOn, "windowsInfo.ini");

	if(autotarget_bIsPickupOn)getInstanceL2Util().showGfxScreenMessage(GetSystemString(3993));
	else getInstanceL2Util().showGfxScreenMessage(GetSystemString(3992));

	Autotarget_updatePickupButton();
	Autotarget_PickupSetCusomTooltip();
}

// 근거리, 원거리 타겟 버튼 텍스쳐 변경
function Autotarget_updateShowHideNextTargetButton()
{
	if(autotarget_bShortTarget)
	{
		shotD_Target_BTN.SetTexture("l2UI_CT1.AutoShotItemWnd.TargetBTN_ShotD_Normal","l2UI_CT1.AutoShotItemWnd.TargetBTN_ShotD_Down" , "l2UI_CT1.AutoShotItemWnd.TargetBTN_ShotD_Over");
	}
	else
	{
		shotD_Target_BTN.SetTexture("l2UI_CT1.AutoShotItemWnd.TargetBTN_LongD_Normal", "l2UI_CT1.AutoShotItemWnd.TargetBTN_LongD_Down", "l2UI_CT1.AutoShotItemWnd.TargetBTN_LongD_Over");
	}
}

// 자동 줍기 버튼 텍스쳐 변경 
function Autotarget_updatePickupButton()
{
	if(autotarget_bIsPickupOn)
	{
		TargetPickupToggle_BTN.SetTexture("L2UI_CT1.AutoShotItemWnd.GetBTNON_Normal", "L2UI_CT1.AutoShotItemWnd.GetBTNON_Down" , "L2UI_CT1.AutoShotItemWnd.GetBTNON_Over");
	}
	else
	{
		TargetPickupToggle_BTN.SetTexture("L2UI_CT1.AutoShotItemWnd.GetBTNOff_Normal", "L2UI_CT1.AutoShotItemWnd.GetBTNOff_down", "L2UI_CT1.AutoShotItemWnd.GetBTNOff_Over");
	}
}

function Autotarget_updateMannerModeButton()
{
	if(autotarget_bIsMannerModeOn)
	{
		TargetMannerToggle_BTN.SetTexture("L2UI_CT1.AutoShotItemWnd.MannerBTNON_Normal", "L2UI_CT1.AutoShotItemWnd.MannerBTNON_down", "L2UI_CT1.AutoShotItemWnd.MannerBTNON_over");
	}
	else
	{
		TargetMannerToggle_BTN.SetTexture("L2UI_CT1.AutoShotItemWnd.MannerBTNOff_Normal", "L2UI_CT1.AutoShotItemWnd.MannerBTNOff_down", "L2UI_CT1.AutoShotItemWnd.MannerBTNOff_over");
	}
}

// 자동 타겟 활성화 버튼 이미지, 모션 제어
function Autotarget_UpdateAutoTargetState()
{
	if(autotarget_bUseAutoTarget)
	{
		AnimTexturePlay(AutoTarget_ToggleMacro_Anim, true);

		AnimTexturePlay(AutoTargetAllON_ToggleEffect_Anim, true);

		GetWindowHandle("AutoUseItemWnd.AutoTargetWnd.AutoTargetAllON_Win").ShowWindow();
		GetWindowHandle("AutoUseItemWnd.AutoTargetWnd.AutoTargetAllOFF_Win").HideWindow();
	}
	else
	{
		AnimTextureStop(AutoTarget_ToggleMacro_Anim, true);
		
		AnimTextureStop(AutoTargetAllON_ToggleEffect_Anim, true);

		GetWindowHandle("AutoUseItemWnd.AutoTargetWnd.AutoTargetAllON_Win").HideWindow();
		GetWindowHandle("AutoUseItemWnd.AutoTargetWnd.AutoTargetAllOFF_Win").ShowWindow();
	}

	// 최소화 애니 
	AutoUseItemWndMinScript.setPlayAutoTargetActiveAnim();
	YetiQuickSlotwndScript.setPlayAutoTargetActiveAnim();
}

// 자동 타겟 실행
function requestAutoPlay(bool bUseAutoTarget, optional int nHPPotionPercent)
{
	local AutoplaySettingData pAutoplaySettingData;

	// 옵션의 타겟 타입
	autotarget_nTargetMode = GetNextTargetModeOption();
	pAutoplaySettingData.IsAutoPlayOn = bUseAutoTarget;
	pAutoplaySettingData.IsPickupOn = autotarget_bIsPickupOn;
	pAutoplaySettingData.NextTargetMode = autotarget_nTargetMode;
	pAutoplaySettingData.IsNearTargetMode = autotarget_bShortTarget;
	pAutoplaySettingData.IsMannerModeOn = autotarget_bIsMannerModeOn;
	pAutoplaySettingData.MacroIndex = byte(nMacroSlotSelect);
	if(nHPPotionPercent > 0)
	{
		pAutoplaySettingData.HPPotionPercent = nHPPotionPercent;
	}
	else
	{
		pAutoplaySettingData.HPPotionPercent = autotarget_nHPPotionPercent;
	}
	Debug("------------------------------------------------------------------");
	Debug("API -각성-- UpdateAutoplaySetting()");
	Debug("bUseAutoTarget               : " @ string(bUseAutoTarget));
	Debug("autotarget_bIsPickupOn       : " @ string(autotarget_bIsPickupOn));
	Debug("autotarget_bIsMannerModeOn    : " @ string(autotarget_bIsMannerModeOn));
	Debug("autotarget_nTargetMode       : " @ string(autotarget_nTargetMode));
	Debug("autotarget_bShortTarget      : " @ string(autotarget_bShortTarget));
	Debug("MacroIndex  : " @ string(nMacroSlotSelect));
	Debug("autotarget_nHPPotionPercent  : " @ string(pAutoplaySettingData.HPPotionPercent));
	UpdateAutoplaySetting(pAutoplaySettingData);
}

function requestAutoPlayForAutoPotion(int nHPPotionPercent)
{
	requestAutoPlay(autotarget_bUseAutoTarget, nHPPotionPercent);
}

function setShortcutTooltip(string tooltipStr)
{
	GetButtonHandle("AutoUseItemWnd.AutoTargetWnd.AutoTargetAll_BTN").SetTooltipCustomType(MakeTooltipMultiText(GetSystemString(2165), getInstanceL2Util().White,, true, tooltipStr, getInstanceL2Util().BWhite,, true));
}

private function HandleUpdatePlayerAutoAttacking()
{
	if(API_IsAutoAttacking())
	{
		GetTextureHandle("AutoUseItemWnd.AutoTargetWnd.AutoTargetAllON_Win.AutoAllIcon_On_texture").SetTexture("L2UI_CT1.AutoShotItemWnd.AutoTargetFight_On");
		GetTextureHandle("AutoUseItemWnd.AutoTargetWnd.AutoTargetAllOFF_Win.AutoAllIcon_Off_texture").SetTexture("L2UI_CT1.AutoShotItemWnd.AutoTargetFight_Off");		
	}
	else
	{
		GetTextureHandle("AutoUseItemWnd.AutoTargetWnd.AutoTargetAllON_Win.AutoAllIcon_On_texture").SetTexture("L2UI_CT1.AutoShotItemWnd.AutoTargetAllIcon_On");
		GetTextureHandle("AutoUseItemWnd.AutoTargetWnd.AutoTargetAllOFF_Win.AutoAllIcon_Off_texture").SetTexture("L2UI_CT1.AutoShotItemWnd.AutoTargetAllIcon_Off");
	}	
}

private function bool API_IsAutoAttacking()
{
	return class'UIDATA_PLAYER'.static.IsAutoAttacking();	
}
