
--BullsEyeRefx = -214615
--BullsEyeRefz = -121441
function LuaExportStart()

	
	--default_output_file = io.open(lfs.writedir().."/Logs/Export.log", "w")
	
	lfs.mkdir(lfs.writedir().."/Scripts/DCS_BRA/logs")
	log_file = io.open(lfs.writedir().."/Scripts/DCS_BRA/logs/export_self_bra.txt", "w")
	--log_BullsEyeRef = io.open(lfs.writedir().."/Scripts/DCS_BRA/logs/export_BullsEyeRef.txt", "w")
	--size_file = io.open(lfs.writedir().."/Scripts/logs/export_test_size.txt", "w")	

	package.path  = package.path..";"..lfs.currentdir().."/LuaSocket/?.lua"
	package.cpath = package.cpath..";"..lfs.currentdir().."/LuaSocket/?.dll"

	socket = require("socket")
	IPAddress = "127.0.0.1"
	Port = 31090

	MySocket = socket.try(socket.connect(IPAddress, Port))
	MySocket:setoption("tcp-nodelay",true) 

	

	--[[
	log_BullsEyeRef = io.open("C:\\python\\BullsEyeRef.txt","rb")
	lines = log_BullsEyeRef:lines()

-- Fetch all lines and add them to a table
	
	for line in lines do
		table.insert(lines, line)
	end
	
		--]]



	
end

function LuaExportBeforeNextFrame()
end

function LuaExportAfterNextFrame()

	--local POS = LoGetSelfData()
	--local bearing = getBearing(0,0,POS.Position.x,POS.Position.z)
	--local range = getDistance(0,0,POS.Position.x,POS.Position.z) -- meters
	--socket.try(MySocket:send(string.format("%.0f,%.0f,%.0f,%.0f\n",POS.Position.x,POS.Position.z,bearing,range)))

	
	 
end

function LuaExportStop()
if MySocket then 
		--socket.try(MySocket:send("exit"))
		MySocket:close()
	end
if log_file then
	log_file: close()
	log_file = nil
	end
		

end

function LuaExportActivityNextEvent(t)

	local tNext = t + 30.0
	local POS = LoGetSelfData()
	
	local file = io.open(lfs.writedir().."/Scripts/DCS_BRA/logs/BullsEyeRef.txt","r")
	local flines = {}
	local i = 0
	if file then
		for line in file:lines() do
		 i = i + 1
		 flines[i] = line
		end
		file:close()
	else
		error('file not found')
	end
	BullsEyeRefx = flines[1]
	BullsEyeRefz = flines[2]

	--log_BullsEyeRef: write(string.format("%.0f,%.0f\n",BullsEyeRefx,BullsEyeRefz))

	--local bullseye = coalition.getMainRefPoint(activePlayer.side)
	--referenceX = bullseye.x
	--referenceZ = bullseye.z

	if (POS) then
		local bearing = getBearing(BullsEyeRefx,BullsEyeRefz,POS.Position.x,POS.Position.z)
		local range = getDistance(BullsEyeRefx,BullsEyeRefz,POS.Position.x,POS.Position.z) -- meters
		socket.try(MySocket:send(string.format("%.0f,%.0f,%.0f,%.0f\n",POS.Position.x,POS.Position.z,bearing,range)))
		--log_file = io.open("C:/python/export_test.txt", "w")
		
	
		log_file: write(string.format("%.0f,%.0f\n",POS.Position.x,POS.Position.z))

		local lfs = require "lfs"
		local size = lfs.attributes (lfs.writedir().."/Scripts/DCS_BRA/logs/export_self_bra.txt", "size")
		--size_file: write(string.format("%.0f\n",size))
		if size > 500 then
			log_file: close()
			log_file = nil
			log_file = io.open(lfs.writedir().."/Scripts/DCS_BRA/logs/export_self_bra.txt", "w")
		end
		
		
		
	end

	tNext = tNext + 1.0
	return tNext
end



function getDistance(obj1PosX, obj1PosZ, obj2PosX, obj2PosZ)
	local xDiff = obj1PosX - obj2PosX
	local yDiff = obj1PosZ - obj2PosZ
	return math.sqrt(xDiff * xDiff + yDiff * yDiff)/1852 -- meters
end

function getBearing(obj1PosX, obj1PosZ, obj2PosX, obj2PosZ)
    local bearing = math.atan2(obj2PosZ - obj1PosZ, obj2PosX - obj1PosX)
    if bearing < 0 then
        bearing = bearing + 2 * math.pi
    end
    bearing = bearing * 180 / math.pi
    return bearing    -- degrees
end