require "odious"

local oocairo = require "oocairo"

-- {{{ lualock settings

background("color", "#000000")

--im = image(utils.get_data_dir() .. "/simpleblack/archlinux-official-light.svg")
----im:scale(0.75, 0.75)
--im:set_position(utils.screen_width()/2 - 600/2, 0.2)
--im:show()

im = image(utils.get_data_dir() .. "/simpleblack/panel.png")
--im:scale(0.75, 0.75)
--im:set_position(utils.screen_width()/2 - 600/2, 0.2)
im:set_position(utils.screen_width()/2 - 600/2, utils.screen_height()/2 - 400/2)
im:show()

sxcenter = utils.screen_width()/2
sycenter = utils.screen_height()/2
input_top = sycenter + 50

--panel = image(utils.get_data_dir() .. "/simpleblack/panel_transp1.png")
--panel:set_position(utils.screen_width()/2 - 300/2, input_top-50)
--panel:show()

style{ color = "black", font = "Sans 24", 
        x = sxcenter - 150/2 - 24, 
        y = input_top + 50, 
        off_x = 10,
        off_y = 10, 
        width = 199, 
        height = 50,
        bg_color = 'rgba(0, 0, 0, 0)',
        border_color = 'rgba(0, 0, 0, 0)',
        border_width = 2
     }
prefs{ timeout = 10 * 60 }

user_text = odious.widget.text{ text = os.getenv("USER"), font = "Sans 24", color = "black",
                                x = sxcenter - 92,
                                y = input_top + 10,  
                                border_color = "black", border_width = 0
                              }
                                 
-- }}}

-- define functions for setting and restoring DPMS settings
local dpms = { 
    set = function (standby, suspend, off)
        utils.spawn(string.format("xset dpms %i %i %i", standby, suspend, off))
    end,
    get = function ()
        local xset_out = odious.util.pread("xset q")
        local standby, suspend, off =
            string.match(xset_out, "Standby: (%d+).*Suspend: (%d+).*Off: (%d+)")
        return tonumber(standby) or 0, tonumber(suspend) or 0, tonumber(off) or 0
    end 
}
-- {{{ Hooks
-- get current DPMS settings
local standby, suspend, off = dpms.get()

hook.connect("lock", function () 
    dpms.set(60, 120, 300)
end)

-- restore DPMS settings once we're done
hook.connect("unlock", function ()
    dpms.set(standby, suspend, off)
end)

-- show a dot for each failed login attempt
--evildot = utils.get_data_dir() .. "/glowydot.png"
--failed_attempts = 0
--hook.connect("auth-failed", function ()
--    local dot = image(evildot)
--    dot:set_position(500 + 20 * failed_attempts, 440)
--    dot:show()
--    failed_attempts = failed_attempts + 1
--end)
-- }}}

-- {{{ 

-- SYSTRAY
function update_systray() 

    print("systray " .. os.date("*t").min .. ":" .. os.date("*t").sec)

    right = utils.screen_width()
--    systray = image(right, 30)
--    systray:set_position(0, 0)
--    --systray:draw_rectangle(0, 0, 100, 100, true, "#ff0000")
--    systray:show()
    
    systray = odious.widget.progressbar{ width = right, height = 30, x = 0, y = 0 }
    systray:set_border_color("black"):set_vertical(false):set_color("#000000")
    systray:set_value(100)

    -- CLOCK
--    right = right - 175
    top = 3
                                    
    local update_clock = function()
        local time = os.date("*t")
        local sec = time.sec

        local file = assert(io.popen("date +\"%a, %b %e  %H:%M\"", 'r'))
        local output = file:read('*all')
        file:close()
        output = string.gsub(output, "\n", "")
        
        systray:set_value(100)
        local clock_text = odious.widget.text{ text = "" .. output, font = "Droid Sans 16", color = "#ffffff",
                                  x = utils.screen_width()/2 - 85,
                                  y = 5,  
                                  border_color = "#000000", border_width = 3 }
    end
    update_clock()

    -- BATTERY
    right = right - 45
    batt_box = odious.widget.progressbar{ width = 30, height = 16, x = right+0, y = top+1 }
    batt_box:set_border_color("#dddddd"):set_vertical(false):set_color("#dddddd")
    batt_box_pip = odious.widget.progressbar{ width = 3, height = 10, x = right+30, y = top+4 }
    batt_box_pip:set_border_color("#dddddd"):set_vertical(false):set_color("#dddddd")
    batt_box_pip:set_value(100)

    function update_battery ()
        local batt_info = odious.util.get_battery("BAT0")
        local level = batt_info.charge_now / batt_info.charge_full
--        level = math.random()
--        level = 100
        batt_box:set_value(level)
        
        if (level <= 0.15) then
            batt_box:set_color("#d70000")
        end

        if (level == 1) then
            level = ""
        else
            level = math.floor(level*100) .. "%"
        end
        local batt_text = odious.widget.text{ text = level .. "", font = "Sans 8", color = "#000000",
                                  x = right,
                                  y = top,  
                                  border_color = "#dddddd", border_width = 3 }
    end
    update_battery()
    
    -- network
    right = right - 35
        
    local file = assert(io.popen("nm-tool", 'r'))
    local output = file:read('*all')
    file:close()
    string.gsub(output, "\nState: (.-)\n", function(a)  print("[network]", a); state = a; end)
    if (state == "disconnected") then
        path = "nm-no-connection.svg"
    else
        path = "nm-signal-100.svg"
    end
    print("[path]", path)
    
    network_icon = image(utils.get_data_dir() .. "/simpleblack/" .. path)
    network_icon:scale(1, 1)
    network_icon:set_position(right, 0)
    network_icon:show()

end

local first_update = function ()
    update_systray()
    local systray_timer = timer(update_systray, 60)
    systray_timer:start()
end

-- align update_systray timer to the start of the minute
local once = function ()
    first_update()
    once_timer:stop()
    print("once timer stopped")
end

once_timer = timer(once, 60-os.date("*t").sec)
once_timer:start()

update_systray()

-- }}}