#include <Array.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>
#include <GuiListBox.au3>
#include <Zip.au3>



Global $hListBox, $hGUI, $cBox[6]
Global $RootDir = @ComputerName, $ZipName = "SandieBoxBackup.zip"
Global $cBoxRestore[11]
$ReadPfad = IniRead('cfg.ini','Config','RestoreDir','')
#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, 13, 569, 249)
$lPfad = GUICtrlCreateLabel("Pfad für Restore", 32, 33, 94, 17)
$lCombo = GUICtrlCreateLabel("Backup File", 32, 55, 94, 17)
$iPfad = GUICtrlCreateInput("", 144, 30, 321, 21)
GUICtrlSetData(-1,$ReadPfad)
GUICtrlSetState(-1,$GUI_DISABLE)
$btnEditPfad = GUICtrlCreateButton("Edit", 472, 30, 89, 20)
$hCombo = GUICtrlCreateCombo("",144,55,321,21)
$hListBox = _GUICtrlListBox_Create($hGUI,"",45, 100, 420, 150)
_LoadZipFiles()
$Group4 = GUICtrlCreateGroup("Liste der SandBox Container", 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()
		Case $hCombo
			_ContrainerLoad()
	EndSwitch
WEnd


Func _Exit()
	IniWrite('cfg.ini','Config','RestoreDir',GUICtrlRead($iPfad))
	For $i = 1 To 10
		If _GUICtrlListBox_GetText($hListBox, $i-1) = '' Then
			IniWrite('cfg.ini','Contrainer','Contrainer'&$i,'')
		Else
			IniWrite('cfg.ini','Contrainer','Contrainer'&$i,_GUICtrlListBox_GetText($hListBox, $i-1))
		EndIf
	Next
	Exit
EndFunc

Func _LoadZipFiles()
	Local $FileList = _FileListToArray(@ScriptDir,'*.Zip')
	Local $FileString = _ArrayToString($FileList)
	Local $Trim = StringTrimLeft($FileString,2)
	GUICtrlSetData($hCombo,$Trim)
EndFunc
Func _ContrainerLoad()
	Local $Section = IniReadSection('cfg.ini','Contrainer')
	For $i = 1 To UBound($Section)-1
		If $Section[$i][1] <> '' Then _GUICtrlListBox_AddString($hListBox,$Section[$i][1])
	Next
EndFunc
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],GUICtrlRead($iPfad))
		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)
	GUICtrlSetState($btnRestore,$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) < 11 Then
		If $newContrainer <> '' Then _GUICtrlListBox_AddString($hListBox,$newContrainer)
	Endif
EndFunc
Func _ChangeRootDir()
	Local $newRootDir = FileSelectFolder("Bitte Contrainer auswählen",$RootDir)
	GUICtrlSetData($iPfad,$newRootDir)
EndFunc
Func _ContainerRemove()
	Local $iIndex = _GUICtrlListBox_GetCurSel($hListBox)
	_GUICtrlListBox_DeleteString($hListBox, $iIndex)
EndFunc



