local node = ThreadNode:extend('nodes.newdash', {
  threadFile = 'nodes/newdash_thread.lua',
  threadChannel = 'newdash',

  updateInterval = 60,
})

local weatherFont = love.graphics.newFont('fonts/weathericons-regular-webfont.ttf', 165)
local tempFont = love.graphics.newFont('fonts/Lato-Light.ttf', 120)
local timeFont = love.graphics.newFont('fonts/Lato-Thin.ttf', 330)
local dateFont = love.graphics.newFont('fonts/Lato-Light.ttf', 90)
local headerFont = love.graphics.newFont('fonts/Lato-Regular.ttf', 40)
local valueFont = love.graphics.newFont('fonts/Lato-Light.ttf', 45)
local atFont = love.graphics.newFont('fonts/Lato-Light.ttf', 35)

local weatherGlyphs = {
  snow = "",
  mist = "",
  clear = "",
  -- clouds = "",
  clouds = "", -- x---DDD
  drizzle = "",
}

function node:spejsiotData(node_id, endpoint, parameter)
  if self.state.spejsiot[node_id] and self.state.spejsiot[node_id]["$online"] and self.state.spejsiot[node_id][endpoint] and self.state.spejsiot[node_id][endpoint][parameter] ~= nil then
    return self.state.spejsiot[node_id][endpoint][parameter]
  else
    return nil
  end
end

function node:renderIOTState(node_id, endpoint, parameter, x, y)
  local rawValue =  self:spejsiotData(node_id, endpoint, parameter)
  if rawValue == true then
    love.graphics.setColor( 0, 1.0, 0 )
    love.graphics.printf("ON", x, y, 400, 'left')
  elseif rawValue == false then
    love.graphics.setColor( 1.0, 0, 0 )
    love.graphics.printf("OFF", x, y, 400, 'left')
  elseif rawValue == nil then
    love.graphics.setColor( 1.0, 0, 0 )
    love.graphics.printf("OFFLINE", x, y, 400, 'left')
  else
    love.graphics.setColor( 1.0, 1.0, 1.0 )
    love.graphics.printf(rawValue, x, y, 400, 'left')
  end
end

function node:render()
  love.graphics.setColor( 0, 0, 0 )
  love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())

  if self.state then
    love.graphics.setColor( 1.0, 1.0, 1.0 )

    if weatherGlyphs[self.state.weather] then
      love.graphics.setFont(weatherFont)
      love.graphics.print(weatherGlyphs[self.state.weather], 100, 340)
    else
      love.graphics.setFont(atFont)
      love.graphics.print(self.state.weather, 100, 370)
    end

    love.graphics.setFont(tempFont)
    love.graphics.printf(string.format("%d°", self.state.temperature), 350, 390, 270, 'center')

    love.graphics.setFont(headerFont)
    love.graphics.printf("Ambient:", 720, 380, 160, 'right')
    love.graphics.printf("Exhaust:", 720, 440, 160, 'right')
    love.graphics.printf("Pope:", 720, 500, 160, 'right')

    love.graphics.setFont(valueFont)

    if self:spejsiotData("d106e1", "environment", "degree") then
      love.graphics.printf(string.format(
        "%d° / %d%%RH",
        self:spejsiotData("d106e1", "environment", "degree"),
        self:spejsiotData("d106e1", "environment", "humidity")
        ), 900, 378, 400, 'left')
    else
      love.graphics.printf("?!", 900, 378, 400, 'left')
    end

    self:renderIOTState("c0dbe7", "relay", "on", 900, 438)
    self:renderIOTState("0eac42", "spin and blink", "on", 900, 498)

    love.graphics.setColor( 1.0, 1.0, 1.0 )
    love.graphics.setFont(headerFont)
    love.graphics.printf("at hackerspace:", 50, 593, 300, 'left')
    love.graphics.setFont(atFont)
    users = {}
    if self.state.at then
      users = lume.map(self.state.at.users, function(v) return v.login end)
      if self.state.at.unknown > 0 then
        users[#users + 1] = string.format("%d unknown creatures", self.state.at.unknown)
      end
      if self.state.at.kektops > 0 then
        users[#users + 1] = string.format("%d kektops", self.state.at.kektops)
      end
      if self.state.at.esps > 0 then
        users[#users + 1] = string.format("%d ESPs", self.state.at.esps)
      end
    end

    usersList = (table.concat(users, ', ') or 'nobody...')

    love.graphics.printf(usersList, 350, 598, 900, 'left')
  else
    love.graphics.setColor(1.0, 1.0, 1.0, 0.5)
    love.graphics.setFont(valueFont)
    love.graphics.printf("Loading...", 0, 530, love.graphics.getWidth(), "center")
  end

  love.graphics.setColor( 1.0, 1.0, 1.0 )
  love.graphics.setFont(timeFont)
  love.graphics.printf(os.date("%H:%M"), 50, -10, 850, 'center')
  love.graphics.setFont(dateFont)
  love.graphics.printf(os.date("%Y\n%m/%d"), 960, 80, 270, 'center')

end

return node
