--[[#############################################################################
GPS Position viewer v1.1
Copyright (C) by mosch   
License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html       
GITHUB: https://github.com/moschotto?tab=repositories


Description:

reads the "GPSpositions.txt" log-file which was generated by the GPS.lua script.
useful if want to check the last GPS coordinates after a crash, power loss etc. 

copy "GPSviewer.lua" to the /SCRIPTS/TOOLS/ folder

################################################################################]]


local toolName = "TNS|GPSTLog Viewer|TNE"
local log_filename = "/LOGS/GPSpositions.txt"
local file_exist = false
local string_gmatch = string.gmatch
local coordinates = {}  
local linectr = 0
local item = 0

local function splitstring(text)

	if text ~= nil then
		local text_split = {} 
		local i=0
		--split by "," and store into array/table
		for word in string_gmatch(text, "([^,]+)") do	
			text_split[i] = word
			i = i + 1			
		end	

		return text_split
	end 
end

local function Viewer_Draw_LCD(item)
  
  	lcd.clear() 	
	lcd.drawLine(0,0,0,95, SOLID, FORCE)	
	lcd.drawLine(127,0,127,95, SOLID, FORCE)
	lcd.drawText(2,1,"GPS Position viewer v1.1" ,SMLSIZE)		
	lcd.drawFilledRectangle(1,0, 126, 9, GREY_DEFAULT)
	
	
	lcd.drawLine(60,8, 60, 19, SOLID, FORCE)	
	lcd.drawText(64,11, "Sats:",SMLSIZE)	
	lcd.drawLine(97,8, 97, 19, SOLID, FORCE)
	lcd.drawLine(0,19, 128, 19, SOLID, FORCE)		
	lcd.drawText(100,11, (item + 1).."/" .. linectr,SMLSIZE)		
		
	if item < 4 then
		--first 4 rows static
		local line0 = splitstring(coordinates[0])
		local line1 = splitstring(coordinates[1])
		local line2 = splitstring(coordinates[2])
		local line3 = splitstring(coordinates[3])
	
		if item == 0 then
			lcd.drawText(2,11, "Time:" .. string.gsub(line0[3], "%s+", ""), SMLSIZE)
			lcd.drawText(64,11, "Sats:" .. string.gsub(line0[4], "%s+","") ,SMLSIZE)	
			lcd.drawText(2,24, line0[1] .."," .. line0[2] ,SMLSIZE + BLINK + INVERS)		
		else
			lcd.drawText(2,24, line0[1] .."," .. line0[2] ,SMLSIZE)
		end 
		
		if item == 1 then
			lcd.drawText(2,11, "Time:" .. string.gsub(line1[3], "%s+",""), SMLSIZE)
			lcd.drawText(64,11, "Sats:" .. string.gsub(line1[4], "%s+","") ,SMLSIZE)	
			lcd.drawText(2,44, line1[1] .."," .. line1[2] ,SMLSIZE + BLINK + INVERS)
		else
			lcd.drawText(2,44, line1[1] .."," .. line1[2] ,SMLSIZE)
		end  
		
		if item == 2 then
			lcd.drawText(2,11, "Time:" .. string.gsub(line2[3], "%s+",""), SMLSIZE)
			lcd.drawText(64,11, "Sats:" .. string.gsub(line2[4], "%s+","") ,SMLSIZE)	
			lcd.drawText(2,64, line2[1] .."," .. line2[2] ,SMLSIZE + BLINK + INVERS)
		else
			lcd.drawText(2,64, line2[1] .."," .. line2[2] ,SMLSIZE)
		end 
		
		if item == 3 then
			lcd.drawText(2,11, "Time:" .. string.gsub(line3[3], "%s+",""), SMLSIZE)
			lcd.drawText(64,11, "Sats:" .. string.gsub(line3[4], "%s+","") ,SMLSIZE)	
			lcd.drawText(2,84, line3[1] .."," .. line3[2] ,SMLSIZE + BLINK + INVERS)
		else
			lcd.drawText(2,84, line3[1] .."," .. line3[2] ,SMLSIZE)
		end  
	else
						
		local line0 = splitstring(coordinates[item-3])
		local line1 = splitstring(coordinates[item-2])
		local line2 = splitstring(coordinates[item-1])
		local line3 = splitstring(coordinates[item])
	
		lcd.drawText(2,11, "Time:" .. string.gsub(line3[3], "%s+",""), SMLSIZE)
		lcd.drawText(64,11, "Sats:" .. string.gsub(line3[4], "%s+","") ,SMLSIZE)	
		lcd.drawText(2,24, line0[1] .."," .. line0[2],SMLSIZE)
		lcd.drawText(2,44, line1[1] .."," .. line1[2],SMLSIZE)
		lcd.drawText(2,64, line2[1] .."," .. line2[2],SMLSIZE)
		lcd.drawText(2,84, line3[1] .."," .. line3[2],SMLSIZE + BLINK + INVERS)
		
	end 
	
	lcd.drawLine(0,95,127,95, SOLID, FORCE)		
	

end

local function Viewer_Init()
	
	lcd.clear() 
	local f2 = io.open(log_filename, "r")	
	
	--check if file exists 
	if f2 ~= nil then
			
		file_exist = true
		buffer = io.read(f2, 4096)
		io.close(f2)

		--read file contents into array/table
		for line in string_gmatch(buffer, "([^\n]+)\n") do			
			coordinates[linectr] = line		
			linectr = linectr + 1		
		end			
	
		--draw inital screen
		Viewer_Draw_LCD(0)
				
	else
		file_exist = false		
	end
 end


-- Main
local function Viewer_Run(event)
  
	if event == nil then
		error("Cannot be run as a model script!")
		return 2
	else		
		
		if file_exist == true then		
			
			Viewer_Draw_LCD(item)  
			--handle scroll counter
			if event == EVT_ROT_RIGHT or event == EVT_PLUS_FIRST then 			
				if item  < linectr-1 then
					item = item + 1
				end						  
			end				
			if event == EVT_ROT_LEFT or event == EVT_MINUS_FIRST then      
				if item > 0 then
					item = item - 1								
				end		
			end
		
		else
			--display error message
			lcd.clear() 
			lcd.drawLine(0,0,0,64, SOLID, FORCE)	
			lcd.drawLine(127,0,127,64, SOLID, FORCE)
			lcd.drawText(2,1,"GPS Postion viewer" ,SMLSIZE)		
			lcd.drawFilledRectangle(1,0, 126, 9, GREY_DEFAULT)
			lcd.drawText(10,20, "Logfile does not exist", SMLSIZE)	
			lcd.drawText(10,30, log_filename, SMLSIZE + BLINK)		
			lcd.drawLine(0,62, 128, 62, SOLID, FORCE)					
		end
		
		if event == EVT_VIRTUAL_EXIT then
			return 2
		end
								
	end
	return 0	
end

return { init=Viewer_Init, run=Viewer_Run }
