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



local dddEditor = {}

local pretty = require("pl.pretty")
local formatter = require("formatter")
local configPars = require("configParser")
local dddPath = "/etc/rsm/ddd/devices/"

function dddEditor:getXmlFiles()

  local templatesFiles = command.getFilesInTable(dddPath)
  local retTable = {}
  count=1
  for key, fileName in ipairs(templatesFiles) do
    xmlForFile =  configPars.parse(dddPath..fileName,"xml")
    retTable[count] = xmlForFile
    count=count+1
  end
  return retTable

end

function dddEditor:writeXmlFiles(filesTable)


command.ex([[rm ]]..dddPath.."*.xml")
  --command.ex(rsm_conf.ddd_down)


  if filesTable[1] ~= nil then
    for key, fileInfo in ipairs(filesTable) do
      local  newSerial = rsm_serials.generateNewSerial()
      local newFile = newSerial..".xml"
      fileInfo["device"]["serial"].__value__ = newSerial
      configPars.write(dddPath..newFile,"xml",fileInfo)
      formatter:replaceSingleQuotes(dddPath..newFile)
    end
  else
    local  newSerial = rsm_serials.generateNewSerial()
    local newFile = newSerial..".xml"
    filesTable["device"]["serial"].__value__ = newSerial
    configPars.write(dddPath..newFile,"xml",filesTable)
    formatter:replaceSingleQuotes(dddPath..newFile)
  end

  --command.ex(rsm_conf.ddd_up)

end

return dddEditor
