local naughty    = require("naughty")
local wibox      = require("wibox")
local gears      = require("gears")
local focused    = require("awful.screen").focused
local easy_async = require("awful.spawn").easy_async

local function factory(args)
  local args = args or {}
  local timeout     = args.timeout or 5
  local status_cmd  = args.status_cmd or "hubic status"
  local full_cmd    = args.full_cmd or "hubic status"
  local cloud   = { widget = wibox.widget.textbox() }
  local settings    = args.settings or function() end
  local followtag   = args.followtag or true
  local notification_preset = args.notification_preset or { font = "Monospace 10" }

  cloud.widget:set_markup("N/A")

  function cloud.show(tout)
    cloud.hide()

    if followtag then
      notification_preset.screen = focused()
    end

    cloud.notification = naughty.notify({
        text = cloud.notification_text,
        -- icon = cloud.icon
        timeout = 0,
        preset = notification_preset,
      })
  end

  function cloud.hide()
    if cloud.notification then
      naughty.destroy(cloud.notification)
    end
  end

  function cloud.attach(obj)
    obj:connect_signal("mouse::enter", function()
      cloud.show()
    end)

    obj:connect_signal("mouse::leave", function()
      cloud.hide()
    end)

    obj:connect_signal("button::press", function()
      cloud.update()
      cloud.show()
    end)
  end

  function cloud.update()
    local cmd = string.format(full_cmd)
    easy_async(cmd, function(response, stderr, reason, exit_code)
	local res = string.match(response, 'State: (.-)Up')
	res = res:gsub("%s+", "")
	res = string.gsub(res, "%s+", "")
        currentstatus = res or "No"
        cloud.notification_text = response
        widget = cloud.widget
        settings()
    end)
  end

  cloud.attach(cloud.widget)
  gears.timer {
    timeout   = timeout,
    autostart = true,
    callback  = cloud.update
  }

  cloud.update()

  return cloud
end

return factory
