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



local snmpEditor = {}


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


function snmpEditor:getXmlFiles()

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

end

function snmpEditor:writeXmlFiles(filesTable)

command.ex([[rm ]]..snmpPath.."*.xml")

  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
      --print(pretty.write(fileInfo))
      configPars.write(snmpPath..newFile,"xml",fileInfo)
      formatter:replaceSingleQuotes(snmpPath..newFile)
    end
  else
    local  newSerial = rsm_serials.generateNewSerial()
    local newFile = newSerial..".xml"
    filesTable["device"]["serial"].__value__ = newSerial
    configPars.write(snmpPath..newFile,"xml",filesTable)
      formatter:replaceSingleQuotes(snmpPath..newFile)
    --print(pretty.write(filesTable))
  end

end

return snmpEditor
