#include <GUIConstantsEx.au3>
#include <GuiListBox.au3>
#include <WindowsConstants.au3>
#include <Zip.au3>

Global $hListBox, $hGUI, $cBox[6]
Global $RootDir = @ComputerName, $ZipName = "SandieBoxBackup.zip"
Global $cBoxRestore[11]

#Region ### START Koda GUI section ### Form=C:\Dokumente und Einstellungen\frmkrmar\Desktop\Form1.kxf

$hGUI = GUICreate("Sandiebox Backup and Restore", 598, 490)
$Group1 = GUICtrlCreateGroup("Container", 16, 16, 569, 249)
$lPfad = GUICtrlCreateLabel("Pfad zur Sicherung", 32, 40, 94, 17)
$iPfad = GUICtrlCreateInput("", 144, 40, 321, 21)
GUICtrlSetState(-1,$GUI_DISABLE)
$btnEditPfad = GUICtrlCreateButton("Edit", 472, 40, 89, 20)
$hListBox = _GUICtrlListBox_Create($hGUI,"",45, 95, 420, 150)
$Group4 = GUICtrlCreateGroup("", 32, 80, 537, 177)
$btnAdd = GUICtrlCreateButton("Hinzufügen", 472, 104, 89, 17)
$btnRemove = GUICtrlCreateButton("Entfernen", 472, 128, 89, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("Options", 16, 272, 569, 153)
;###########################
$cBox[1] = GUICtrlCreateCheckbox("Sicherung beim Start wiederherstellen", 40, 296, 353, 17)
GUICtrlSetState(-1,$GUI_DISABLE)
$cBox[2] = GUICtrlCreateCheckbox("Sandbox Container in der Taskleiste anzeigen", 40, 320, 353, 17)
GUICtrlSetState(-1,$GUI_DISABLE)
$cBox[3] = GUICtrlCreateCheckbox("Sandbox Container nach beenden des Programmes automatisch sichern", 40, 344, 401, 17)
GUICtrlSetState(-1,$GUI_DISABLE)
$cBox[4] = GUICtrlCreateCheckbox("Schreibschutz für Sicherung aktivieren", 40, 368, 289, 17)
GUICtrlSetState(-1,$GUI_DISABLE)
$cBox[5] = GUICtrlCreateCheckbox("beim überschreiben der Sicherung nachfragen", 40, 392, 289, 17)
GUICtrlSetState(-1,$GUI_DISABLE)
; Noch nicht implementiert
;###########################
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group3 = GUICtrlCreateGroup("", 16, 424, 569, 41)
$btnSave = GUICtrlCreateButton("Save Config", 56, 440, 129, 17)
GUICtrlSetState(-1,$GUI_DISABLE)
$btnBackup = GUICtrlCreateButton("Backup", 224, 440, 145, 17)
$btnRestore = GUICtrlCreateButton("Restore", 416, 440, 137, 17)
If FileExists($ZipName) = 0 Then GUICtrlSetState(-1,$GUI_DISABLE)
$Progress1 = GUICtrlCreateProgress(16, 469, 569, 13)
GUICtrlSetState(-1,$GUI_DISABLE)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$hRestoreGUI = GUICreate("Restore Files", 319, 363)
$RestoreGUIRestore = GUICtrlCreateButton("Restore", 32, 320, 97, 17)
$RestoreGUIClose = GUICtrlCreateButton("Close", 160, 320, 97, 17)
$Group1 = GUICtrlCreateGroup("", 16, 296, 281, 57)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("", 16, 16, 281, 273)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW,$hGUI)
GUISetState(@SW_HIDE,$hRestoreGUI)
#EndRegion ### END Koda GUI section ###

While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			Exit
		Case $btnAdd
			_ContrainerAdd()
		Case $btnRemove
			_ContainerRemove()
		Case $btnEditPfad
			_ChangeRootDir()
		Case $btnBackup
			_ContrainerBackup()
		Case $btnRestore
			_ContrainerRestore()
		Case $RestoreGUIClose
			GUISetState(@SW_ENABLE,$hGUI)
			GUISetState(@SW_HIDE,$hRestoreGUI)
		Case $RestoreGUIRestore
			_RestoreFiles()
	EndSwitch
WEnd


Func _RestoreFiles()
	Local $FileList = _Zip_List(@ScriptDir &'\'&$ZipName)
	GUICtrlSetState($RestoreGUIRestore,$GUI_DISABLE)
	For $i = 1 To UBound($cBoxRestore)-1
		if GUICtrlRead($cBoxRestore[$i]) = $GUI_CHECKED Then
			;Restore Files
			_Zip_Unzip(@ScriptDir & "\"&$ZipName,$FileList[$i],@ScriptDir)
		EndIf
	Next
	MsgBox(0,"Restore Compleated","")
	GUICtrlSetState($RestoreGUIRestore,$GUI_ENABLE)
EndFunc
Func _ContrainerBackup()
	GUICtrlSetState($btnBackup,$GUI_DISABLE)
	Local $Data[11]
	Local $hZip = _Zip_Create(@ScriptDir & "\"&$ZipName) ;Create The Zip File. Returns a Handle to the zip File
	Local $Count = _GUICtrlListBox_GetCount($hListBox)
	For $i = 1 To $Count
		GUICtrlSetData($Progress1,($i*100)/$Count)
		$Data[$i] = _GUICtrlListBox_GetText($hListBox, $i-1)
		If $Data[$i] <> "" Then
		_Zip_AddFolder($hZip,$Data[$i],4) ;Add a folder to the zip file (files/subfolders will be added)
		Endif
	Next
	GUICtrlSetData($Progress1,0)
	GUICtrlSetState($btnBackup,$GUI_ENABLE)
EndFunc

Func _ContrainerRestore()
	GUISetState(@SW_DISABLE,$hGUI)
	GUISetState(@SW_SHOW,$hRestoreGUI)
	Local $Abstand = 30
	Local $FileList = _Zip_List(@ScriptDir &'\'&$ZipName)
	For $i = 1 To UBound($FileList)-1
		$cBoxRestore[$i] = GUICtrlCreateCheckbox($FileList[$i],30,$Abstand,200,20)
		$Abstand += 20
	Next
EndFunc
Func _ContrainerAdd()
	Local $newContrainer = FileSelectFolder("Bitte Contrainer auswählen",$RootDir)
	If _GUICtrlListBox_GetCount($hListBox) < 4 Then
	_GUICtrlListBox_AddString($hListBox,$newContrainer)
	Endif
EndFunc
Func _ChangeRootDir()
	Local $newRootDir = FileSelectFolder("Bitte Contrainer auswählen",$RootDir)
	GUICtrlSetData($iPfad,$newRootDir)
	$RootDir = $newRootDir
EndFunc
Func _ContainerRemove()
	Local $iIndex = _GUICtrlListBox_GetCurSel($hListBox)
	_GUICtrlListBox_DeleteString($hListBox, $iIndex)
EndFunc



