﻿B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=12.8
@EndOfDesignText@
Sub Class_Globals
	Private Root As B4XView 'ignore
	Private xui As XUI 'ignore
	Private info As Label
	Private FileHandler1 As FileHandler
	Dim DBName As String = Main.DBName
	'Dim DBName As String = "sccontrino.png"
	Dim Fname() As String = Array As String(DBName)
	Dim dialog As B4XDialog
End Sub

'You can add more parameters here.
Public Sub Initialize As Object
	Return Me
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
	Root = Root1
	'load the layout to Root
	Root.LoadLayout("backup")
	FileHandler1.Initialize
End Sub
'
Private Sub B4XPage_Appear
	info.Text = Main.NomeNegozio
End Sub

Sub B4XPage_CloseRequest As ResumableSub
	Return False
End Sub
'
Private Sub esci_Click
	B4XPages.ShowPageAndRemovePreviousPages("cruscotto1")
End Sub
'
Private Sub Label2_Click
	Dim strFilenameInGdrive As String = "backup_" & DateTime.Date(DateTime.Now) & ".zip"
	strFilenameInGdrive = strFilenameInGdrive.Replace("/", "_")
	Dim zip As Archiver
	zip.AsyncZipFiles (Main.FolderName, Fname, Main.FolderName, "dati.zip", "zip")
	Wait For zip_ZipDone(CompletedWithoutError As Boolean, NbOfFiles As Int)
	If CompletedWithoutError = False Then
		ToastMessageShow("Errore su zip " & strFilenameInGdrive, True)
		Return
	End If
	Wait For (FileHandler1.SaveAs(File.OpenInput(Main.FolderName, "dati.zip"), "application/zip", strFilenameInGdrive)) Complete (Success As Boolean)
	If Success Then
		ToastMessageShow("Una copia di backup è stata aggiunta correttamente",True)
	Else
		ToastMessageShow("L'operazione non è stata completata, riprova",True)
	End If
End Sub
'
Private Sub HandleLoadResult(Result As LoadResult)
	If Result.Success Then
'		Log(Result.FileName)
'		Log(Result.MimeType)
'		Log(Result.RealName)
'		Log(Result.Size)
		Dim zip As Archiver
		For Each n As String In File.ListFiles(Main.FolderName)
			If 	n.Contains(".zip") Then
				If File.Exists(Main.FolderName, n) Then
					File.Delete(Main.FolderName, n)
				End If
			End If
		Next
		Wait For (File.CopyAsync(Result.Dir, Result.FileName, Main.FolderName, Result.RealName)) Complete (Success As Boolean)
		If Success = False Then
			ToastMessageShow("Errore recupero file", True)
			Return
		End If
		'Log("unzip " & Fname(0))
		zip.AsyncUnZipFiles (Main.FolderName, Result.RealName, Main.FolderName, Fname, "UnZip")
		Wait For UnZip_UnZipDone(CompletedWithoutError As Boolean, NbOfFiles As Int)
		If CompletedWithoutError = False Then
			ToastMessageShow("Errore unzip file", True)
			Return
		End If
		'HideLoading
		ToastMessageShow("File ripristinato", True)
	Else
		ToastMessageShow("Nessun file selezionato",True)
	End If
End Sub
'
Sub btnWholePositive As ResumableSub
	Dim esito As Boolean = False
	Dim oggi As String = DateTime.Date(DateTime.Now)
	Dim test As Int = oggi.SubString2(0, 2) * oggi.SubString2(3, 5) * oggi.SubString2(6, 10)
	Dim input As B4XInputTemplate
	dialog.Initialize(Root)
	input.Initialize
	input.lblTitle.Text = "Codice PIN"
	input.ConfigureForNumbers(False, False) 'AllowDecimals, AllowNegative
	Wait For (dialog.ShowTemplate(input, "OK", "", "CANCEL")) Complete (Result As Int)
	If Result = xui.DialogResponse_Positive Then
		Dim res As Int = input.Text 'no need to check with IsNumber
		If res = test Then
			esito = True
		End If
	End If
	Return esito
End Sub
'
Private Sub Label2_LongClick
	'ripristina backup
	Wait For (btnWholePositive) complete (esito As Boolean)
	If esito Then
		Wait For (FileHandler1.Load) Complete (Result2 As LoadResult)
		HandleLoadResult(Result2)
	Else
		ToastMessageShow("Codice PIN errato!", True)
		esci_Click
	End If
End Sub