require("luamods/command")
require("config/rsm_conf")
require("luamods/rsm_serials")



local staticEditor = {}

local pretty = require("pl.pretty")
local configPars = require("configParser")
local formatter = require("formatter")

local ConfigFilesFolderPath = "/etc/rsm/"

local sddFileName = "sddconf.xml"
local mddFileName = "mddconf.xml"
local dddFileName = "dddconf.xml"
local flddFileName = "flddconf.xml"
--camera handled in cddEditor
--"other" device type has no xml files
--gsm handled in gsmEditor
local serialConfFileName = "serialconf.xml"


function file_exists(name)
  local f=io.open(name,"r")
  if f~=nil then io.close(f) return true else return false end
end


function staticEditor:getXmlFiles()

  local retTable = {}

  if file_exists(ConfigFilesFolderPath..sddFileName) ~= nil then
    retTable["sddConf"] = configPars.parse(ConfigFilesFolderPath..sddFileName,"xml")
  end

  if file_exists(ConfigFilesFolderPath..mddFileName) ~= nil then
    retTable["mddConf"] = configPars.parse(ConfigFilesFolderPath..mddFileName,"xml")
  end

  if file_exists(ConfigFilesFolderPath..dddFileName) ~= nil then
    retTable["dddConf"] = configPars.parse(ConfigFilesFolderPath..dddFileName,"xml")
  end

  if file_exists(ConfigFilesFolderPath..flddFileName) ~= nil then
    retTable["flddConf"] = configPars.parse(ConfigFilesFolderPath..flddFileName,"xml")
  end


  if file_exists(ConfigFilesFolderPath..serialConfFileName) ~= nil then
    retTable["serialConf"] = configPars.parse(ConfigFilesFolderPath..serialConfFileName,"xml")
  end

  return retTable
end

function staticEditor:writeXmlFiles(filesTable)

  if filesTable["sddConf"] ~= nil then
    configPars.write(ConfigFilesFolderPath..sddFileName,"xml",filesTable["sddConf"])
    formatter:replaceSingleQuotes(ConfigFilesFolderPath..sddFileName)
  end

  if filesTable["mddConf"] ~= nil then
    configPars.write(ConfigFilesFolderPath..mddFileName,"xml",filesTable["mddConf"])
    formatter:replaceSingleQuotes(ConfigFilesFolderPath..mddFileName)
  end

  if filesTable["dddConf"] ~= nil then
    configPars.write(ConfigFilesFolderPath..dddFileName,"xml",filesTable["dddConf"])
    formatter:replaceSingleQuotes(ConfigFilesFolderPath..dddFileName)
  end

  if filesTable["flddConf"] ~= nil then
    configPars.write(ConfigFilesFolderPath..flddFileName,"xml",filesTable["flddConf"])
    formatter:replaceSingleQuotes(ConfigFilesFolderPath..flddFileName)
  end

  if filesTable["serialConf"] ~= nil then
    configPars.write(ConfigFilesFolderPath..serialConfFileName,"xml",filesTable["serialConf"])
    formatter:replaceSingleQuotes(ConfigFilesFolderPath..serialConfFileName)
  end

end




return staticEditor
