local component = require("component")
local computer = require("computer")
local term = require("term")

if not term.isAvailable() then
  computer.beep()
  os.exit()
end
if component.gpu.getDepth() < 4 then
  print("A tier 2 or higher GPU is required")
  os.exit()
end

local environmentalSensors = {}
for address, _ in component.list("warpdriveEnvironmentalSensor", true) do
  print("Wrapping " .. address)
  table.insert(environmentalSensors, component.proxy(address))
end

function textOut(x, y, text, fg, bg)
  if term.isAvailable() then
    local w, _ = component.gpu.getResolution()
    if w then
      component.gpu.setBackground(bg)
      component.gpu.setForeground(fg)
      component.gpu.set(x, y, text)
      component.gpu.setBackground(0x000000)
    end
  end
end


local noExit = true

if #environmentalSensors == 0 then
  computer.beep()
  textOut(1, 2, "No environmental sensor detected", 0xFFFFFF, 0xFF0000)
  noExit = false
end

local file = io.open("/etc/hostname")
local label
if file then
  label = file:read("*l")
  file:close()
else
  label = "" .. computer.address()
end

if noExit then
  local isRunning = true
  repeat
    for key, environmentalSensor in pairs(environmentalSensors) do
      local isSuccessWeather, currentWeather, nextWeatherSeconds = environmentalSensor.getWeather()
      local isSuccessWorldTime, day, hours, minutes, totalSeconds = environmentalSensor.getWorldTime()
      
      term.clear()
      textOut(1,  1, label .. " - Environmental sensor " .. key .. " of " .. #environmentalSensors, 0x0000FF, 0x00FF00)
      
      if isSuccessWeather then
        local colorText
        if currentWeather == "CLEAR" then
          colorText = 0xFFFF00
        elseif currentWeather == "RAIN" then
          colorText = 0x8080FF
        else
          colorText = 0xFF8080
        end
        textOut(1,  3, "Local weather is " .. currentWeather .. ", changing in " .. string.format("%d", math.floor(nextWeatherSeconds)) .. " s.", colorText, 0x000000)
      else
        -- show failure message
        textOut(1,  3, "Local weather is ? (" .. currentWeather .. ")", 0xFF0000, 0x000000)
      end
	    
      if isSuccessWorldTime then
        local colorText
        if hours >= 6 and hours < 18 then
          colorText = 0xFFFFFF
        else
          colorText = 0x808080
        end
        textOut(1,  5, "Day " .. string.format("%d", math.floor(day)), colorText, 0x000000)
      else
        -- show failure message
        textOut(1,  5, "Day ? (" .. day .. ")", 0xFF0000, 0x000000)
      end
      
      if isSuccessWorldTime then
        if hours >= 6 and hours < 18 then
          colorText = 0xFFFFFF
        else
          colorText = 0x808080
        end
        textOut(1,  7, "Local time is " .. string.format("%02d", hours) .. ":" .. string.format("%02d", minutes), colorText, 0x000000)
      else
        -- show failure message
        textOut(1,  7, "Local time is ? (" .. day .. ")", 0xFF0000, 0x000000)
      end
      
      os.sleep(1)
    end
  until not isRunning
end

textOut(1, 1, "", 0xFFFFFF, 0x000000)

print()
print()
print()
print()
print()
print()
print()
print()
print()
print()
print("Program closed")
