#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region
'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=exemploLista50Registros.zip
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
  
    Private SQL1 As SQL
    Private Nomes As List = Array As String("Lucas", "Miguel", "Alice", "Sofia", "João", "Helena", "Gabriel", "Isabella", "Bernardo", "Lorenzo")
    Private Sobrenomes As List = Array As String("Silva", "Santos", "Oliveira", "Souza", "Pereira", "Lima", "Carvalho", "Ferreira", "Ribeiro", "Almeida")
    Private Cidades As List = Array As String("São Paulo", "Rio de Janeiro", "Belo Horizonte", "Salvador", "Curitiba", "Porto Alegre", "Recife", "Fortaleza", "Brasília", "Manaus")
    Private Estados As List = Array As String("SP", "RJ", "MG", "BA", "PR", "RS", "PE", "CE", "DF", "AM")
    Private bitmaps As List = Array("pexels-photo-446811.jpeg", "pexels-photo-571195.jpeg", "pexels-photo-736212.jpeg", "pexels-photo-592798.jpeg")
    Private CLV1 As CustomListView
    Private lblNome As B4XView
    Private lblEndereco As B4XView
    Private lblTelefone As B4XView
    Private lblEmail As B4XView
    Private imvImagem As B4XImageView
  
End Sub
Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("1")
  
    #if b4j
        xui.SetDataFolder("b4x")
        sql.InitializeSQLite(xui.DefaultFolder, "dados.db", True)
    #Else
        SQL1.Initialize(xui.DefaultFolder, "dados.db", True)
    #End If
  
    Dim n As Long = DateTime.Now
  
    Wait for (GeraDadosAleatorios(5000)) Complete (success As Boolean)
  
    CarregarDados(CLV1.Size, 50)
  
    Log("O carregamento dos cartões levou: " & (DateTime.Now - n) & "ms")
  
    Log("Dados inseridos com sucesso.")
End Sub
Private Sub CLV1_ReachEnd
    ' Carrega o próximo bloco de 50 registros ao chegar ao final
    CarregarDados(CLV1.Size, 50)
End Sub
Sub CarregarDados(offset As Int, limite As Int)
    Dim rs As ResultSet = SQL1.ExecQuery2("SELECT * FROM contatos LIMIT ? OFFSET ?", Array As String(limite, offset))
    Do While rs.NextRow
        Dim nome As String = rs.GetString("nome")
        Dim email As String = rs.GetString("email")
        Dim telefone As String = rs.GetString("telefone")
        Dim localizacao As String = rs.GetString("localizacao")
        Dim imagem As String = rs.GetString("imagem")
      
        ' Adicione o registro ao CustomListView
        AdicionarItem(nome, email, telefone, localizacao, imagem)
    Loop
    rs.Close
End Sub
Sub AdicionarItem(nome As String, email As String, telefone As String, localizacao As String, imagem As String)
    Dim pnl As B4XView = xui.CreatePanel("")
    pnl.SetLayoutAnimated(0, 0, 0, CLV1.AsView.Width, 100dip)
    pnl.LoadLayout("card1") ' Certifique-se de ter um layout 'ItemLayout' configurado
    lblNome.Text = nome
    lblEndereco.Text = localizacao
    lblEmail.Text = email
    lblTelefone.Text = telefone
    imvImagem.Load(File.DirAssets, imagem)
    CLV1.Add(pnl, nome) ' Adiciona o painel ao CustomListView
End Sub
Private Sub CLV1_ItemClick (Index As Int, Value As Object)
    xui.MsgboxAsync(Value, "Nome")
End Sub
#Region FUNCOES
Sub CriaTabela As ResumableSub
    ' Cria a tabela no banco de dados SQLite
    SQL1.ExecNonQuery("CREATE TABLE IF NOT EXISTS contatos (id INTEGER PRIMARY KEY AUTOINCREMENT, nome TEXT, email TEXT, telefone TEXT, localizacao TEXT, imagem TEXT)")
    Return True
End Sub
Sub GeraDadosAleatorios(quantidade As Int) As ResumableSub
  
    Wait for (CriaTabela) Complete (success As Boolean)
  
    Dim nome, sobrenome, email, telefone, cidade, estado, localizacao, imagem As String
    Dim i As Int
  
    SQL1.BeginTransaction
    Try
        For i = 1 To quantidade
            nome = Nomes.Get(Rnd(0, Nomes.Size))
            sobrenome = Sobrenomes.Get(Rnd(0, Sobrenomes.Size))
            email = nome.ToLowerCase & "." & sobrenome.ToLowerCase & "@exemplo.com"
            telefone = GeraTelefone
            cidade = Cidades.Get(Rnd(0, Cidades.Size))
            estado = Estados.Get(Rnd(0, Estados.Size))
            localizacao = cidade & " - " & estado
            imagem = bitmaps.Get(Rnd(0, bitmaps.Size))
          
            SQL1.ExecNonQuery2("INSERT INTO contatos (nome, email, telefone, localizacao, imagem) VALUES (?, ?, ?, ?, ?)", Array As String(nome & " " & sobrenome, email, telefone, localizacao, imagem))
        Next
        SQL1.TransactionSuccessful
    Catch
        Log(LastException)
        #if b4j or b4i
            SQL1.RollBack
        #End If
    End Try
  
    #if b4a
        SQL1.EndTransaction
    #End If
    Return True
End Sub
Sub GeraTelefone() As String
    ' Gera um número de telefone realista no formato (XX) XXXXX-XXXX
    Dim ddd As Int = Rnd(11, 99)  ' Código de área entre 11 e 99
    Dim numero As Int = Rnd(10000, 99999)
    Dim sufixo As Int = Rnd(1000, 9999)
    Return $"(${ddd}) ${numero}-${sufixo}"$
End Sub
#End Region