require("luamods/command")
require("luamods/layout")
require("luamods/cgi")
require("config/rsm_conf")

header = layout.getHeader()
footer = layout.getFooter()

userLevel = db.getPriv(layout.username) or 1

-- Get the bus we are busy with configuring
bus = cgi.get.bus or cgi.post.bus or ""

if (userLevel == 1) then
    buttonControl = " disabled"
    inputControl = " readonly"
else
    buttonControl = ""
    inputControl = ""
end

-- REMOVE THIS
--todo parse config file in order to make it more configurable
ckd_devices_path = "/etc/rsm/ckdd/devices"



-- Hold table with parsed CK100 configuration files for this bus
local ck100Devices = {}
-- Get all the CK100 devices in device directory
local allDeviceFiles = command.getFilesInTable(ckd_devices_path)
for k,v in pairs(allDeviceFiles) do
    -- See if the bus matches the one we expect
    local devicePath = ckd_devices_path .. "/" .. v

    local deviceFile = command.ex([[cat ]]..devicePath) or ""
    local devPort = deviceFile:match([[<port>(.*)</port>]])

    if (devPort == bus) then
        -- Parse the content to show the items
        local deviceData = {}
        deviceData.file = v
        deviceData.port = devPort
        -- Parse the master serial
        deviceData.master = deviceFile:match([[<master>(.*)</master>]])
        deviceData.numSlaves = 3
        -- Parse all the configured slaves
        deviceData.slaveOne = deviceFile:match([[<slave1>(.*)</slave1>]])
        deviceData.slaveTwo = deviceFile:match([[<slave2>(.*)</slave2>]])
        deviceData.slaveThree = deviceFile:match([[<slave3>(.*)</slave3>]])
        table.insert(ck100Devices, deviceData)


           --   d.master = cfg:match([[<master>%s-(.-)%s-</master>]]) or "unknown"
  --  d.port = cfg:match([[<port>%s-(.-)%s-</port>]]) or bus
   -- d.owner = cfg:match([[<owner>%s-(.-)%s-</owner>]]) or "ckdd"
   -- d.slave = cfg:match([[<slave>%s-(.-)%s-</slave>]]) or "unknown"

    end
end


content = ""

content = content .. [[
                    <div id="info">
                        <div style="background:url(images/busconfig.png); background-size:32px; background-repeat:no-repeat; background-position: 20px center;">
                            <center><h1>CKD Configuration</h1></center>
                        </div>
                    </div>
]]

if (mismatch == 1) then
    -- Bail on error here
    content = [[
                    <div id="info">
                        <center>
                            <h2><img src="images/warning.png"/> Invalid bus</h2>
                            Either an invalid bus was specified, or the bus cannot be matched<br>
                            to a valid CKD entry in the serial config file "]]..[[".<br><br>
                            <a href="rsm_bus_config.lua">Back to bus configuration</a>
                        </center>
                    </div>
]]
    html = header..content..footer
    print(html)
    os.exit()
end


content = content .. [[
                    <b><br>
                    <center>
                        This is a list of all CKD devices on ]]..bus..[[<br><br>
                        Click on the device labels to manage them.</b>
                    </center><br>
                    <div>

]]

if (userLevel == 1) then
    content = content .. [[
                    <center><b>Note:</b> Editing this section requires administrator privileges.</center><br>
]]
end

if (#(ck100Devices) > 0) then
    content = content .. [[

                        <table class="onetwo" style="text-align: center;">
                            <tr>
                                <th>Master Serial</th>
                            </tr>
]]
    for i,dev in pairs(ck100Devices) do
        content = content .. [[
                            <tr>
                                <td style="text-align: left;"><a href="rsm_ckd_edit.lua?file=]]..dev.file .. [[&bus=]]..dev.port..[[">]]..dev.master..[[<img src="images/spanner.png" style="float: right;"></a></td>
                            </tr>

]]
    end
    content = content .. [[
                        </table>
]]
else
    content = content .. [[
                        <div class="formLayoutfull">
                            <center><b>There are no CKD devices configured on this bus.</b></center>
                        </div>
]]
end

content = content .. [[
                        <br>
                        <form action="rsm_ckd_edit.lua" method="post">
                            <input type="hidden" value="]]..bus..[[" name="bus"/>
                            <input type="button" value="Back to Bus Configuration" onclick="window.location.assign('rsm_bus_config.lua');"/>
                            <input type="submit" name="addButton" value="Add CKD Device"]]..buttonControl..[[/>
                        </form>
                    </div>
]]

print(header..content..footer)
