-- General modules
require("Core")
require("Tools")
 
-- Third-party libraries
local json = require("dkjson")
local inspect = require("inspect")
 
-- Functional modules
require("Filter")
 
-- Network modules
require("Generic")
 
local bridges = json.decode(readFile("Data/bridge.json"))         -- List of cross-network bridges
local mirrors = json.decode(readFile("Data/mirror.json"))         -- List of mirrored talking groups
local deliveries = json.decode(readFile("Data/delivery.json"))    -- List of statically routed talking groups
local reflectors = readReflectorMapFromFile("Data/reflector.db")  -- List of reflector mappings for DV4mini
 
 
function handleCallSession(kind, name, number, slot, flavor, source, destination)
  --[[
 
    *** This function is called during building routes by LinkManager
 
    Parameters:
      kind - kind of link, see LINK_TYPE_*
      name - name of link
      number - ID of link
      flavor - set of bits that defines call type, see SESSION_TYPE_FLAG_*
      source - source ID of call
      destination - destination ID of call
 
    Returns:
      REGISTRY_CONTINUE_APPENDING to continue building route list
      REGISTRY_STOP_APPENDING to stop building route list
 
  ]]--
 
  if filterCallSession(kind, name, number, slot, flavor, source, destination)
  then
    report("Call rejected by filtering rule (Filter.lua)")
    return REGISTRY_STOP_APPENDING
  end
 
  if (bit.band(flavor, SESSION_TYPE_FLAG_VOICE) ~= 0) and
     (bit.band(flavor, SESSION_TYPE_FLAG_GROUP) ~= 0)
  then
    local result1 = makeExtendedRouteForDV4mini(kind, name, number, destination)
    local result2 = makeRouteForDV4mini(kind, name, number, destination, reflectors)
    if (result1 == REGISTRY_STOP_APPENDING) or
       (result2 == REGISTRY_STOP_APPENDING)
    then
      return REGISTRY_STOP_APPENDING
    end
 
    if mirrors[destination] == nil
    then
      makeGlobalRouteForGroup(kind, destination)
    end
 
    makeRouteToMirrorGroup(name, destination, mirrors)
    makeStaticDeliveryRouteForGroup(kind, number, destination, deliveries)
    makeBridgedNetworkRouteForGroup(kind, name, number, destination, bridges)
 
    makeOnDemandRouteForGroup(kind, number, slot, destination, 600)
    makeAutomaticRouteForNationalGroup(kind, number, destination, 1)
   end
 
  if (bit.band(flavor, SESSION_TYPE_FLAG_DATA) ~= 0) and
     (bit.band(flavor, SESSION_TYPE_FLAG_GROUP) ~= 0) and
     (name ~= "FastForward")
  then
    makeRouteForFullFeaturedSpot(kind, name, number, destination)
    makeAutomaticRouteForNationalGroup(kind, number, destination, 1)
    makeStaticDeliveryRouteForGroup(kind, number, destination, deliveries)
  end
 
  -- Default return value
  return REGISTRY_CONTINUE_APPENDING
end