local m = {}

local channelStartId = 35000
local channelIdOffset = 10

local notificationDptIdToTypeUrn = {
  [0] = "de.gira.schema.channels.Monitoring.Bool",
  [1] = "de.gira.schema.channels.Monitoring.Percent",
  [2] = "de.gira.schema.channels.Monitoring.Byte",
  [3] = "de.gira.schema.channels.Monitoring.Float",
  [4] = "de.gira.schema.channels.Monitoring.String",
  [5] = "de.gira.schema.channels.Monitoring.Float",
  [6] = "de.gira.schema.channels.Monitoring.DWord",
  [7] = "de.gira.schema.channels.Monitoring.Integer"
}

local priorityMap = { [0] = "Low", [1] = "High", [2] = "Alarm" }

-- Notification are simple monitoring channels
function m.CreateNotificationDefiniton(number, config)
    local channel = device:AddChannel(string.format("Monitoring-%d", number))
    Logger:Debug(string.format("Create Channel: Monitoring-%d", number))
    helper.LogConfiguration("Notification " .. number, config)
    channel.TypeURN = notificationDptIdToTypeUrn[config.DptId]
    channel.FieldHandlerName = "MonitoringApp"
    channel.StartId = channelStartId + (channelIdOffset * (number - 1))
    channel:AddParameterSet("Monitoring", "Type", "GA-Value")
    channel:AddParameterSet("Monitoring", "Filter", config.Filter)
    channel:AddParameterSet("Monitoring", "NotifyOnlyOnChange", tostring(config.NotifyOnlyOnChange))
    channel:AddParameterSet("Monitoring", "Priority", priorityMap[config.Priority])
    channel:AddParameterSet("Monitoring", "Category", config.Category)
    channel:AddParameterSet("Monitoring", "Subject", config.Subject)
    channel:AddParameterSet("Monitoring", "Text", config.Text)
    if string.len(config.AttachmentURL) ~= 0 then
        channel:AddParameterSet("Monitoring", "AttachmentURL", config.AttachmentURL)        
    end
    if config.Threshold ~= nil then
        channel:AddParameterSet("Monitoring", "Threshold", config.Threshold)
    end
    if config.DptId == 3 or config.DptId == 5 then
      channel:AddParameterSet("Monitoring", "FloatingPointSeparator", config.FloatingPointSeparator < 1 and "." or ",")
    end
end

function m.Configure(config)
	Logger:Info("-- monitoring_module.lua: configure parameter")
    helper.LogConfiguration("Monitoring-App", config)
    local channel = device:AddChannel("Monitoring-App")
    channel.Enabled = config.EnabledBool
end

return m
