local gears = require('gears')
local lain = require('lain')
local awful = require('awful')
local wibox = require('wibox')
local dpi = require('beautiful.xresources').apply_dpi

local my_table = awful.util.table or gears.table -- 4.{0,1} compatibility

local theme = require('theme.base_theme')

local function build_widget_list(widgets)
	local out = {}
	local last_bg = 'alpha'

	for _, widget in pairs(widgets) do
		table.insert(out, lain.util.separators.arrow_left(last_bg, widget.bg))

		local bg_widget = wibox.widget.background(wibox.container.margin(widget.widget, 5, 5), widget.bg)
		bg_widget.fg = '#000000'
		table.insert(out, bg_widget)

		last_bg = widget.bg
	end

	return out
end

-- MPD
--[[
local mpdicon = wibox.widget.imagebox()
theme.mpd = lain.widget.mpd({
    settings = function()
        mpd_notification_preset = {
            text = string.format("%s [%s] - %s\n%s", mpd_now.artist,
                   mpd_now.album, mpd_now.date, mpd_now.title)
        }

        if mpd_now.state == "play" then
            artist = mpd_now.artist .. " > "
            title  = mpd_now.title .. " "
            mpdicon:set_image(theme.widget_note_on)
        elseif mpd_now.state == "pause" then
            artist = "mpd "
            title  = "paused "
        else
            artist = ""
            title  = ""
            --mpdicon:set_image() -- not working in 4.0
            mpdicon._private.image = nil
            mpdicon:emit_signal("widget::redraw_needed")
            mpdicon:emit_signal("widget::layout_changed")
        end
        widget:set_markup(markup.fontfg(theme.font, "#e54c62", artist) .. markup.fontfg(theme.font, "#b2b2b2", title))
    end
})
]]
--

local info = build_widget_list(require(... .. '.info'))

function theme.at_screen_connect(s)
	-- Quake application
	s.quake = lain.util.quake({ app = awful.util.terminal })

	-- If wallpaper is a function, call it with the screen
	local wallpaper = theme.wallpaper
	if type(wallpaper) == 'function' then
		wallpaper = wallpaper(s)
	end
	gears.wallpaper.maximized(wallpaper, s, true)

	-- Tags
	awful.tag(awful.util.tagnames, s, awful.layout.layouts[1])

	-- Create a promptbox for each screen
	s.mypromptbox = awful.widget.prompt()
	-- Create an imagebox widget which will contains an icon indicating which layout we're using.
	-- We need one layoutbox per screen.
	s.mylayoutbox = awful.widget.layoutbox(s)
	s.mylayoutbox:buttons(my_table.join(
		awful.button({}, 1, function()
			awful.layout.inc(1)
		end),
		awful.button({}, 2, function()
			awful.layout.set(awful.layout.layouts[1])
		end),
		awful.button({}, 3, function()
			awful.layout.inc(-1)
		end),
		awful.button({}, 4, function()
			awful.layout.inc(1)
		end),
		awful.button({}, 5, function()
			awful.layout.inc(-1)
		end)
	))
	-- Create a taglist widget
	s.mytaglist = awful.widget.taglist(s, awful.widget.taglist.filter.all, awful.util.taglist_buttons)

	-- Create a tasklist widget
	s.mytasklist = awful.widget.tasklist({
		screen = s,
		filter = awful.widget.tasklist.filter.minimizedcurrenttags,
		buttons = awful.util.tasklist_buttons,
		style = {
			shape = function(cr, w, h)
				gears.shape.partially_rounded_rect(cr, w, h, false, false, true, true, 10)
			end,
		},
		layout = {
			spacing = 5,
			layout = wibox.layout.flex.horizontal,
		},
		widget_template = {
			{
				{
					id = 'text_role',
					widget = wibox.widget.textbox,
				},
				left = 10,
				right = 10,
				widget = wibox.container.margin,
			},
			id = 'background_role',
			widget = wibox.container.background,
		},
	})
	s.mytasklist = wibox.container.margin(s.mytasklist, 10, 10, 0, 0)

	-- Create the wibox
	s.mywibox =
		awful.wibar({ position = 'top', screen = s, height = dpi(19), bg = theme.bg_normal, fg = theme.fg_normal })

	-- Add widgets to the wibox
	s.mywibox:setup({
		layout = wibox.layout.align.horizontal,
		{ -- Left widgets
			layout = wibox.layout.fixed.horizontal,
			s.mylayoutbox,
			s.mytaglist,
			s.mypromptbox,
			--mpdicon,
			--theme.mpd.widget,
		},
		s.mytasklist, -- Middle widget
		--nil,
		gears.table.join({ -- Right widgets
			layout = wibox.layout.fixed.horizontal,
			wibox.container.margin(wibox.widget.systray(), 0, 5),
		}, info),
	})
end

return theme
