local awful = require("awful")
local wibox = require("wibox")
local gears = require("gears")
local awmtk = require("awmtk")
local beautiful = require("beautiful")
local simplepam = require("simplepam")
local function read(path)
    local file = io.open(path,"r")
    if file then
        local answer = file:read("*a")
        file:close()
        return answer
    end
end
return function(args)
    local style = awmtk.style(awmtk.defaults,args.style or {},"lockscreen_")
    if not args.screen then
        error("Screen not specified")
    end
    local s = args.screen
    if not G_KeyboardLock then
        G_KeyboardLock = awful.keygrabber {
            mask_modkeys = true,
            mask_event_callback = true
        }
    end
    local function gen_password_prompt()
        G_KeyboardLock:start()
        local inputbox = style.inputbox("Enter password",{},{
            pre_callback = function()
                --Just before we activate the prompt, deactivate keygrabber
                G_KeyboardLock:stop()
            end,
            exe_callback = function(input,textbox)
                --Activate keygrabber before calling pam interface
                --This prevents any input while pam is checking password.
                G_KeyboardLock:start()
                if simplepam(input) then
                    s.lockscreen.visible = false
                    G_KeyboardLock:stop() 
                end
            end,
            highlighter = (args.obscure and function(b,a)
                --This adds a black fully transparent text tag to password field
                return "<span foreground='#00000000'>","</span>"
            end),
            prompt = "Password: ",
            history_max = 0,
            history_path = "/tmp/lockfakehist",
            done_callback = function()
                --Ensure that even if input is cancelled and if the lock screen is still visible, the lock would start.
                if (not G_KeyboardLock.is_running) and s.lockscreen.visible then
                    G_KeyboardLock:start()
                end
            end
        })
        return inputbox
    end
    s.lockscreen = awful.popup({
        widget = {
            {
                --Top bar 
                {
                    {
                        {
                            markup = "AwesomewWM",
                            widget = wibox.widget.textbox
                        }, 
                        {
                            widget = wibox.container.background
                        },
                        {
                            require("widgets.battery")({percentage = true}),
                            wibox.widget.textclock(),
                            widget = wibox.container.background,
                            layout = wibox.layout.fixed.horizontal
                        },
                        layout = wibox.layout.align.horizontal,
                    },
                    widget = wibox.container.background,
                    forced_width = s.geometry.width,
                    forced_height = style.lockscreen_bar_height or 25,
                    bg = style.lockscreen_bar_bg or style.lockscreen_bg_normal
                },
                widget = wibox.container.place,
                valign = "top",
                halign = "center"
            },{
                --Combo box (face, nickname, password entry)
                style.container({
                    (read("./.face") and style.container {
                        widget = wibox.widget.imagebox,
                        image = "./.face",
                        resize = true,
                        forced_height = style.lockscreen_image_height or 128,
                        forced_width = style.lockscreen_image_width or 128
                    }),
                    style.container { 
                        widget = wibox.widget.textbox,
                        text = "Logged in as "..os.getenv("USER")
                    },
                    gen_password_prompt(),
                    layout = wibox.layout.fixed.vertical,
                    spacing = style.lockscreen_container_spacing_vertical
                }),
                widget = wibox.container.place
            },
            widget = wibox.container.background,
            layout = wibox.layout.stack,
            forced_width = s.geometry.width,
            forced_height = s.geometry.height,
        },
        hide_on_right_click = false,
        bgimage = function(context,cr,width,height)
            local surface = gears.surface(
                read("./.wallpaper") or beautiful.wallpaper
            )
            local bg_width,bg_height = gears.surface.get_size(surface)
            local scale = height/bg_height
            if width/bg_width > scale then
                scale = width/bg_width
            end
            cr:translate((width - (bg_width*scale))/2,(height - (bg_height*scale))/2)
            cr:rectangle(0,0, (bg_width*scale), (bg_height*scale))
            cr:clip()
            cr:scale(scale,scale)
            cr:set_source_surface(surface)
            cr:paint()
        end,
        visible = false,
        ontop = true
    })
    awesome.connect_signal("lock_screen",function()
        G_KeyboardLock:start()
        s.lockscreen.visible = true
    end)
    G_KeyboardLock:stop()
    return s
end
