if not term.isColor() then
  print("Advanced computer required")
  error()
end

local sides = peripheral.getNames()
local environmentalSensors = {}
for _, side in pairs(sides) do
  if peripheral.getType(side) == "warpdriveEnvironmentalSensor" then
    print("Wrapping " .. side)
    table.insert(environmentalSensors, peripheral.wrap(side))
  end
end


local noExit = true

if #environmentalSensors == 0 then
  term.setBackgroundColor(colors.red)
  term.setTextColor(colors.white)
  print("No environmental sensor detected")

  noExit = false
end

local label = os.getComputerLabel()
if label then
else
  label = "" .. os.getComputerID()
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.setBackgroundColor(colors.black)
      term.setTextColor(colors.blue)
      term.clear()
      term.setBackgroundColor(colors.lime)
      term.setCursorPos(1, 1)
      term.write(label .. " - Environmental sensor " .. key .. " of " .. #environmentalSensors)
      term.setBackgroundColor(colors.black)
      
      term.setCursorPos(1, 3)
      if isSuccessWeather then
        if currentWeather == "CLEAR" then
          term.setTextColor(colors.yellow)
        elseif currentWeather == "RAIN" then
          term.setTextColor(colors.blue)
        else
          term.setTextColor(colors.orange)
        end
        term.write("Local weather is " .. currentWeather .. ", changing in " .. nextWeatherSeconds .. " s.")
      else
        -- show failure message
        term.setTextColor(colors.red)
        term.write("Local weather is ? (" .. currentWeather .. ").")
      end
      
      term.setCursorPos(1, 5)
      if isSuccessAtmosphere then
        if isBreathable then
          term.setTextColor(colors.green)
          term.write("Atmosphere is breathable (" .. airConcentration .. " air concentration).")
        else
          term.setTextColor(colors.orange)
          term.write("Atmosphere is NON-breathable (" .. airConcentration .. " air concentration).")
        end
      else
        -- show failure message
        term.setTextColor(colors.red)
        term.write("Atmosphere is ? (" .. isBreathable .. ").")
      end
      
      term.setCursorPos(1, 7)
      if isSuccessBiome then
        if biomeTag1 == nil then
          biomeTag1 = ""
        end
        if biomeTag2 == nil then
          biomeTag2 = ""
        end
        if biomeTag3 == nil then
          biomeTag3 = ""
        end
        term.setTextColor(colors.white)
        term.write("Biome is " .. biomeName .. " [" .. biomeTag1 .. " " .. biomeTag2 .. " " .. biomeTag3 .. " ...].")
      else
        -- show failure message
        term.setTextColor(colors.red)
        term.write("Biome is ? (" .. biomeName .. ").")
      end
      
      term.setCursorPos(1, 9)
      if isSuccessHumidity then
        if humidityCategory == "DRY" then
          term.setTextColor(colors.yellow)
        elseif humidityCategory == "WET" then
          term.setTextColor(colors.blue)
        else
          term.setTextColor(colors.white)
        end
        term.write("Humidity is " .. string.format("%.2f", biomeRainfall) .. " in " .. humidityCategory .. " category.")
      else
        -- show failure message
        term.setTextColor(colors.red)
        term.write("Humidity is ? (" .. humidityCategory .. ").")
      end
      
      term.setCursorPos(1, 11)
      if isSuccessTemperature then
        term.setTextColor(colors.white)
        term.write("Temperature is " .. string.format("%.3f", biomeTemperature) .. " in " .. biomeTemperatureCategory .. " category.")
      else
        -- show failure message
        term.setTextColor(colors.red)
        term.write("Temperature is ? (" .. biomeTemperatureCategory .. ").")
      end
      
      term.setCursorPos(1, 13)
      if isSuccessWorldTime then
        if hours >= 6 and hours < 18 then
          term.setTextColor(colors.white)
        else
          term.setTextColor(colors.lightGray)
        end
        term.write("World day is " .. day .. ".")
      else
        -- show failure message
        term.setTextColor(colors.red)
        term.write("World day is  ? (" .. day .. ").")
      end
      
      term.setCursorPos(1, 15)
      if isSuccessWorldTime then
        if hours >= 6 and hours < 18 then
          term.setTextColor(colors.white)
        else
          term.setTextColor(colors.lightGray)
        end
        term.write("Local time is " .. string.format("%02d", hours) .. ":" .. string.format("%02d", minutes) .. ".")
      else
        -- show failure message
        term.setTextColor(colors.red)
        term.write("Local time is ? (" .. day .. ").")
      end
      
      os.sleep(1)
    end
  until not isRunning
end

term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)

print()
print("Program closed")
