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 isSuccessAtmosphere, isBreathable, airConcentration = environmentalSensor.getAtmosphere()
      local isSuccessBiome, biomeName, biomeTag1, biomeTag2, biomeTag3 = environmentalSensor.getBiome()
      local isSuccessHumidity, humidityCategory, biomeRainfall = environmentalSensor.getHumidity()
      local isSuccessTemperature, biomeTemperatureCategory, biomeTemperature = environmentalSensor.getTemperature()
      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 isSuccessAtmosphere then
        if isBreathable then
          textOut(1, 5, "Atmosphere is breathable (" .. airConcentration .. " air concentration).", 0x8080FF, 0x8080FF)
        else
          textOut(1, 5, "Atmosphere is NON-breathable (" .. airConcentration .. " air concentration).", 0xFF8080, 0x000000)
        end
      else
        -- show failure message
        textOut(1, 5, "Atmosphere is ? (" .. isBreathable .. ").", 0xFF0000, 0x000000)
      end
      
      if isSuccessBiome then
        if biomeTag1 == nil then
          biomeTag1 = ""
        end
        if biomeTag2 == nil then
          biomeTag2 = ""
        end
        if biomeTag3 == nil then
          biomeTag3 = ""
        end
        textOut(1, 7, "Biome is " .. biomeName .. " [" .. biomeTag1 .. " " .. biomeTag2 .. " " .. biomeTag3 .. " ...].", 0xFFFFFF, 0x000000)
      else
        -- show failure message
        textOut(1, 7, "Biome is ? (" .. biomeName .. ").", 0xFF0000, 0x000000)
      end
      
      if isSuccessHumidity then
        if humidityCategory == "DRY" then
          colorText = 0xFFFF00
        elseif humidityCategory == "WET" then
          colorText = 0x8080FF
        else
          colorText = 0xFFFFFF
        end
        textOut(1, 9, "Humidity is " .. string.format("%.2f", biomeRainfall) .. " in " .. humidityCategory .. " category.", colorText, 0x000000)
      else
        -- show failure message
        textOut(1, 9, "Humidity is ? (" .. humidityCategory .. ").", 0xFF0000, 0x000000)
      end
      
      if isSuccessTemperature then
        textOut(1, 11, "Temperature is " .. string.format("%.3f", biomeTemperature) .. " in " .. biomeTemperatureCategory .. " category.", 0xFFFFFF, 0x000000)
      else
        -- show failure message
        textOut(1, 11, "Temperature is ? (" .. biomeTemperatureCategory .. ").", 0xFF0000, 0x000000)
      end
      
      if isSuccessWorldTime then
        local colorText
        if hours >= 6 and hours < 18 then
          colorText = 0xFFFFFF
        else
          colorText = 0x808080
        end
        textOut(1,  13, "Day " .. string.format("%d", math.floor(day)), colorText, 0x000000)
      else
        -- show failure message
        textOut(1,  13, "Day ? (" .. day .. ")", 0xFF0000, 0x000000)
      end
      
      if isSuccessWorldTime then
        if hours >= 6 and hours < 18 then
          colorText = 0xFFFFFF
        else
          colorText = 0x808080
        end
        textOut(1,  15, "Local time is " .. string.format("%02d", hours) .. ":" .. string.format("%02d", minutes), colorText, 0x000000)
      else
        -- show failure message
        textOut(1,  15, "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")
