#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#include "includes/ext/resources.au3"
#include "includes/ext/_XMLDomWrapper.au3"

#include "includes/functions.inc.au3"

Global $gVersionChecked = 0

Global $oErrorHandler = ObjEvent("AutoIt.Error", "_printError");


;; Main
_main()
Func _main()
	Global $GuiWidth = 370, $GuiHeight = 177
	Local $GuiPosX = (@DesktopWidth - $GuiWidth) / 2, $GuiPosY = (@DesktopHeight - $GuiHeight) / 2

	Global $GUi = GUICreate("SMACheck - smacbans.com", $GuiWidth, $GuiHeight, $GuiPosX, $GuiPosY)
	$GUiBG = GUICtrlCreatePic("", 0, 0, $GuiWidth, $GuiHeight)
		_ResourceSetImageToCtrl(-1, "img_bg", $RT_RCDATA)
		GUICtrlSetState(-1, $GUI_DISABLE)
	$gInput = GUICtrlCreateLabel("SteamID:", 0, 50, 368, 17, $SS_CENTER)
		GUICtrlSetFont(-1, 10, 400, 0, "Tahoma")
		GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
	$gSteamId = GUICtrlCreateInput("", 112, 70, 145, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_CENTER))
		GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
	$gButton_Submit = GUICtrlCreateButton("Check", 144, 104, 83, 25, $BS_CENTER)
		GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
		GuiCtrlSetState(-1, 512)
	$gCopyright = GUICtrlCreateLabel("©2012-2013 SpiffyTek && smacBANS", 0, 159, 368, 17, $SS_RIGHT)
		GUICtrlSetFont(-1, 8, 400, 0, "Tahoma")
		GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
	$gVersion = GUICtrlCreateLabel("v" & $VERSION, 2, 159, 369, 17, $SS_LEFT)
		GUICtrlSetFont(-1, 8, 400, 0, "Tahoma")
		GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
	GUISetState(@SW_SHOW)


	While 1
		Switch GUIGetMsg()
			Case $GUI_EVENT_CLOSE
				ExitLoop
			Case $gButton_Submit
				$iSteamId = StringStripWS(GUICtrlRead($gSteamId), 8)
				If $iSteamId = "" Then
					GUISetState(@SW_DISABLE, $GUi)
					MsgBox(16, "ERROR", "Please enter a SteamId or SteamId64.")
					GUISetState(@SW_ENABLE, $GUi)
					WinActivate($GUi)
					GUISwitch($GUi)
				ElseIf StringLeft($iSteamId, 6) = 765611 Then
					$iSteamId = steamId64ToSteamId($iSteamId)
				EndIf

				If StringLeft($iSteamId, 6) = "STEAM_" Then
					GUISetState(@SW_DISABLE, $GUi)
					gui_Info($iSteamId)
					GUISetState(@SW_ENABLE, $GUi)
					WinActivate($GUi)
					GUISwitch($GUi)
				EndIf
				resetInput($gSteamId)
		EndSwitch
	WEnd
	GUIDelete()
EndFunc



Func gui_Info($output)
	Local $pInfo = lookupSteamId($output)

	If IsArray($pInfo) Then
		Global $GUi
		Local $overlayWidth = 312, $overlayHeight = 154, $pos = WinGetPos($GUi)

		$GUI2 = GUICreate("INFO (" & $output & ")", $overlayWidth, $overlayHeight, $pos[0] + 20, $pos[1] + 20, BitXOR($GUI_SS_DEFAULT_GUI, $WS_MINIMIZEBOX))
		$GUiBG2 = GUICtrlCreatePic("", 0, 0, $overlayWidth, $overlayHeight)
			_ResourceSetImageToCtrl(-1, "img_bg_popup", $RT_RCDATA)
			GUICtrlSetState(-1, $GUI_DISABLE)
		$bClose = GUICtrlCreateButton("OK", 120, 125, 65, 25, $BS_CENTER)
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
			GuiCtrlSetState(-1, 512)
		$gStatus = GUICtrlCreateLabel("Status:", 24, 12, 48, 17)
			GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
		If $pInfo[0] = "1" Then
			$oStatus = GUICtrlCreateLabel("SteamID is Banned", 72, 12, 215, 17)
				GUICtrlSetFont(-1, 8.5, 800)
				GUICtrlSetColor(-1, 0xD11313)
		Else
			$oStatus = GUICtrlCreateLabel("SteamID is not Banned", 72, 12, 215, 17)
				GUICtrlSetFont(-1, 8.5, 800)
				GUICtrlSetColor(-1, 0x245E00)
		EndIf
			GUICtrlSetBkColor($oStatus, $GUI_BKCOLOR_TRANSPARENT)
		$gNick = GUICtrlCreateLabel("Nick:", 24, 36, 48, 17)
			GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
		$oCc = GUICtrlCreatePic("", 72, 36, 50, 17)
			_ResourceSetImageToCtrl($oCc, $pInfo[7] & ".png", $RT_RCDATA)
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
			GUICtrlSetTip($oCc, StringUpper($pInfo[7]))
		$oNick = GUICtrlCreateLabel($pInfo[1], 93, 36, 215, 17)
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
		$gReason = GUICtrlCreateLabel("Reason:", 24, 60, 48, 17)
			GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
		$oReason = GUICtrlCreateLabel($pInfo[2], 72, 60, 215, 40)
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
		$gDate = GUICtrlCreateLabel("Date:", 24, 84, 48, 17)
			GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
		$oDate = GUICtrlCreateLabel($pInfo[3], 72, 84, 215, 17)
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
		$gMod = GUICtrlCreateLabel("Mod:", 120, 84, 48, 17)
			GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
		$oMod = GUICtrlCreateLabel($pInfo[6], 150, 84, 215, 17)
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
		$gVac = GUICtrlCreateLabel("VAC:", 30, 105, 48, 17)
			GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
		$oVac = GUICtrlCreatePic("", 60, 104, 16, 16)
				_setStatusIcon(-1, $pInfo[5])
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
		$gEac = GUICtrlCreateLabel("EAC:", 80, 105, 48, 17)
			GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
		$oEac = GUICtrlCreatePic("", 110, 104, 16, 16)
			_setStatusIcon(-1, $pInfo[8])
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
		$gEsea = GUICtrlCreateLabel("ESEA:", 130, 105, 48, 17)
			GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
		$oEsea = GUICtrlCreatePic("", 170, 104, 16, 16)
			_setStatusIcon(-1, $pInfo[9])
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
		$gKac = GUICtrlCreateLabel("KAC:", 190, 105, 48, 17)
			GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
		$oKac = GUICtrlCreatePic("", 220, 104, 16, 16)
			_setStatusIcon(-1, $pInfo[10])
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
		$gEsl = GUICtrlCreateLabel("ESL:", 240, 105, 48, 17)
			GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
		$oEsl = GUICtrlCreatePic("", 270, 104, 16, 16)
			_setStatusIcon(-1, $pInfo[11])
			GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)

		GUISetState()


		While 1
			; We can only get messages from the second GUI
			Switch GUIGetMsg()
				Case $GUI_EVENT_CLOSE
					GUIDelete($GUI2)
					ExitLoop
				Case $bClose
					GUIDelete($GUI2)
					ExitLoop
			EndSwitch
		WEnd
	EndIf
EndFunc