-- ----------------------------------------------------------------------------
-- Copyright (C) 2012,2013 Nokia Gate5 GmbH Berlin
--
-- These coded instructions, statements, and computer programs contain
-- unpublished proprietary information of Nokia Gate5 GmbH Berlin, and
-- are copy protected by law. They may not be disclosed to third parties
-- or copied or duplicated in any form, in whole or in part, without the
-- specific, prior written permission of Nokia Gate5 GmbH Berlin.
-- ----------------------------------------------------------------------------
--      Authors: Fabian TP Riek, Dirk Huppermann
-- ----------------------------------------------------------------------------

function get_text_output( )		
	if output_type == "tts" then
		nonTTS = false
		preparations( )
		if getManeuverGroup then
			get_maneuver_group( )
		elseif usePT then
			get_public_transport( )
		else
			select_sentence_1( )
		end
		if double_command and not usePT then
			select_sentence_2( )
		end

		set_result_tts( )
		cleanup( )
		second_cmd_trigger = ""
		maneuver_1 = nil
		maneuver_2 = nil
	end
end

function get_voicefiles( )		
	if output_type == "audio" then
		nonTTS = true
		preparations( )
		select_sentence_1( )
		if double_command then
			select_sentence_2( )
		end
		set_result_wave( )
		cleanup( )
		second_cmd_trigger = ""
		maneuver_1 = nil
		maneuver_2 = nil
	end
end

function preparations( )

	-------------------------------------------------------------------------------
	
	-------------------------------------------------------------------------------

	distance_1 = ""			
	usedUnit_1 = ""			
	tts_street_1 = ""		
	tts_street_1_a = ""		
	tts_street_1_phoneme = ""		
	tts_signpost_1 = ""		
	tts_street_2 = ""		
	tts_signpost_2 = ""		
	command_id_1 = ""		
	command_id_2 = ""		
	exit_no_roundabout = ""	
	head_orientation = ""	
	ped_turn_no = ""		
	exit_number = ""		
	EXIST_result_sentence_2 = false
	waypoint_1 = ""
	stop_count = 0
	station_type = ""
	station_name = ""
	line_name = ""
	line_destination = ""
	station_company_name = ""
	transit_type = ""
	time_to_wait = 0
	next_station_name = ""

	second_cmd_trigger = ""	

	nG_cmd_1 = ""			
	nG_cmd_2 = ""			
	-------------------------------------------------------------------------------

	if trigger == "PEDESTRIAN_ANNOUNCEMENT" or trigger == "PEDESTRIAN_COMMAND" then
		guidance_mode = "pedestrian"
	end
	if maneuver_1 == nil then
		maneuver = base_maneuver_table
		maneuver_1 = base_maneuver_table
	elseif maneuver_1.id == "STOPOVER" then
		maneuver = base_maneuver_table
		maneuver_1 = base_maneuver_table
	else
		maneuver = maneuver_1
	end
	if maneuver_2 == nil then
		nextManeuver = base_maneuver_table
		maneuver_2 = base_maneuver_table
	elseif maneuver_2.id == "STOPOVER" then
		nextManeuver = base_maneuver_table
		maneuver_2 = base_maneuver_table
		maneuver_set_count = 1
	else
		nextManeuver = maneuver_2
	end

	if maneuver.road_class ~= nil then
		road_class = maneuver.road_class
	end

	if gps_location == nil then
		gps_location = { ["route"] = "", ["street_label"] = "", ["fow"] = "" }
	end

	useHTML = false
	usePT = false
	if trigger == "WEB" then
		if instruction_format == "html" then
			useHTML = true
		end
		if guidance_mode == "publicTransport" and ( maneuver_1.id == "ENTER_STATION" or maneuver_1.id == "ENTER_LINE"
		  or maneuver_1.id == "LEAVE_LINE" or maneuver_1.id == "LEAVE_STATION" or maneuver_1.id == "CHANGE_LINE" ) then
			usePT = true
		end
	end

	if maneuver.dist_to_unit_string == nil then
		maneuver.dist_to_unit_string = maneuver.dist_to_unit
		nextManeuver.dist_to_unit_string = nextManeuver.dist_to_unit
	end

	check_maneuver_table( maneuver )	
	check_maneuver_table( nextManeuver )

	check_voice_guidance_settings()




--~ 	if ( string.lower(maneuver.ng_language) == string.lower(main_attribute_array.ngLangCode) ) and use_nguidance then
--~ 		maneuver.nguidance = nG_prepositions[maneuver.ng_preposition].." "..maneuver.ng_name
--~ 		nextManeuver.nguidance = nG_prepositions[nextManeuver.ng_preposition].." "..nextManeuver.ng_name
--~ 	else
--~ 		maneuver.nguidance = ""
--~ 		nextManeuver.nguidance = ""
--~ 	end
--~ 		maneuver.nguidance = ""
--~ 		nextManeuver.nguidance = ""


    if ( maneuver_set_count == 2 ) then	    
        double_command = true
    else
		double_command = false
    end

	dgttsp_rules()

end

function check_maneuver_table( maneuver )
	for k,v in pairs(base_maneuver_table) do
		if maneuver[ k ] == nil or maneuver[ k ] == "" then
			maneuver[ k ] = v
		end
	end
end

function cleanup( )
	distance_1 = ""			
	usedUnit_1 = ""			
	tts_street_1 = ""		
	tts_signpost_1 = ""		
	tts_street_2 = ""		
	tts_signpost_2 = ""		
	command_id_1 = ""		
	command_id_2 = ""		
	exit_no_roundabout = "" 
	head_orientation = ""	
	ped_turn_no = ""		
	exit_number = ""		
	waypoint_1 = ""
end

function expand_dgttsp(token, result_sentence)

	if trigger ~= "WEB" then
		function nocase (s)	
		  s = string.gsub(s, "%a", function (c)
				return string.format("[%s%s]", string.lower(c),
											   string.upper(c))
			  end)
		  return s
		end

		token, count = string.gsub( token, "\/", " !BREAK! " )	

		
		--token, count = string.gsub( token, "Xi%s([A-Z])", "