B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Activity
Version=9.3
@EndOfDesignText@
#Region  Activity Attributes 
	#FullScreen: False
	#IncludeTitle: False
#End Region

'Activity module
Sub Process_Globals
	Dim GPS As GPS
	Dim rp As RuntimePermissions
	Dim ruta As String
'	Dim skmt As SQL
	Dim c As Cursor
	Dim c2 As Cursor
	Dim c22 As Cursor
	Dim c3 As Cursor
End Sub

Sub Globals
	Private gmap As GoogleMap
	Private MapFragment1 As MapFragment
	Dim Latitud As Double = 0
	Dim Longitud As Double = 0
	Dim Lat2 As Double = 0
	Dim Lon2 As Double = 0
	Dim p1, p2 As Location
	Dim Distance As Float
	Dim boton1 As Button
	Dim HUE_BLUE As Float
	Dim HUE_RED As Float
	Dim HUE_GREEN As Float
	Private B_AZUL As Button
	Private B_ROJO As Button
	Private B_VERDE As Button
	Private B_TODOS As Button
	Dim Tienda As String
	Dim LatitudRU As String
	Dim LongitudRU As String
	Dim LIST_AZUL As List
	Dim LIST_ROJO As List
	Dim LIST_VERDE As List
	Dim MARK_AZUL As Marker
	Dim MARK_ROJO As Marker
	Dim MARK_VERDE As Marker
	Dim MARK_CEDIS As Marker
	Dim rojo As String
	Dim azul As String
	Dim verde As String
	Dim todos As String
	Dim NumSerie As Int
	Dim OnInfoWindowClickListener1 As OnInfoWindowClickListener
	Dim GoogleMapEXTRA As GoogleMapsExtras
	Dim CODIGO As String
	Private SEMANA As String
	Private p_header As Panel
	Private p_mapa As Panel
	
	
	Dim Canvas1 As Canvas
	Dim Counter As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
	Activity.LoadLayout("MAPA_RUTAS")
	ruta = File.DirInternal
'	If File.Exists(ruta, "kmt.db") = False Then
'		File.Copy(File.DirAssets, "kmt.db", ruta, "kmt.db")
'	End If
'	skmt.Initialize(ruta,"kmt.db", True)
	'GPS
	If(FirstTime) Then
		GPS.Initialize("GPS")
	End If
	If MapFragment1.IsGooglePlayServicesAvailable = False Then
		ToastMessageShow("Please install Google Play Services.", True)
	End If
	
	'Boton velocidad'
	boton1.Initialize(0)
	boton1.Text = 0 &" "&"km/h"
	boton1.TextColor = Colors.Red
	boton1.TextSize = 15
	Activity.AddView(boton1, 40%x, 5dip, 25%x, 40dip)
	'Fin Boton velocidad'
	'MARK_CEDIS.IsInitialized
	MARK_AZUL.IsInitialized
	MARK_ROJO.IsInitialized
	MARK_VERDE.IsInitialized
	
	LIST_AZUL.Initialize
	LIST_ROJO.Initialize
	LIST_VERDE.Initialize
	verde = 0
	azul = 0
	rojo = 0
	todos = 1
	c=Starter.skmt.ExecQuery2("select count(*) AS CUANTOS from CAT_VARIABLES WHERE CAT_VA_DESCRIPCION = ?", Array As String ("SEMANA"))
	c.Position =0
	SEMANA = c.GetString("CUANTOS")
	c.Close
	If SEMANA > 0 Then
		c=Starter.skmt.ExecQuery2("select CAT_VA_VALOR from CAT_VARIABLES WHERE CAT_VA_DESCRIPCION = ?", Array As String ("SEMANA"))
		c.Position =0
		SEMANA = c.GetString("CAT_VA_VALOR")
		c.Close
	End If
'	p_header.Width = Activity.Width
	p_mapa.Width = Activity.Width
	p_mapa.GetView(0).SetLayout(0, 0, p_mapa.Width, p_mapa.Height)
End Sub

Sub Activity_Resume
	If GPS.GPSEnabled = False Then
		ToastMessageShow("Debe Activar el GPS del Equipo.", True)
		StartActivity(GPS.LocationSettingsIntent)
	Else
		GPS.Start(19.4316281, -99.1333579)
	End If
'	Subs.ultimaActividad(Me)
End Sub

Sub MapFragment1_Ready
	gmap = MapFragment1.GetMap
	LongitudRU = 19.4316281
	LatitudRU = -99.1333579
	
'	todos= 1
	'permisos
	rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
	Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
	gmap.MyLocationEnabled = Result

	Dim JavaMapsObject As JavaObject
	JavaMapsObject = gmap.GetUiSettings
	JavaMapsObject.RunMethod("setMapToolbarEnabled", Array As Object(True))
	
	'''''''----------------------------									MARKER AZUL - POR VISITAR
	If azul = 1 Or todos = 1  Then
		c.IsInitialized
		c=Starter.skmt.ExecQuery2("select CAT_CL_CODIGO,CAT_CL_NOMBRE,CAT_CL_LONG,CAT_CL_LAT,CAT_CL_NUM_SERIEFISICO from kmt_info where CAT_CL_NOMBRE <> 'VENTA ABORDO' AND CAT_CL_CODIGO NOT IN (Select PE_CLIENTE from PEDIDO) AND CAT_CL_CODIGO NOT In (Select NV_CLIENTE from NOVENTA) AND gestion <> '4' AND CAT_CL_CODIGO NOT LIKE 'N%' and CAT_CL_DIAS_VISITA = (SELECT CAT_VA_VALOR FROM CAT_VARIABLES WHERE CAT_VA_DESCRIPCION = 'DIA_VISITA') and cat_cl_codigo <> 0 AND CAT_CL_TIPO_RUTA IN (?,?)  ORDER BY CAT_CL_NUM_SERIEFISICO", Array As String(SEMANA, "SEMANAL"))
'		skmt.Initialize(ruta,"kmt.db", True)
		Log($"Por visitar = ${c.RowCount}"$)
		If c.RowCount > 0 Then
			For i = 0 To c.RowCount -1
				c.Position = i
				LongitudRU = c.GetDouble("CAT_CL_LONG")
				LatitudRU = c.GetDouble("CAT_CL_LAT")
				CODIGO=c.GetString("CAT_CL_CODIGO")
				Tienda= c.GetString("CAT_CL_NOMBRE")
				NumSerie=c.GetInt("CAT_CL_NUM_SERIEFISICO")
				'MARK_AZUL = gmap.AddMarker2(LongitudRU,LatitudRu,Tienda, gmap.HUE_BLUE)
'				MARK_AZUL = gmap.AddMarker3(LatitudRU, LongitudRU, CODIGO, LoadBitmap(File.DirAssets, "marker-azul-" & NumSerie &".png"))
				MARK_AZUL = gmap.AddMarker3(LatitudRU,LongitudRU,CODIGO, Subs.CreateBitmapWithNumber(LoadBitmap(File.DirAssets, "marker-azul.png"), NumSerie))
				MARK_AZUL.Snippet = Tienda
			Next
			c .Close
			If MARK_AZUL.IsInitialized Then LIST_AZUL.Add(MARK_AZUL)
		End If
	End If

	'''''''----------------------------									MARKER VERDE - VENTA
	If verde = 1 Or todos = 1  Then
		c2.IsInitialized
		c2=Starter.skmt.ExecQuery("select CAT_CL_CODIGO, CAT_CL_NOMBRE,CAT_CL_LONG,CAT_CL_LAT,CAT_CL_NUM_SERIEFISICO from kmt_info WHERE CAT_CL_NOMBRE <>'VENTA ABORDO' AND CAT_CL_CODIGO IN (Select PE_CLIENTE from PEDIDO) AND CAT_CL_CODIGO NOT In (Select NV_CLIENTE from NOVENTA) and CAT_CL_DIAS_VISITA = (SELECT CAT_VA_VALOR FROM CAT_VARIABLES WHERE CAT_VA_DESCRIPCION = 'DIA_VISITA')")
		Log($"Con ventar = ${c2.RowCount}"$)
		For i = 0 To c2.RowCount -1
			c2.Position = i
			LongitudRU = c2.GetDouble("CAT_CL_LONG")
			LatitudRU = c2.GetDouble("CAT_CL_LAT")
			CODIGO=c2.GetString("CAT_CL_CODIGO")
			Tienda= c2.GetString("CAT_CL_NOMBRE")
			NumSerie=c2.GetInt("CAT_CL_NUM_SERIEFISICO")
			
			Log("Ponemos marcador verde")
'			Antes
'			MARK_VERDE = gmap.AddMarker3(LatitudRU,LongitudRU,CODIGO, LoadBitmap(File.DirAssets, "marker-verde-"&NumSerie&".png"))
'			Despues
			MARK_VERDE = gmap.AddMarker3(LatitudRU,LongitudRU,CODIGO, CreateBitmapWithNumber(LoadBitmap(File.DirAssets, "marker-verde.png"), i+350))

			MARK_VERDE.Snippet = Tienda
		Next
		c2 .Close
	End If
	
	'''''''----------------------------							MARKER ROJO - NO VENTA
	
	If rojo =1 Or todos = 1 Then
		c3.IsInitialized
		c3=Starter.skmt.ExecQuery("select CAT_CL_CODIGO, CAT_CL_NOMBRE,CAT_CL_LONG,CAT_CL_LAT ,CAT_CL_NUM_SERIEFISICO from kmt_info where CAT_CL_NOMBRE <>'VENTA ABORDO' AND CAT_CL_CODIGO In (Select NV_CLIENTE from NOVENTA )  AND CAT_CL_CODIGO NOT IN (Select PE_CLIENTE from PEDIDO)  and CAT_CL_DIAS_VISITA = (SELECT CAT_VA_VALOR FROM CAT_VARIABLES WHERE CAT_VA_DESCRIPCION = 'DIA_VISITA')order by CAT_CL_NUM_SERIEFISICO")
		Log($"Sin venta = ${c3.RowCount}"$)
		For i = 0 To c3.RowCount -1
			c3.Position = i
			LongitudRU = c3.GetDouble("CAT_CL_LONG")
			LatitudRU = c3.GetDouble("CAT_CL_LAT")
			CODIGO=c3.GetString("CAT_CL_CODIGO")
			Tienda= c3.GetString("CAT_CL_NOMBRE")
			NumSerie=c3.GetInt("CAT_CL_NUM_SERIEFISICO")
			MARK_ROJO = gmap.AddMarker3(LatitudRU,LongitudRU,CODIGO, LoadBitmap(File.DirAssets, "marker-rojo-" & NumSerie &".png"))
			MARK_ROJO.Snippet = Tienda
		Next
		c3.Close
	End If
	'''------------------------------

'	MARK_CEDIS = gmap.AddMarker3("19.3961802","-99.0784293","CEDIS", LoadBitmap(File.DirAssets, "marker-azul-0.png"))
'	If  MARK_VERDE.Visible Or MARK_ROJO.Visible  Then
'		MARK_CEDIS.Remove
'	End If
	If LatitudRU > -1 Then
	LongitudRU = 19.4316281
	LatitudRU = -99.1333579
	End If
	Dim aa As CameraPosition
	aa.Initialize(LongitudRU,LatitudRU,10)''' RECOMENDABLE CAMBIAR A 10 PARA QUE SE VEAN MAS MARCADORES
	gmap.AnimateCamera(aa)
	
	'''''---------------------- ESTO ES PARA LOS CLICK EN LAS VENTANAS DE INFORMACION-----------
	Dim OnInfoWindowClickListener1 As OnInfoWindowClickListener
	OnInfoWindowClickListener1.Initialize("OnInfoWindowClickListener1")
	GoogleMapEXTRA.SetOnInfoWindowClickListener(gmap, OnInfoWindowClickListener1)
End Sub
'
''''--------------------------				PRUEBA CON MARKER _CLICK

Sub OnInfoWindowClickListener1_click(Marker1 As Marker)
	Starter.skmt.ExecNonQuery("delete from CUENTAA")
	Starter.skmt.ExecNonQuery2("INSERT INTO CUENTAA VALUES (?) ", Array As Object(Marker1.Title))
	Activity.Finish
	Subs.iniciaActividad("Cliente")
End Sub

Sub GPS_LocationChanged (Parametro As Location)
	gmap = MapFragment1.GetMap
	MARK_CEDIS.IsInitialized
	Dim sp As Int
	sp = Ceil(Parametro.Speed * 3.6)
	boton1.Text = sp &" "&"km/h"
	Latitud = Parametro.Latitude
	Longitud = Parametro.Longitude
	p2.Initialize2(Latitud,Longitud)
	p1.Initialize2(Lat2, Lon2)
	Distance  = p1.DistanceTo(p2)
'	If Latitud <> 0 And Longitud <> 0 Then
'		If Distance > 10 Then
'			Lat2 = Latitud
'			Lon2 = Longitud
'			Dim cp As CameraPosition
'			cp.Initialize2(Parametro.Latitude, Parametro.Longitude, gmap.CameraPosition.Zoom, Parametro.Bearing, 0)
'			gmap.AnimateCamera(cp)
'		End If
'	End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
	GPS.Stop
End Sub

Sub B_TODOS_Click
	todos = 1
	verde = 0
	azul = 0
	rojo = 0
	If gmap.IsInitialized Then gmap.Clear
	MapFragment1_Ready
End Sub

Sub B_VERDE_Click
	verde = 1
	azul = 0
	rojo = 0
	todos = 0
	If gmap.IsInitialized Then gmap.Clear
	MapFragment1_Ready
End Sub

Sub B_ROJO_Click
	rojo = 1
	verde = 0
	azul = 0
	todos = 0
	If gmap.IsInitialized Then gmap.Clear
	MapFragment1_Ready
End Sub

Sub B_AZUL_Click
	azul = 1
	verde = 0
	rojo = 0
	todos = 0
	If gmap.IsInitialized Then gmap.Clear
	MapFragment1_Ready
End Sub

Sub Activity_KeyPress (key As Int) As Boolean 'ignore
	' BACK key pressed
	If key=KeyCodes.KEYCODE_BACK Then
		Subs.iniciaActividad("Principal")
		Return False
		'End If
	End If
	' Returning False signals the system to handle the key
End Sub







Sub inicio
'	Dim Bitmap1, Bitmap2 As Bitmap
''	Dim Canvas1 As Canvas
''	Dim Counter As Int
'
'	Counter = 1
'
'	' Cargar la imagen del globito
'	Bitmap1 = LoadBitmap(File.DirAssets, "marker-azul-1.png")
'
'	' Crear un nuevo Bitmap con el número dibujado
'	Bitmap2 = CreateBitmapWithNumber(Bitmap1, Counter)
'	Counter = Counter + 1
'
'	' Agregar el marker con el Bitmap personalizado
'	Dim Marker1 As Marker
'	Marker1 = gmap.AddMarker2(Latitud1, Longitud1, Bitmap2)
'
'	' Repetir para el siguiente marker
'	Bitmap2 = CreateBitmapWithNumber(Bitmap1, Counter)
'	Counter = Counter + 1
'
'	Dim Marker2 As Marker
'	Marker2 = gmap.AddMarker2(Latitud2, Longitud2, Bitmap2)
End Sub

' Función para crear un Bitmap con el número dibujado
'Sub CreateBitmapWithNumber2(OriginalBitmap As Bitmap, Number As Int) As Bitmap
'	Dim NewBitmap As Bitmap
'	NewBitmap.InitializeMutable(OriginalBitmap.Width, OriginalBitmap.Height)
'    
'	Dim Canvas As Canvas
'	Canvas.Initialize2(NewBitmap)
'    
'	' Definir el rectángulo donde se dibujará el Bitmap
'	Dim DestRect(4) As Float
'	DestRect(0) = 0 ' Left
'	DestRect(1) = 0 ' Top
'	DestRect(2) = OriginalBitmap.Width ' Right
'	DestRect(3) = OriginalBitmap.Height ' Bottom
'    
'	' Dibujar la imagen original
'	Canvas.DrawBitmap(OriginalBitmap, Null, DestRect)
'    
'	' Configurar el estilo del texto
'	Canvas.DrawColor(Colors.Transparent) ' Fondo transparente
''	Canvas.TextSize = 24 ' Tamaño del texto
''	Canvas.DrawText( = "CENTER" ' Alinear el texto al centro
'	Canvas.DrawText(Number, (OriginalBitmap.Width / 2), (OriginalBitmap.Height / 2), Typeface.DEFAULT, 20, Colors.Blue, "CENTER")
'    
'	' Dibujar el número en el centro del Bitmap
''	Canvas.DrawText(Number, OriginalBitmap.Width / 2, OriginalBitmap.Height / 2)
'	Return NewBitmap
'End Sub

Sub CreateBitmapWithNumber(OriginalBitmap As Bitmap, Number As Int) As Bitmap
    Dim NewBitmap As Bitmap
    NewBitmap.InitializeMutable(OriginalBitmap.Width, OriginalBitmap.Height)
    
    Dim Canvas As Canvas
    Canvas.Initialize2(NewBitmap)
    
    ' Definir el rectángulo donde se dibujará el Bitmap
    Dim DestRect As Rect
    DestRect.Initialize(0, 0, OriginalBitmap.Width, OriginalBitmap.Height)
    
    ' Dibujar la imagen original
    Canvas.DrawBitmap(OriginalBitmap, Null, DestRect)
    
	' Dibujar el número en el centro del Bitmap y configurar el estilo del texto
	Canvas.DrawText(Number, (OriginalBitmap.Width / 2), (OriginalBitmap.Height / 2.4), Typeface.DEFAULT, 20, Colors.White, "CENTER")
	
	Return NewBitmap
End Sub                                                                      