local layout = require("luamods/layout")
local cfg = require("config/datalogger_conf")
local cgi = require("luamods/cgi")
local cmd = require("luamods/command")

local datamodel = {}
datamodel.saved = false
datamodel.dbc_saved = false
datamodel.dbc_file_location = cfg.cando_dbc

if (cgi.post.file ~= nil) then
   cmd.log("Received a file")
   cmd.writefile(cfg.cando_dbc,cgi.post.file.data)
   cmd.ex(cfg.cando_restart)
   datamodel.dbc_saved = true
end



local mqtt_cfg_file = io.open(cfg.mqtt_bridge_conf,"r") or nil
datamodel.mqtt_server = ""
if mqtt_cfg_file ~= nil then
   for cfg_line in mqtt_cfg_file:lines() do
      if cfg_line:match("address") then
        datamodel.mqtt_server = cfg_line:match("address%s+(%S+)") 
      end
   end
   mqtt_cfg_file:close()
end

local dl_cfg_file = io.open(cfg.cando_conf,"r") or nil
datamodel.machine_type = ""
datamodel.machine_id = ""
if dl_cfg_file ~= nil then
   for cfg_line in dl_cfg_file:lines() do
      if cfg_line:match("machine_type") then
         datamodel.machine_type = cfg_line:match("machine_type%s*=%s*(%S+)")
         cmd.log("Found machine_type")
      end
      if cfg_line:match("machine_id") then
         datamodel.machine_id = cfg_line:match("machine_id%s*=%s*(%S+)")
         cmd.log("Found machine_id")
      end
   end
   dl_cfg_file:close()
end

local dbc_cfg_file = io.open(cfg.cando_dbc,"r") or nil
datamodel.dbc_description = "-"
datamodel.dbc_version = "-"
if dbc_cfg_file ~= nil then
   for cfg_line in dbc_cfg_file:lines() do
      if cfg_line:match("VERSION") then
         datamodel.dbc_description = cfg_line:match([[VERSION%s+"(.+)"]])
         cmd.log("Found DBC description")
      end
      if cfg_line:match([[BA_%s+"DatabaseVersion"]]) then
         datamodel.dbc_version = cfg_line:match([[BA_%s+"DatabaseVersion"%s+"(.+)"]])
         cmd.log("Found DBC Version")
      end
   end
   dbc_cfg_file:close()
end

if (cgi.post.submitButton == "Save and Apply") and (userLevel ~= 1) then
    datamodel.saved = true
    if datamodel.mqtt_server ~= cgi.post.mqttserver then
      datamodel.mqtt_server = cgi.post.mqttserver
      local curr_mqtt_conf = cmd.returnFileString(cfg.mqtt_bridge_conf)
      curr_mqtt_conf = curr_mqtt_conf:gsub("address%s+(%S+)","address "..datamodel.mqtt_server)
      cmd.writefile(cfg.mqtt_bridge_conf,curr_mqtt_conf)
      cmd.ex(cfg.mqtt_restart)
   end
    if (datamodel.machine_type ~= cgi.post.machinetype) or
       (datamodel.machine_id ~= cgi.post.machineid) then
         datamodel.machine_type = cgi.post.machinetype
         datamodel.machine_id = cgi.post.machineid
         local curr_dl_conf = cmd.returnFileString(cfg.cando_conf)
         curr_dl_conf = curr_dl_conf:gsub("machine_type%s*=%s*%S+","machine_type="..datamodel.machine_type)
         curr_dl_conf = curr_dl_conf:gsub("machine_id%s*=%s*%S+","machine_id="..datamodel.machine_id)
         cmd.writefile(cfg.cando_conf,curr_dl_conf)
         cmd.ex(cfg.cando_restart)
    end
    -- 
    -- Also output the new config here
    --
end




layout.renderLayout("templates/datalogger.html",datamodel)