//================================================================================
// QuitReportInstantZoneWnd.
//================================================================================

class QuitReportInstantZoneWnd extends UICommonAPI;

var WindowHandle Me;
var WindowHandle PlayReportWnd;
var TextBoxHandle txtPlayReportWnd_exp;
var TextBoxHandle txtPlayReportWnd_adena;
var TextBoxHandle txtPlayReportWnd_item;
var ButtonHandle ITEMButton_Right;
var INT64 beforeExp;
var INT64 Exp;
var INT64 beforeAdena;
var INT64 Adena;
var bool firstSetting;
var bool firstAdenaSetting;
var QuitReportDrawerInstantZoneWnd QuitReportDrawerWndScript;
var UserInfo PlayerInfo;
var bool bGainStart;
const TIMER_ID = 999001;
var bool bTryShowOnGamingState;

function OnRegisterEvent ()
{
  RegisterEvent(EV_Restart);
  RegisterEvent(EV_UpdateUserInfo);
  RegisterEvent(EV_AdenaInvenCount);
  RegisterEvent(EV_ChangedSubjob);
	RegisterEvent(EV_GamingStateEnter);
	RegisterEvent(EV_HideQuitReportInstanceZone);
}

function OnLoad ()
{
  SetClosingOnESC();
  Initialize();
}

function OnShow ()
{
	Me.SetFocus();
}

function OnHide ()
{
}

function Initialize ()
{
  Me = GetWindowHandle("QuitReportInstantZoneWnd");
  txtPlayReportWnd_item = GetTextBoxHandle("QuitReportInstantZoneWnd.PlayReportWnd.txtPlayReportWnd_item");
  txtPlayReportWnd_exp = GetTextBoxHandle("QuitReportInstantZoneWnd.PlayReportWnd.txtPlayReportWnd_exp");
  txtPlayReportWnd_adena = GetTextBoxHandle("QuitReportInstantZoneWnd.PlayReportWnd.txtPlayReportWnd_adena");
  ITEMButton_Right = GetButtonHandle("QuitReportInstantZoneWnd.PlayReportWnd.ITEMButton_Right");
  QuitReportDrawerWndScript = QuitReportDrawerInstantZoneWnd(GetScript("QuitReportDrawerInstantZoneWnd"));
  Init();
}

function Init ()
{
  beforeExp = 0;
  Exp = 0;
  Adena = 0;
  beforeAdena = GetAdena();
  firstSetting = False;
  firstAdenaSetting = False;
  txtPlayReportWnd_exp.SetText(MakeFullSystemMsg(GetSystemMessage(4323),"0"));
  txtPlayReportWnd_item.SetText(MakeFullSystemMsg(GetSystemMessage(1983),"0"));
  txtPlayReportWnd_adena.SetText(MakeFullSystemMsg(GetSystemMessage(2932),"0"));
  if ( GetWindowHandle("QuitReportDrawerInstantZoneWnd").IsShowWindow() )
  {
    setDrawerButtonState(True);
    GetWindowHandle("QuitReportDrawerInstantZoneWnd").HideWindow();
  }
}

function setDrawerButtonState (bool bShow)
{
  if ( bShow )
  {
    ITEMButton_Right.SetTexture("L2UI_CT1.Button.Button_DF_Right_Down","L2UI_CT1.Button.Button_DF_Right_Down","L2UI_CT1.Button.Button_DF_Right_Over");
  } else {
    ITEMButton_Right.SetTexture("L2UI_CT1.Button.Button_DF_Left_Down","L2UI_CT1.Button.Button_DF_Left_Down","L2UI_CT1.Button.Button_DF_Left_Over");
  }
}

function OnClickButton (string Name)
{
  switch (Name)
  {
    case "ITEMButton_Right":
    if (  !GetWindowHandle("QuitReportDrawerInstantZoneWnd").IsShowWindow() )
    {
      setDrawerButtonState(False);
      GetWindowHandle("QuitReportDrawerInstantZoneWnd").ShowWindow();
    } else {
      setDrawerButtonState(True);
      GetWindowHandle("QuitReportDrawerInstantZoneWnd").HideWindow();
    }
    break;
    case "BtnCancel":
    OnbtnCancelClick();
    break;
    default:
  }
}

function InfoGainStart()
{
	bGainStart = true;
	GetPlayerInfo(PlayerInfo);
	beforeExp = PlayerInfo.nCurExp;
	Exp = 0;
	Adena = 0;
	beforeAdena = GetAdena();
	firstSetting = true;
	firstAdenaSetting = true;
	QuitReportDrawerWndScript.Init();
	Debug("수집 시작!!!!!");	
}

function InfoGainResult()
{
	bGainStart = false;
	Me.ShowWindow();
	UpdateAdena(Adena, true);
	UpdateUserInfoHandler(true);
	QuitReportDrawerWndScript.updateList();	
}

function OnbtnCancelClick ()
{
	Me.HideWindow();
}

function OnEvent (int a_EventID, string a_Param)
{
  local INT64 pAdena;
	local int nQuitRestrictField;

	if ( a_EventID == EV_AdenaInvenCount )
	{
		if (  !bGainStart )
		{
			return;
		}
		ParseINT64(a_Param,"Adena",pAdena);
		UpdateAdena(pAdena);
	} 
	else if ( a_EventID == EV_Restart )
	{
		bTryShowOnGamingState = false;
		ParseInt(a_Param, "QuitRestrictField", nQuitRestrictField);
		Debug("EV_Restart" @ string(nQuitRestrictField));
		if(nQuitRestrictField == 0)
		{
			Init();
			QuitReportDrawerWndScript.Init();				
		}
		else
		{
			if(Me.IsShowWindow())
			{
				bTryShowOnGamingState = true;
			}
		}
	}
	else if ( a_EventID == EV_UpdateUserInfo )
	{
        if (  !bGainStart )
        {
          return;
        }
        UpdateUserInfoHandler();
	}
	else if ( a_EventID == EV_ChangedSubjob )
	{
        firstSetting = False;
        beforeExp = 0;
        Exp = 0;
        txtPlayReportWnd_exp.SetText(MakeFullSystemMsg(GetSystemMessage(4323),"0"));
        Debug("----> EV_ChangedSubjob 초기화 경험치 초기화 ");
	}
	else if(a_EventID == EV_GamingStateEnter)
	{
        Debug("EV_GamingStateEnter 다시 열릴지 체크 " @ string(bTryShowOnGamingState));
        if(bTryShowOnGamingState)
        {
			bTryShowOnGamingState = false;
			Me.ShowWindow();
        }						
	}
	else if(a_EventID == EV_HideQuitReportInstanceZone)
	{
		Me.HideWindow();
	}
}

function UpdateAdena(INT64 pAdena, optional bool bSetTextUpdate)
{
	if(firstAdenaSetting == false)
	{
		Adena = 0;
		beforeAdena = pAdena;
		firstAdenaSetting = true;
	}
	if(pAdena > beforeAdena)
	{
		Adena = Adena + (pAdena - beforeAdena);
	}
	beforeAdena = pAdena;
	if(bSetTextUpdate)
	{
		if(Adena > 0)
		{
			txtPlayReportWnd_adena.SetText(MakeFullSystemMsg(GetSystemMessage(2932), MakeCostString(string(Adena))));			
		}
		else
		{
			txtPlayReportWnd_adena.SetText(MakeFullSystemMsg(GetSystemMessage(2932), "0"));
		}
	}
}

function UpdateUserInfoHandler(optional bool bSetTextUpdate)
{
	local INT64 ItemCount;

	if(GetGameStateName() != "GAMINGSTATE")
		return;

	GetPlayerInfo(PlayerInfo);

	if(firstSetting == false)
	{
		firstSetting = true;
		beforeExp = PlayerInfo.nCurExp;
		Exp = 0;
	}

	if(PlayerInfo.nCurExp > beforeExp)
	{
		Exp = Exp + (PlayerInfo.nCurExp - beforeExp);
	}
	beforeExp = PlayerInfo.nCurExp;

	if(bSetTextUpdate)
	{
		ItemCount = int64(QuitReportDrawerWndScript.getTotalItemCount());

		if(Exp > 0)
		{
			txtPlayReportWnd_exp.SetText(MakeFullSystemMsg(GetSystemMessage(4323), MakeCostString(string(Exp))));
		}
		else
		{
			txtPlayReportWnd_exp.SetText(MakeFullSystemMsg(GetSystemMessage(4323), "0"));
		}

		if(ItemCount > 0)
		{
			txtPlayReportWnd_item.SetText(MakeFullSystemMsg(GetSystemMessage(1983), MakeCostString(string(ItemCount))));
		}
		else
		{
			txtPlayReportWnd_item.SetText(MakeFullSystemMsg(GetSystemMessage(1983), "0"));
		}
	}
}

function externalAddItem (ItemInfo addItemInfo)
{
  if (  !bGainStart )
  {
    return;
  }
  QuitReportDrawerWndScript.externalAddItem(addItemInfo);
}

function OnReceivedCloseUI ()
{
  PlayConsoleSound(IFST_WINDOW_CLOSE);
  GetWindowHandle(getCurrentWindowName(string(self))).HideWindow();
}

