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

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

stateStrings = {}
stateStrings["00"] = "DISCOVERED"
stateStrings["01"] = "MAINTENANCE"
stateStrings["02"] = "OPERATIONAL"
stateStrings["03"] = "UNKNOWN"

statusStrings = {}
statusStrings["00"] = "OFFLINE"
statusStrings["01"] = "ONLINE"
statusStrings["02"] = "UNKNOWN"

statusStyle = {}
statusStyle["00"] = "background: #ff9090;"
statusStyle["01"] = "background: #90ff90;"
statusStyle["02"] = ""

typeStrings = {}
typeStrings["00"] = "IO"
typeStrings["01"] = "AC"
typeStrings["02"] = "ANALOG"
typeStrings["03"] = "ANALOG_IO"
typeStrings["04"] = "SNMP"
typeStrings["05"] = "MODBUS"
typeStrings["06"] = "DLMS"
typeStrings["07"] = "VEEDER_ROOT"
typeStrings["08"] = "CAMERA"
typeStrings["09"] = "OTHER"
typeStrings["0A"] = "GSM"
typeStrings["0B"] = "UNKNOWN"

siteConfig = command.ex(rsm_conf.rsmc_path..[[ GET: TAG_SITE_CFG]]) or ""
siteLabel = siteConfig:match([[TAG_SITE_LABEL=%"([^%"]+)%"]]) or "unknown"

devConfig = command.ex(rsm_conf.rsmc_path..[[ GET: TAG_DEV_CFG]]) or ""

content = ""

content = content .. [[
                    <div id="info">
                        <center><h1>]]..siteLabel..[[ / Devices</h1></center>
                    </div>
]]

if (siteConfig == "") or (devConfig == "") then
    -- Bail on error
    content = content .. [[
                    <div id="info">
                        <center>
                            <h2><img src="images/warning.png"/> Invalid response</h2>
                            The RSM client ]]..rsm_conf.rsmc_path..[[ is missing, not responding or returned an invalid reply.<br>
                            This may also happen if the site is currently experiencing a lot of traffic.
                        </center>
                    </div>
]]
    print(header..content..footer)
    return
end

content = content .. [[
                    <div>
                        <br>
                        <center>
                            <b>This is a list of all the discovered devices at this site.<br><br>
                            Click on the labels to manage individual devices.</b>
                        </center><br>

                        <table border="1" class="onetwo">
                            <tr>
                                <th>Device Label</th>
                                <th>Device Serial</th>
                                <th>Type</th>
                                <th>State</th>
                                <th>Status</th>
                            </tr>
]]

for dcfg in devConfig:gmatch("TAG_DEV_CFG (.-) TAG_VERSION") do
    devID = dcfg:match("TAG_DEV_ID=(%x+)") or "unknown"
    devType = dcfg:match("TAG_DEV_TYPE=(%x+)") or "0A"
    devStatus = dcfg:match("TAG_DEV_STATUS=(%x+)") or "02"
    devState = dcfg:match("TAG_DEV_STATE=(%x+)") or "03"
    devLabel = dcfg:match([[TAG_DEV_LABEL=%"(.-)%"]]) or "unknown"
    content = content .. [[
                            <tr>
                                <td><a href="rsm_device_edit.lua?devID=]]..devID..[[">]]..devLabel..[[<img src="images/spanner.png"/></a></td>
                                <td>]]..devID..[[</td>
                                <td>]]..typeStrings[devType]..[[</td>
                                <td>]]..stateStrings[devState]..[[</td>
                                <td style="]]..statusStyle[devStatus]..[[">]]..statusStrings[devStatus]..[[</td>
                            </tr>
]]
end

content = content .. [[
                        </table>
                    </div>

]]

print(header..content..footer)
