#include-once

;; SMAC XML APi
Func lookupSteamId($input)
	Local $agent = "SMACheck/" & FileGetVersion(@ScriptName, "FileVersion")
	Local $oXML = ObjCreate("winhttp.winhttprequest.5.1")
	$oXML.Open("GET", "http://api.spiffytek.com/pub/smacheck/steamid/" & $input, False)
	$oXML.SetTimeouts(30000,30000,30000,30000)
	$oXML.SetRequestHeader("User-Agent", $agent)
	$oXML.Send()
	$oXML = $oXML.ResponseText

	$oXML = _XMLLoadXML($oXML)
	If @error <> 0 Then
		_printError("noapi")
		Return "ERROR_HANDELED"
	EndIf
	If NOT StringLeft($oXML, 13) = "<?xml version" Then
		_printError("noapi")
		Return "ERROR_HANDELED"
	EndIf

	Local $pStatus = _XMLGetValue('/isbanned/status')
	If IsArray($pStatus) <> 1 Then
		_printError("noapi")
		Return "ERROR_HANDELED"
	EndIf

	If @Compiled <> 0 Then
		Local $curVersion = FileGetVersion(@ScriptName, "FileVersion")
		Local $pMinVersion = _XMLGetValue("/isbanned/smaCheck/minVersion")
		Local $pActVersion = _XMLGetValue("/isbanned/smaCheck/actVersion")
		Global $gVersionChecked
		If IsArray($pMinVersion) Then
			If $curVersion < $pActVersion[1] Then
				If $curVersion <  $pMinVersion[1] Then
					_printError("update_force")
					Return "ERROR_HANDELED"
				ElseIf $gVersionChecked = 0 Then
					_printError("update")
					$gVersionChecked = 1
				EndIf
			EndIf
		EndIf
	EndIf


	If IsArray($pStatus) Then
		Local $bReason = _XMLGetValue("/isbanned/reason")
		Local $bNick = _XMLGetValue("/isbanned/nickname")
		Local $bTime = _XMLGetValue("/isbanned/banTime")
		Local $bVac = _XMLGetValue("/isbanned/vacStatus")
		Local $bMod = _XMLGetValue("/isbanned/mod")
		Local $bCc = _XMLGetValue("/isbanned/country")
		Local $bEac = _XMLGetValue("/isbanned/eacStatus")
		Local $bEsea = _XMLGetValue("/isbanned/eseaStatus")
		Local $bKac = _XMLGetValue("/isbanned/kacStatus")
		Local $bEsl = _XMLGetValue("/isbanned/eslStatus")

		If $bCc[1] = "" Then
			$bCc[1] = "nocountry"
		EndIf

		If $pStatus[1] = "Y" Then
			Local $oStatus = "1"
			Local $return[12]
			$return[0] = $oStatus
			$return[1] = _xmlentitiesDecode($bNick[1])
			$return[2] = $bReason[1]
			$return[3] = $bTime[1]
			$return[4] = $pStatus[1]
			$return[5] = $bVac[1]
			$return[6] = $bMod[1]
			$return[7] = $bCc[1]
			$return[8] = $bEac[1]
			$return[9] = $bEsea[1]
			$return[10] = $bKac[1]
			$return[11] = $bEsl[1]
		Else
			Local $oStatus = "0"
			Local $return[12]
			$return[0] = $oStatus
			$return[1] = _xmlentitiesDecode($bNick[1])
			$return[2] = "-"
			$return[3] = "-"
			$return[4] = $pStatus[1]
			$return[5] = $bVac[1]
			$return[6] = "-"
			$return[7] = "nocountry"
			$return[8] = $bEac[1]
			$return[9] = $bEsea[1]
			$return[10] = $bKac[1]
			$return[11] = $bEsl[1]
		EndIf

	Else
		_printError("noapi")
		Return "ERROR_HANDELED"
	EndIf

	Return $return

EndFunc



;; SteamID Convert
Dim Const $COMM_ID_START = 76561197960265728

Func steamId64ToSteamId($sComID)
    $sComID = Number($sComID)

    $a = ($sComID - $COMM_ID_START) / 2
    $b = Floor($a)
    $c = Round( Mod($a,Floor($a)) )

    Return "STEAM_0:" & String( $c ) & ":" & String( $b )
EndFunc


Func steamIdToCommunityId($sSteamID)
    Local $sResult, $expl, $serverid, $accountid

    $expl = StringSplit($sSteamID,":")

    if (UBound($expl) == 4) Then
        $serverid = Number($expl[2])
        $accountid = Number($expl[3])

        $sResult = ($accountid * 2) + $COMM_ID_START + $serverid
    EndIf

    Return $sResult
EndFunc


;; Misc
Func resetInput($iID)
	GUICtrlSetData($iID, "")
EndFunc

Func _printError($id = false)
	Switch $id
		Case "noapi"
			MsgBox(16, "ERROR", "API not available.")
		Case "update_force"
			MsgBox(16, "Required Update available", "Required Update available." & @CRLF & @CRLF & "Please download new Version from smacbans.com.")
		Case "update"
			MsgBox(48, "Update available", "New Update available." & @CRLF & @CRLF & "Visit smacbans.com for more Info and Download.")
		Case Else
			Return
	EndSwitch
EndFunc

Func _setStatusIcon($ctrlId, $status)
	If $status = "1" Then
		_ResourceSetImageToCtrl($ctrlId, "error.png", $RT_RCDATA)
	ElseIf $status = "-1" Then
		_ResourceSetImageToCtrl($ctrlId, "warning.png", $RT_RCDATA)
	Else
		_ResourceSetImageToCtrl($ctrlId, "tick.png", $RT_RCDATA)
	EndIf
EndFunc

Func _xmlentitiesDecode($string)
	Local $replace[6] = ["", '"', "&", "<", ">", "'"]
	Local $find[6] = ["", "&quot;", "&amp;", "&lt;", "&gt;", "&apos;"]
	For $iCC = 1 To UBound($replace) - 1
		$string = StringReplace($string, $find[$iCC], $replace[$iCC])
	Next
	Return $string
EndFunc