#Region  Project Attributes
    #ApplicationLabel: SpesaOK
    #Version: 1.0.0
    'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
    #iPhoneOrientations: Portrait
    #iPadOrientations: Portrait
    #Target: iPhone, iPad
    #ATSEnabled: True
    #MinVersion: 10
    #PlistExtra:<key>NSSpeechRecognitionUsageDescription</key><string>More information here...</string>
    #PlistExtra:<key>NSMicrophoneUsageDescription</key><string>Speech recognition</string>
    #QueriesSchemes: False
#End Region
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private Page2 As Page
    Dim picker1 As List
    Private pk1 As Picker
    Private web As WebView
    Private imgsplash As ImageView
    Dim timer1 As Timer
    Dim txtordine As TextField
    Private speech As SpeechRecognition
    Private pnlbase As Panel
    Private imgbocca As ImageView
    Private imgvolantino As ImageView
    Private imggomma As ImageView
    Private imgaggiungi As ImageView
    Private imginvio As ImageView
    Private TextView1 As TextView
    Private pnlvolantino As Panel
    Private imgback As ImageView
    Private imgsfondo As ImageView
    Private tableview1 As TableView
End Sub
Private Sub Application_Start (Nav As NavigationController)
    'SetDebugAutoFlushLogs(True) 'Uncomment if program crashes before all logs are printed.
    NavControl = Nav
    Page1.Initialize("Page1")   
    Page1.Title = "SpesaOK"
    Page1.RootPanel.Color = Colors.RGB (255,248,220)
    Page2.Initialize("Page2")
    Page2.Title = "SpesaOK"
    Page2.RootPanel.Color = Colors.RGB (255,248,220)
    NavControl.ShowPage(Page1)
    pk1.Initialize("pk1")
    web.Initialize("web")
    tableview1.Initialize("TableView1", False)
    Page1.RootPanel.AddView(tableview1, 1%x, 12%y, 98%x, 50%y)
    For i = 1 To 5
        Dim tc As TableCell = tableview1.AddTwoLines("", "")
        tc.IndentationLevel = Rnd(0, 5)
    
    Next
    imgsplash.Initialize("imgsplash")
    imgback.Initialize("imgback")
    imgsfondo.Initialize("imgsfondo")
    txtordine.Initialize("txtordine")
    pnlbase.Initialize("pnlbase")
    imgbocca.Initialize("imgbocca")
    imgvolantino.Initialize("imgvolantino")
    imggomma.Initialize("imggomma")
    imgaggiungi.Initialize("imgaggiungi")
    imginvio.Initialize("imginvio")       
    TextView1.Initialize("textview1")
    pnlvolantino.Initialize("pnlvolantino") 
    pnlvolantino.Visible=False   
    timer1.Initialize("timer1",2000)
    timer1.Enabled=True
    
    
    Page1.RootPanel.AddView(pnlbase,0%x, 0%y, 100%x, 85%y)
    Page1.RootPanel.AddView(imgsfondo,0%x,0%y,100%x,100%y)
    Page1.RootPanel.AddView(txtordine, 5%x,3%y,72%x,9%y)
    Page1.RootPanel.AddView(imgaggiungi,79%x,3%y,18%x,9%y)
    Page1.RootPanel.AddView(tableview1, 5%x, 14%y, 90%x, 48%y)
    Page1.RootPanel.AddView(imgvolantino, 5%x, 65%y, 25%x, 7%y)
    Page1.RootPanel.AddView(imgbocca, 34%x, 65%y, 18%x, 7%y)
    Page1.RootPanel.AddView(imggomma, 55%x, 65%y, 18%x, 7%y)
    Page1.RootPanel.AddView(imginvio, 77%x, 65%y, 18%x, 7%y)
    
    Page2.RootPanel.AddView(pnlvolantino,0%x, 0%y, 100%x, 85%y)
    Page2.RootPanel.AddView(web,5%x,3%y,90%x,50%y)
    Page2.RootPanel.AddView(pk1,5%x,48%y,90%x,20%y)
    Page2.RootPanel.AddView(imgback,40%x,65%y,20%x,10%y)
    picker1.Initialize
    pk1.SetItems(0,picker1)
    
    picker1.Add("CONAD")
    picker1.Add("EUROSPIN")
    picker1.Add("DESPAR")
    picker1.Add("SIGMA")
    picker1.Add("IL CENTESIMO")
    picker1.Add("LIDL")
    picker1.Add("PAGHI POCO")
    picker1.Add("FAMILA")
    picker1.Add("PENNY")
    picker1.Add("COOP")
    picker1.Add("ESSELUNGA")
    picker1.Add("AUCHAN")
    picker1.Add("A&O")
    picker1.Add("MD")
    picker1.Add("SIMPLY")
    picker1.Add("CARREFOUR")
    
    speech.Initialize("speech")
    
    imgsplash.Bitmap = LoadBitmap(File.DirAssets,"1200splashscreen.jpg")
    imgbocca.Bitmap = LoadBitmap(File.DirAssets,"bocca.png")
    imgaggiungi.Bitmap = LoadBitmap(File.DirAssets,"aggiungi.png")
    imgvolantino.Bitmap = LoadBitmap(File.DirAssets,"volantino.png")
    imggomma.Bitmap = LoadBitmap(File.DirAssets,"gomma.png")
    imginvio.Bitmap = LoadBitmap(File.DirAssets,"INVIO.png")
    imgback.Bitmap = LoadBitmap(File.DirAssets,"back.png")
    imgsfondo.Bitmap= LoadBitmap(File.DirAssets,"listview398833.jpg")
    imgsfondo.SendToBack
    
End Sub
Sub Speech_AuthorizationStatusChanged (Status As Int)
    If speech.IsAuthorized Then
        Dim lang As String
        If speech.SupportedLanguages.IndexOf(speech.DefaultLanguage) > -1 Then
            lang = speech.DefaultLanguage
        Else
            lang = "it"
        End If
        If speech.SetLanguage(lang) = False Then
            txtordine.Text = "vocale non disponibile"
        Else
            txtordine.Text = "Inserisci un prodotto e premi aggiungi"
            
        End If
    Else
        txtordine.Text = "Non Autorizzato"
    End If
End Sub
Sub Speech_Result (Success As Boolean, IsFinal As Boolean, Texts As List)
    If Success Then
        txtordine.Text = Texts.Get(0)
        
    Else
        txtordine.Text = "Errore: " & LastException
    End If
    
End Sub
Sub pk1_ItemSelected (Column As Int, Row As Int)
    If Row = 0 Then
        web.Visible=True
        web.LoadUrl ("https://www.conad.it/ricerca-negozi.html")
    Else If Row = 1 Then
        web.Visible=True
        web.LoadUrl("https://www.eurospin.it/volantino-nazionale/")
    Else If Row = 2 Then
        web.Visible=True
        web.LoadUrl ("https://www.despar.it/it/")
    Else If Row = 3 Then
        web.Visible=True
        web.LoadUrl("https://www.supersigma.com/offerte")
    Else If Row = 4 Then
        web.Visible=True
        web.LoadUrl("http://www.ilcentesimo.com/sfoglia-il-volantino/")
    Else If Row = 5 Then
        web.Visible=True
        web.LoadUrl("https://www.lidl.it/contents/informazioni/i-nostri-volantini")
    Else If Row = 6 Then
        web.Visible=True
        web.LoadUrl("https://www.paghipoco.com/")
    Else If Row = 7 Then
        web.Visible=True
        web.LoadUrl ("https://www.famila.it/punti-vendita")
    Else If Row = 8 Then
        web.Visible=True
        web.LoadUrl("https://www.pennymarket.it/offerte/sfoglia-volantino")
    Else If Row = 9 Then
        web.Visible=True
        web.LoadUrl("https://www.e-coop.it/punti-vendita")
    Else If Row = 10 Then
        web.Visible=True
        web.LoadUrl("https://www.esselunga.it/cms/promozioni/volantini.html")
    Else If Row = 11 Then
        web.Visible=True
        web.LoadUrl("https://www.auchan.it/it-IT-it/Custom/Organa/PuntiVendita.aspx")
    Else If Row = 12 Then
        web.Visible=True
        web.LoadUrl("https://www.aeo.it/punti-vendita")
    Else If Row = 13 Then
        web.Visible=True
        web.LoadUrl("https://www.mdwebstore.it/")
    Else If Row = 14 Then
        web.Visible=True
        web.LoadUrl ("https://www.simplymarket.it/it-IT-it/Custom/Organa/PuntiVendita.aspx")
    Else If Row = 15 Then
        web.Visible=True
        web.LoadUrl("https://www.carrefour.it/volantini/")
    
        
    End If
End Sub
Sub timer1_Tick
    pnlbase.Visible=True
    imgsplash.Visible=False
    timer1.Enabled=False
End Sub
Private Sub Page1_Resize(Width As Int, Height As Int)
    
End Sub
Private Sub Application_Background
    
End Sub
Sub avc_Complete (Success As Boolean, ActivityType As String)
    Log($"Success: ${Success}, ActivityType: ${ActivityType}"$)
End Sub
Sub imgbocca_Click
    speech.StartRecording(True) 'report partial results
    txtordine.Text = "Sta registrando...."
End Sub
Sub imgvolantino_Click
    NavControl.ShowPage(Page2)
    pnlvolantino.Visible=True
    pnlbase.Visible=False
End Sub
Sub imggomma_Click
    txtordine.Text=""
End Sub
Sub imgaggiungi_Click
    speech.StopRecording
    Dim items As List = tableview1.GetItems(0)
    Dim tc As TableCell 'create a new item
    tc.InitializeSingleLine(txtordine.Text)
    items.InsertAt(0, tc)
    tableview1.ReloadSection(0)
    txtordine.Text=""
End Sub
Sub TableView1_SelectedChanged (SectionIndex As Int, Cell As TableCell)
    tableview1.ReloadSection(0)
    tableview1.RemoveCells(SectionIndex, tableview1.GetItems(SectionIndex).IndexOf(Cell), 1)
End Sub
Sub imginvio_Click
    Dim sb As StringBuilder
    sb.Initialize
    For Each tc As TableCell In tableview1.GetItems(0)
        sb.Append(tc.Text.ToString).Append(CRLF)
    Next
    Dim avc As ActivityViewController
    avc.Initialize("avc", Array(sb.ToString , ""))
    avc.Show(Page1, Page1.RootPanel)
End Sub
Sub imgback_Click
    NavControl.ShowPage(Page1)
    pnlvolantino.Visible=False
    pnlbase.Visible=True
    
End Sub