-- ----------------------------------------------------------------------------
-- Copyright (C) 2009 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.
-- ----------------------------------------------------------------------------

-- use_dgttsp_1 = use dynamically generated TTS prompts for maneuver 1
-- use_dgttsp_2 = use dynamically generated TTS prompts for maneuver 2
use_dgttsp_1 = false
use_dgttsp_2 = false

engine_mode = nil

function get_common_code( )
    
    engine_mode = nil
    internal_set_engine_mode("normal")
    dgttsp_rules()
    
    -- GPS triggers
    if trigger == "GPS_SIGNAL_LOST" or trigger == "GPS_SIGNAL_RESTORED" or trigger == "ROUTE_RECALCULATION" then
        process_gps_triggers()
    -- Maneuver triggers
    elseif trigger == "COMMAND" or trigger == "REMINDER1" or trigger == "REMINDER2" or trigger =="ANNOUNCEMENT" then
        process_car_triggers()
    -- Safety SpotWarner 
    elseif trigger == "SAFETYSPOT_WARNER" then
            add_wav_file ( wav_files[ "beep_sound" ] )
            if above_speed_limit == true then
                table.insert(result_list, misc[ "safety_camara_ahead_wav" ] )
            end    
    -- Speed Warners triggers
    elseif trigger == "SPEED_WARNER" then
        table.insert(result_list, misc[ "over_speed_limit_wav" ] )
    -- Traffic Warner
    elseif trigger == "TRAFFIC_WARNER" then
        if avoidance_mode == "AUTOMATIC_AVOIDANCE" then
            table.insert(result_list, misc[ "traffic_automatic_detour_wav" ] )
        elseif avoidance_mode == "MANUAL_AVOIDANCE" then
            table.insert(result_list, misc[ "traffic_manual_detour_wav" ] )
        end
    -- Traffic detour failed
    elseif trigger == "TRAFFIC_DETOUR_FAILED" then
        table.insert(result_list, misc[ "no_detour_possible" ] )
    -- Pedestrian command
    elseif trigger == "PEDESTRIAN_COMMAND" or trigger == "PEDESTRIAN_ANNOUNCEMENT" then
        process_pedestrian_triggers()
    end
    
    -- Add "." at the end of last element from table "result_list"
    result_list[#result_list] = result_list[#result_list] .. misc[ "dot" ]

end

function process_gps_triggers()

    if guidance_mode ~= "pedestrian" then
        if trigger == "GPS_SIGNAL_LOST" then
            -- "GPS signal lost"
            table.insert( result_list, misc[ "gps_signal_lost_wav" ]) 
        end
        if trigger == "GPS_SIGNAL_RESTORED" then
            -- "GPS connection has been restored"
            table.insert( result_list, misc[ "gps_signal_restored_wav" ] )
        end
    end

 --   if trigger == "NO_GPS_SIGNAL" then
        -- "No GPS signal"
 --       table.insert( result_list, misc[ "no_gps_signal_wav" ] )
 --   end

    if trigger == "ROUTE_RECALCULATION" then
        -- "Route recalculation"
        table.insert( result_list, misc[ "route_recalculation_wav" ] )
    end
end


function dgttsp_rules()
--------------------------------------------------------------------------
-- Rules when to use DGTTSP (dynamically generated TTS prompts)
--------------------------------------------------------------------------
    
    -- Non Highway
    if road_class == "STREET" then
        
        if ( trigger == "ANNOUNCEMENT" and maneuver_1.dist_to <= 500 ) or trigger == "REMINDER1" then
            use_dgttsp_1 = true
            use_dgttsp_2 = false
        elseif trigger == "COMMAND" and double_command == true  then 
            use_dgttsp_1 = false
            use_dgttsp_2 = true
        else
            use_dgttsp_1 = false
            use_dgttsp_2 = false
        end
        
    -- Highway
    else
        
        if ( trigger == "ANNOUNCEMENT" and  maneuver_1.dist_to <= 650  ) or trigger == "REMINDER1" then
            use_dgttsp_1 = true
            use_dgttsp_2 = false
        elseif trigger == "COMAND" and double_command == true then
            use_dgttsp_1 = false
            use_dgttsp_2 = true
        else
            use_dgttsp_1 = false
            use_dgttsp_2 = false
        end
        
    end
end

function  internal_add_result( result )
    -- Add result to the list    
    if type( result ) == "table" then
        for j,t in pairs( result ) do
            table.insert( result_list, t )
        end
    else
        table.insert( result_list, result )
    end
end

function internal_set_engine_mode( mode )
    if engine_mode ~= mode then
            table.insert( result_list, "\\tn=" .. mode .. "\\" )
            engine_mode = mode
    end 
end

function add_result( result )
    if result ~= nil then
        internal_set_engine_mode( "normal" )
        internal_add_result( result )
    end
end

function add_address_result( result )
    if result ~= nil then
        internal_set_engine_mode( "address" )
        internal_add_result( result )
    end
end

function add_wav_file ( file )
    if file ~= nil then
        table.insert( result_list, '\\audio="' .. audio_files_path_absolute .. "\\" .. file .. '"\\' )
    end
end
    
    