/*------------------------------------------------------------------------------------------------------------------------

 제목     : 정령 UI - SCALEFORM UI

 event ID : 
 npc 

 - 빌드 명령어 - 

// 정령 부여
//elemental_system_insert

// 각 정령들 경험치 올리기
//elemental_system_exp fire 1000000000
//elemental_system_exp water 1000000
//elemental_system_exp wind 1000000
//elemental_system_exp earth 1000000

//elemental_system_delete_elemental
  현재 모든 정령 삭제 및 초기화(속성 부여받지 않은 상태로 돌아감)

//elemental_system_level [elemental_type] [grade] [level]
  정령을 특정 성급에 특정 레벨로 변경(ex)//elemental_system_level fire 2 8)
  type은 fire / water / wind / earth 4가지 입니다.


- 관련 npc: 스베인 
스베인을 호출 해서 직접 정령을 부여 받아도 된다.

- 관련 개발망 위치
http://lineage2:8080/wiki/ElementalSystem#A.2Bvky03A_.2BuoW4OcW0-

------------------------------------------------------------------------------------------------------------------------*/
class ElementalSpiritWnd extends L2UIGFxScriptNoneContainer;

const SPIRIT_TYPE_FIRE = 0;
const SPIRIT_TYPE_WATER = 1;
const SPIRIT_TYPE_WIND = 2;
const SPIRIT_TYPE_EARTH = 3;
const SPIRITTYPECOUNTMAX = 4;

struct ElementalInfo
{
	var int SpiritType;
	var int NpcID;
	var int SpiritClassID;
	var int EvolLevel;
	var INT64 Exp;
	var INT64 NextExp;
	var int CurrLevel;
	var int RemainPoint;
};

var bool bUseSpirit;
var private array<ElementalInfo> elementalInfos;
            
event OnRegisterEvent()
{
	// 모든 정령에 대한 정보 10860
	RegisterGFxEvent(EV_ElementalSpiritInfo); 

	// 추출 버튼 클릭 후 상세창 10870
	RegisterGFxEvent(EV_ElementalSpiritExtractInfo);

	// 추출 완료 후 10871
	RegisterGFxEvent(EV_ElementalSpiritExtract);

	// 진화 버튼 클릭 후 진화관련 상세창 10880
	RegisterGFxEvent(EV_ElementalSpiritEvolutionInfo);

	// 정령 진화 실행 버튼 클릭 후 응답 10890
	RegisterGFxEvent(EV_ElementalSpiritEvolution);

	// 정령의 특성을 선택하거나 초기화해서 능력치가 갱신될 때 10900
	RegisterGFxEvent(EV_ElementalSpiritSetTalent);

	// 흡수 버튼 누르면 오는 이벤트 10910
	RegisterGFxEvent(EV_ElementalSpiritAbsorbInfo); 

	// 흡수 아이템 적용 눌렀을 때 서버로부터 오는 응답 이벤트 10920
	RegisterGFxEvent(EV_ElementalSpiritAbsorb);

	// 경험치가 변경 되었을때 
	RegisterGFxEvent(EV_ElementalSpiritGetExp);
	RegisterEvent(EV_ElementalSpiritSimpleInfo);
	RegisterEvent(EV_ElementalSpiritGetExp);
	RegisterEvent(EV_Restart);
	RegisterEvent(EV_GameStart);
	RegisterEvent(EV_ProtocolBegin + class'UIPacket'.const.S_EX_ELEMENTAL_SPIRIT_ATTACK_TYPE);
}

event OnLoad()
{
	SetSaveWnd(true, false);

	SetClosingOnESC();
	AddState("GAMINGSTATE");

	SetAnchor("", EAnchorPointType.ANCHORPOINT_CenterCenter, EAnchorPointType.ANCHORPOINT_CenterCenter, 0,0);

	//SetContainerWindow(WINDOWTYPE_NONE, 0);		
	//SetContainerWindow(WINDOWTYPE_DECO_NORMAL, 7285);

	//SetClosingOnESC();

	//registerState("ElementalSpiritWnd", "GamingState");

	//SetAnchor("", EAnchorPointType.ANCHORPOINT_CenterCenter, EAnchorPointType.ANCHORPOINT_CenterCenter, 0,0);
	///SetHavingFocus(false);
	elementalInfos.Length = SPIRITTYPECOUNTMAX;
	GetTextureSideElementalHandle(SPIRIT_TYPE_FIRE).HideWindow();
	GetTextureSideElementalHandle(SPIRIT_TYPE_WATER).HideWindow();
	GetTextureSideElementalHandle(SPIRIT_TYPE_WIND).HideWindow();
	GetTextureSideElementalHandle(SPIRIT_TYPE_EARTH).HideWindow();
}

event OnFlashLoaded()
{
	RegisterDelegateHandler(EDelegateHandlerType.EDHandler_ElementalSpirit);
	RegisterDelegateHandler(EDelegateHandlerType.EDHandler_Container);
	RegisterDelegateHandler(EDelegateHandlerType.EDHandler_Default);
	RegisterDelegateHandler(EDelegateHandlerType.EDHandler_GameData);
}

event OnShow()
{
	if(class'UIAPI_WINDOW'.static.IsShowWindow("CardDrawEventWnd"))
		class'UIAPI_WINDOW'.static.HideWindow("CardDrawEventWnd");

	if(class'UIAPI_WINDOW'.static.IsShowWindow("ItemUpgrade"))
		class'UIAPI_WINDOW'.static.HideWindow("ItemUpgrade");

	super.OnShow();

	L2Util(GetScript("L2Util")).ItemRelationWindowHide(getCurrentWindowName(string(self)));
	class'SideBar'.static.Inst().ToggleByWindowName(getCurrentWindowName(string(self)), true);
}

event OnHide()
{
	class'SideBar'.static.Inst().ToggleByWindowName(getCurrentWindowName(string(self)), false);
	super.OnHide();	
}

event OnEvent(int a_EventID, string a_Param)
{
	switch(a_EventID)
	{
		case EV_Restart:
			bUseSpirit = false;
			class'SideBar'.static.Inst().SetWindowShowHideByIndex(class'SideBar'.static.Inst().SIDEBAR_WINDOWS.TYPE_ELEMENT, false);
			break;
		case EV_ElementalSpiritSimpleInfo:
			HandleEV_ElementalSpiritSimpleInfo(a_Param);
			break;
		case EV_ElementalSpiritGetExp:
			HandleEV_ElementalSpiritGetExp(a_Param);
			break;
		case EV_ProtocolBegin + class'UIPacket'.const.S_EX_ELEMENTAL_SPIRIT_ATTACK_TYPE:
			RT_S_EX_ELEMENTAL_SPIRIT_ATTACK_TYPE();
			break;
		case EV_GameStart:
			bUseSpirit = false;
			ResetElementalSpirits();
			break;
	}	
}

function _API_RequestElementalSpiritInfo(bool bIsOpen)
{
	local int nIsOpen;

	Debug("bUseSpirit" @ string(bUseSpirit));
	if(bUseSpirit == false)
	{
		AddSystemMessage(13827);
		return;
	}
	if(bIsOpen)
	{
		nIsOpen = 1;
	}
	RequestElementalSpiritInfo(nIsOpen);	
}

private function HandleEV_ElementalSpiritSimpleInfo(string a_Param)
{
	local int Result, Len, i, NpcID, SpiritClassID, SpiritType,
		spiritIndex;

	Debug("HandleSpriteInfo" @ a_Param);
	if(ParseInt(a_Param, "Result", Result) && Result == 0)
	{
		return;
	}
	ParseInt(a_Param, "SpiritCount", Len);
	class'SideBar'.static.Inst().SetAlarmOnOff(class'SideBar'.static.Inst().SIDEBAR_WINDOWS.TYPE_ELEMENT, false);

	for(i = 0; i < Len; i++)
	{
		if(! ParseInt(a_Param, "NpcID_" $ string(i), NpcID))
		{
			// [Explicit Continue]
			continue;
		}
		bUseSpirit = true;
		ParseInt(a_Param, "SpiritClassID_" $ string(i), SpiritClassID);
		if((NpcID == 0) && SpiritClassID == 0)
		{
			// [Explicit Continue]
			continue;
		}
		ParseInt(a_Param, "SpiritType_" $ string(i), SpiritType);
		spiritIndex = SpiritType - 1;
		elementalInfos[spiritIndex].SpiritType = SpiritType;
		elementalInfos[spiritIndex].NpcID = NpcID;
		elementalInfos[spiritIndex].SpiritClassID = SpiritClassID;
		ParseInt(a_Param, "EvolLevel_" $ string(i), elementalInfos[spiritIndex].EvolLevel);
		ParseINT64(a_Param, "Exp_" $ string(i), elementalInfos[spiritIndex].Exp);
		ParseINT64(a_Param, "NextExp_" $ string(i), elementalInfos[spiritIndex].NextExp);
		ParseInt(a_Param, "CurrLevel_" $ string(i), elementalInfos[spiritIndex].CurrLevel);
		ParseInt(a_Param, "RemainPoint_" $ string(i), elementalInfos[spiritIndex].RemainPoint);
	}
	SetElementalExps();
	if(GetGameStateName() != "GAMINGSTATE")
	{
		return;
	}
	if(! bUseSpirit)
	{
		DetailStatusWndClassic(GetScript("DetailStatusWndClassic"))._LockElement();
		class'SideBar'.static.Inst().SetWindowShowHideByIndex(class'SideBar'.static.Inst().SIDEBAR_WINDOWS.TYPE_ELEMENT, false);		
	}
	else
	{
		DetailStatusWndClassic(GetScript("DetailStatusWndClassic"))._UnLockElement();
		class'SideBar'.static.Inst().SetWindowShowHideByIndex(class'SideBar'.static.Inst().SIDEBAR_WINDOWS.TYPE_ELEMENT, getInstanceUIData().getIsClassicServer());
	}

	for(i = 0; i < elementalInfos.Length; i++)
	{
		if(elementalInfos[i].RemainPoint > 0)
		{
			class'SideBar'.static.Inst().SetAlarmOnOff(class'SideBar'.static.Inst().SIDEBAR_WINDOWS.TYPE_ELEMENT, true);
			break;
		}
	}	
}

private function HandleEV_ElementalSpiritGetExp(string a_Param)
{
	local int Type;

	ParseInt(a_Param, "Type", Type);
	ParseINT64(a_Param, "Exp", elementalInfos[Type - 1].Exp);
	SetElementalExps();	
}

private function SetElementalExps()
{
	local INT64 ExpValue;
	local float per;
	local string nickname;
	local int CurrLevel, i;
	local DrawItemInfo Info, infoTexture;
	local CustomTooltip ToolTip;
	local bool isRemain;

	Info.eType = DIT_TEXT;
	Info.t_bDrawOneLine = true;

	for(i = 0; i < elementalInfos.Length; i++)
	{
		isRemain = elementalInfos[i].RemainPoint > 0;
		if(isRemain)
		{
			infoTexture.u_strTexture = "L2UI_NewTex.SideBar.ElementalPoint_Plus";			
		}
		else
		{
			infoTexture.u_strTexture = "L2UI_NewTex.SideBar.ElementalPoint_Unable";
		}
		infoTexture.eType = DIT_TEXTURE;
		infoTexture.t_bDrawOneLine = true;
		infoTexture.u_nTextureWidth = 8;
		infoTexture.u_nTextureHeight = 8;
		infoTexture.nOffSetX = 0;
		infoTexture.nOffSetY = 3;
		infoTexture.u_nTextureUWidth = 8;
		infoTexture.u_nTextureUHeight = 8;
		infoTexture.bLineBreak = true;
		infoTexture.eAlignType = DIAT_LEFT;
		ToolTip.DrawList[ToolTip.DrawList.Length] = infoTexture;
		CurrLevel = elementalInfos[i].CurrLevel;
		nickname = class'UIDATA_NPC'.static.GetNPCNickName(elementalInfos[i].NpcID);
		Info.t_strText = GetSystemString(88) $ "." $ string(CurrLevel) @ nickname;
		Info.t_color = class'L2Util'.static.Inst().BrightWhite;
		Info.eAlignType = DIAT_LEFT;
		Info.nOffSetX = 5;
		Info.bLineBreak = false;
		ToolTip.DrawList[ToolTip.DrawList.Length] = Info;
		GetElementalSpiritExpData(elementalInfos[i].SpiritType, elementalInfos[i].EvolLevel, elementalInfos[i].CurrLevel - 1, ExpValue);
		per = float(elementalInfos[i].Exp - ExpValue) / float(elementalInfos[i].NextExp - ExpValue);
		Info.t_strText = " " $ string(per * 100) $ "%";
		Info.t_color = class'L2Util'.static.Inst().Yellow;
		Info.bLineBreak = false;
		Info.eAlignType = DIAT_RIGHT;
		ToolTip.DrawList[ToolTip.DrawList.Length] = Info;
	}
	class'SideBar'.static.Inst().GetWindowByIndex(class'SideBar'.static.Inst().SIDEBAR_WINDOWS.TYPE_ELEMENT).SetTooltipCustomType(ToolTip);	
}

private function RT_S_EX_ELEMENTAL_SPIRIT_ATTACK_TYPE()
{
	local UIPacket._S_EX_ELEMENTAL_SPIRIT_ATTACK_TYPE packet;

	if(! class'UIPacket'.static.Decode_S_EX_ELEMENTAL_SPIRIT_ATTACK_TYPE(packet))
	{
		return;
	}
	CheckElementalActive(packet.cBitset);	
}

private function ResetElementalSpirits()
{
	local DetailStatusWndClassic script;

	script = DetailStatusWndClassic(GetScript("DetailStatusWndClassic"));
	script._ElementalDeActive(SPIRIT_TYPE_FIRE);
	script._ElementalDeActive(SPIRIT_TYPE_WATER);
	script._ElementalDeActive(SPIRIT_TYPE_WIND);
	script._ElementalDeActive(SPIRIT_TYPE_EARTH);	
}

private function CheckElementalActive(int bitSet)
{
	local int i, multiple;
	local DetailStatusWndClassic script;

	script = DetailStatusWndClassic(GetScript("DetailStatusWndClassic"));

	for(i = 0; i < 4; i++)
	{
		multiple = ExpInt(2, i);
		if((bitSet & multiple) > 0)
		{
			GetTextureSideElementalHandle(i).ShowWindow();
			script._ElementalActive(i);
			// [Explicit Continue]
			continue;
		}
		GetTextureSideElementalHandle(i).HideWindow();
		script._ElementalDeActive(i);
	}	
}

private function TextureHandle GetTextureSideElementalHandle(int Index)
{
	switch(Index)
	{
		case SPIRIT_TYPE_FIRE:
			return GetTextureHandle("SideBar.BtnElementalSpiritWnd.Icon_FireOn");
		case SPIRIT_TYPE_WATER:
			return GetTextureHandle("SideBar.BtnElementalSpiritWnd.Icon_WaterOn");
		case SPIRIT_TYPE_WIND:
			return GetTextureHandle("SideBar.BtnElementalSpiritWnd.Icon_WindOn");
		case SPIRIT_TYPE_EARTH:
			return GetTextureHandle("SideBar.BtnElementalSpiritWnd.Icon_EarthOn");
	}
	return GetTextureHandle("SideBar.BtnElementalSpiritWnd.Icon_EarthOn");
}
