--[[#############################################################################
GPS stats viewer v1.2
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|GPS Stats X9|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,64, SOLID, FORCE)	
	lcd.drawLine(211,0,211,64, SOLID, FORCE)
	lcd.drawText(2,1,"GPS stats viewer v1.2" ,SMLSIZE)		
	lcd.drawFilledRectangle(1,0, 210, 9, GREY_DEFAULT)
	
	lcd.drawText(2,16, "No# " ..(item + 1).." / " .. linectr,SMLSIZE)	
	
	
	lcd.drawText(60,11, "Sats:",SMLSIZE)	
	lcd.drawText(60,22, "ALT:",SMLSIZE)	
	lcd.drawText(130,11, "Time:", SMLSIZE)
	lcd.drawText(130,22, "Spd:",SMLSIZE)		
	
	lcd.drawLine(55,19, 212, 19, SOLID, FORCE)	
	lcd.drawLine(55,8, 55, 28, SOLID, FORCE) --vertical line
	lcd.drawLine(0,30, 212, 30, SOLID, FORCE) --horizontal line
	
	if linectr > 3 then	
		if item < 3 then
			--first 4 rows static
			local line0 = splitstring(coordinates[0])
			local line1 = splitstring(coordinates[1])
			local line2 = splitstring(coordinates[2])
			
			if item == 0 then
				lcd.drawText(130,11, "Time:" .. string.gsub(line0[3], "%s+",""), SMLSIZE)
				lcd.drawText(60,11, "Sats:" .. string.gsub(line0[4], "%s+","") ,SMLSIZE)	
				lcd.drawText(60,22, "ALT:" .. string.gsub(line0[5].."m", "%s+","") ,SMLSIZE)	
				lcd.drawText(130,22, "Spd:" .. string.gsub(line0[6].."kmh", "%s+","") ,SMLSIZE)	
				lcd.drawText(2,34, line0[1] .."," .. line0[2] ,SMLSIZE + BLINK + INVERS)
			else
				lcd.drawText(2,34, line0[1] .."," .. line0[2] ,SMLSIZE)
			end  
			
			if item == 1 then
				lcd.drawText(130,11, "Time:" .. string.gsub(line1[3], "%s+",""), SMLSIZE)
				lcd.drawText(60,11, "Sats:" .. string.gsub(line1[4], "%s+","") ,SMLSIZE)	
				lcd.drawText(60,22, "ALT:" .. string.gsub(line1[5].."m", "%s+","") ,SMLSIZE)	
				lcd.drawText(130,22, "Spd:" .. string.gsub(line1[6], "%s+","").."kmh" ,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(130,11, "Time:" .. string.gsub(line2[3], "%s+",""), SMLSIZE)
				lcd.drawText(60,11, "Sats:" .. string.gsub(line2[4], "%s+","") ,SMLSIZE)	
				lcd.drawText(60,22, "ALT:" .. string.gsub(line2[5].."m", "%s+","") ,SMLSIZE)	
				lcd.drawText(130,22, "Spd:" .. string.gsub(line2[6], "%s+","").."kmh" ,SMLSIZE)	
				lcd.drawText(2,54, line2[1] .."," .. line2[2] ,SMLSIZE + BLINK + INVERS)
			else
				lcd.drawText(2,54, line2[1] .."," .. line2[2] ,SMLSIZE)
			end  
		else
							
			local line0 = splitstring(coordinates[item-2])
			local line1 = splitstring(coordinates[item-1])
			local line2 = splitstring(coordinates[item])	
		
			lcd.drawText(130,11, "Time: " .. string.gsub(line2[3], "%s+", ""), SMLSIZE)
			lcd.drawText(60,11, "Sats:" .. string.gsub(line2[4], "%s+","") ,SMLSIZE)	
			lcd.drawText(60,22, "ALT:" .. string.gsub(line2[5].."m", "%s+","") ,SMLSIZE)	
			lcd.drawText(130,22, "Spd:" .. string.gsub(line2[6], "%s+","") .."kmh",SMLSIZE)		
			lcd.drawText(2,34, line0[1] .."," .. line0[2],SMLSIZE)
			lcd.drawText(2,44, line1[1] .."," .. line1[2],SMLSIZE)
			lcd.drawText(2,54, line2[1] .."," .. line2[2],SMLSIZE + BLINK + INVERS)
			
		end 
	else
		--display error message
		lcd.clear() 
		lcd.drawLine(0,0,0,64, SOLID, FORCE)	
		
		lcd.drawText(2,1,"GPS stats viewer" ,SMLSIZE)		
		lcd.drawFilledRectangle(1,0, 211, 9, GREY_DEFAULT)
		lcd.drawText(10,20, "more than 4 entries in GPS logfile needed", SMLSIZE + BLINK)			
		lcd.drawText(10,30, log_filename, SMLSIZE + BLINK)		
		lcd.drawLine(0,68, 128, 68, SOLID, FORCE)	
	
	end
		
	lcd.drawLine(0,62, 212, 62, 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		
			if not string.find(line, "Number")  then --exclude logfile headline		
				coordinates[linectr] = line		
				linectr = linectr + 1		
			end
		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_PLUS_FIRST or event == EVT_PLUS_REPT then 			
				if item  < linectr-1 then
					item = item + 1
				end						  
			end				
			if event == EVT_MINUS_FIRST or event == EVT_MINUS_REPT  then      
				if item > 0 then
					item = item - 1								
				end		
			end
			
			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 }
