local gears = require("gears")
local lain  = require("lain")
local awful = require("awful")
local wibox = require("wibox")
local os, math, string = os, math, string

local fonts = {
    mono_regular = "Fira Code Regular 8",
    mono_bold    = "Fira Code Bold 8"
}

fonts.widget = "Fira Code Bold 7"

local colors = {
    dark        = "#161616",
    light       = "#dedede",
    accent      = "#003056",
    accent_fg   = "#dedede",
    attention   = "#fc5d19",
    transparent = "#00000000",

    transparency = "22",
}

colors.background = colors.dark .. colors.transparency
colors.foreground = colors.light
colors.widget_bg  = colors.transparent 

local theme                                     = {}
theme.dir                                       = os.getenv("HOME") .. "/.config/awesome/themes/kadet"
theme.wallpaper                                 = theme.dir .. "/wallpapers/make.jpg"
theme.font                                      = fonts.mono_regular
theme.taglist_font                              = fonts.mono_bold
theme.tasklist_font                             = fonts.mono_regular

theme.bg_systray                                = "#000000"
theme.systray_icon_spacing                      = 3
theme.fg_normal                                 = colors.light
theme.bg_normal                                 = colors.dark .. "dd"
theme.fg_wibar                                  = colors.light
theme.bg_wibar                                  = colors.background
theme.fg_normal                                 = colors.light
theme.bg_focus                                  = colors.dark
theme.fg_focus                                  = colors.light
theme.bg_urgent                                 = colors.attention
theme.fg_urgent                                 = colors.light

theme.fg_widget                                 = colors.light

theme.taglist_bg_focus                          = colors.dark .. "aa"
theme.taglist_fg_focus                          = colors.acceng_fg
theme.taglist_bg_occupied                       = colors.dark .. "66"
theme.taglist_fg_occupied                       = colors.light
theme.taglist_bg_empty                          = colors.transparent
theme.taglist_fg_empty                          = colors.light
theme.taglist_bg_urgent                         = "#C92132"
theme.taglist_fg_urgent                         = "#282828"

theme.tasklist_bg_normal                        = "#32302f"
theme.tasklist_fg_normal                        = "#ebdbb2"
theme.tasklist_bg_focus                         = "#32302f"
theme.tasklist_fg_focus                         = "#ebdbb2"
theme.tasklist_bg_urgent                        = "#C92132"
theme.tasklist_fg_urgent                        = "#32302f"

theme.border_width                              = 0
theme.border_normal                             = "#32302f"
theme.border_focus                              = "#3f3f3f"
theme.border_marked                             = "#CC9393"

theme.titlebar_bg_normal                        = "#161616"
theme.titlebar_fg_normal                        = colors.white

theme.menu_height                               = 16
theme.menu_width                                = 140

theme.notification_font                         = fonts.mono_regular
theme.notification_bg                           = colors.dark .. "dd"
theme.notification_fg                           = theme.fg_normal
theme.notification_border_width                 = 1
theme.notification_border_color                 = colors.dark
-- theme.notification_shape                        = gears.shape.infobubble
theme.notification_opacity                      = 1
theme.notification_margin                       = 30
--theme.warning                                   = theme.dir .. "/icons/status/warning.png"
--theme.notification_width                        = 300
--theme.notification_height                       = 200

theme.menu_submenu_icon                         = theme.dir .. "/icons/submenu.png"
theme.awesome_icon                              = theme.dir .. "/icons/awesome.png"

-- Layout icons
theme.layout_tile                               = theme.dir .. "/icons/layout/tileright.png"
theme.layout_tileleft                           = theme.dir .. "/icons/layout/tileleft.png"
theme.layout_tilebottom                         = theme.dir .. "/icons/layout/tilebottom.png"
theme.layout_tiletop                            = theme.dir .. "/icons/layout/tiletop.png"
theme.layout_fairv                              = theme.dir .. "/icons/layout/fairv.png"
theme.layout_fairh                              = theme.dir .. "/icons/layout/fairh.png"
theme.layout_spiral                             = theme.dir .. "/icons/layout/spiral.png"
theme.layout_centerwork                         = theme.dir .. "/icons/layout/centerwork.png"
theme.layout_dwindle                            = theme.dir .. "/icons/layout/dwindle.png"
theme.layout_max                                = theme.dir .. "/icons/layout/max.png"
theme.layout_fullscreen                         = theme.dir .. "/icons/layout/fullscreen.png"
theme.layout_magnifier                          = theme.dir .. "/icons/layout/magnifier.png"
theme.layout_floating                           = theme.dir .. "/icons/layout/floating.png"
theme.layout_cornerse                           = theme.dir .. "/icons/layout/cprmerse.png"
theme.layout_cornersw                           = theme.dir .. "/icons/layout/cornersw.png"
theme.layout_cornerne                           = theme.dir .. "/icons/layout/cprmerne.png"
theme.layout_cornernw                           = theme.dir .. "/icons/layout/cornernw.png"

theme.tasklist_disable_icon                     = true
theme.useless_gap                               = 7

-- titlebar
theme.titlebar_close_button_focus               = theme.dir .. "/icons/titlebar/close.png"
theme.titlebar_close_button_normal              = theme.dir .. "/icons/titlebar/close-inactive.png"

theme.titlebar_ontop_button_focus_active        = theme.dir .. "/icons/titlebar/top.png"
theme.titlebar_ontop_button_focus_inactive      = theme.dir .. "/icons/titlebar/top-inactive.png"

theme.titlebar_sticky_button_focus_active       = theme.dir .. "/icons/titlebar/sticky.png"
theme.titlebar_sticky_button_focus_inactive     = theme.dir .. "/icons/titlebar/sticky-inactive.png"

theme.titlebar_floating_button_focus_active     = theme.dir .. "/icons/titlebar/floating.png"
theme.titlebar_floating_button_focus_inactive   = theme.dir .. "/icons/titlebar/floating-inactive.png"

theme.titlebar_maximized_button_focus_active    = theme.dir .. "/icons/titlebar/maximized.png"
theme.titlebar_maximized_button_focus_inactive  = theme.dir .. "/icons/titlebar/maximized-inactive.png"

local image = {}

function icon(dir, icon) return wibox.widget.imagebox(theme.dir .. "/icons/" .. dir .. "/" .. icon .. ".png") end

-- systray 
image.systray_start = icon("bar", "start")
image.systray_end   = icon("bar", "end")

image.icons = {
    cpu  = icon("widget", "cpu"),
    hdd  = icon("widget", "hdd"),
    mem  = icon("widget", "memory"),
    temp = icon("widget", "temp"),
}

-- colors
theme.green          = "#00b159"
theme.red            = "#d64d4d"
theme.yellow         = "#cc9c00"
theme.blue           = "#428bca"
theme.darkred        = "#c92132"
theme.darkgreen      = "#4d7358"
theme.darkyellow     = "#f18e38" 
theme.gray           = "#5e5e5e"
theme.violet         = "#8c8ccd"
theme.pink           = "#B85C8A"
theme.black          = theme.bg_normal

-- Panel
local markup = lain.util.markup

-- Widgets
local clock = awful.widget.textclock("<span font=\"" .. fonts.widget .. "\" color=\"" .. colors.light .. "\"> %a %d %b  %H:%M </span>")
local clock_widget = wibox.container.background(wibox.container.margin(wibox.widget { clock, layout = wibox.layout.align.horizontal }, 0, 1), colors.widget_bg)

-- Calendar
local calendar = lain.widget.calendar({
    cal = "cal -h",
    attach_to = { clock_widget },
    notification_preset = {
        font = fonts.mono_regular,
        fg   = theme.fg_normal,
        bg   = theme.bg_normal
    }
})

-- MEM
local mem = lain.widget.mem({
    settings = function()
        widget:set_markup(markup.font(fonts.widget, markup.fg.color(theme.fg_widget, " " .. mem_now.used .. "MB [" .. mem_now.perc .. "%]")))
    end
})
local mem_widget =  wibox.container.background(wibox.container.margin(wibox.widget { image.icons.mem, mem.widget, layout = wibox.layout.align.horizontal }, 0, 0), colors.widget_bg)


-- CPU
local cpu = lain.widget.cpu({
    settings = function()
        widget:set_markup(markup.font(fonts.widget, markup.fg.color(theme.fg_widget, " " .. cpu_now.usage .. "% ")))
    end
})
local cpu_widget =  wibox.container.background(wibox.container.margin(wibox.widget { image.icons.cpu, cpu.widget, layout = wibox.layout.align.horizontal }, 0, 0), colors.widget_bg)


-- Coretemp (lain, average)
local temp_icon = wibox.widget.imagebox(theme.widget_temp)
local temp = lain.widget.temp({
    settings = function()
        widget:set_markup(markup.font(fonts.widget, markup.fg.color(theme.fg_widget, " " .. coretemp_now .. "° ")))
    end
})
local temp_widget =  wibox.container.background(wibox.container.margin(wibox.widget { image.icons.temp, temp.widget, layout = wibox.layout.align.horizontal }, 0, 0), colors.widget_bg)


-- FS
-- local fs_icon = wibox.widget.imagebox(theme.widget_hdd)
-- local fs = lain.widget.fs({
--     notification_preset = { fg = theme.fg_normal, bg = theme.bg_normal, font = fonts.widget },
--     settings = function()
--         local fsp = string.format(" %3.2f %s ", fs_now["/home"].free, fs_now["/home"].units)
--         widget:set_markup(markup.font(fonts.widget, markup.fg.color(theme.fg_widget, fsp)))
--     end
-- })
-- local fs_widget =  wibox.container.background(wibox.container.margin(wibox.widget { image.icons.hdd, fs.widget, layout = wibox.layout.align.horizontal }, 0, 0), colors.widget_bg)

function theme.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])
    layout = { 
        awful.layout.layouts[3],
        awful.layout.layouts[10], 
        awful.layout.layouts[8], 
        awful.layout.layouts[7], 
        awful.layout.layouts[10], 
        awful.layout.layouts[10],
    }
    awful.tag({ " </> ", " >_ ", " web ", " & ", " etc ", " # " }, s, layout)

    -- 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(awful.util.table.join(
                           awful.button({ }, 1, function () awful.layout.inc( 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(s, awful.widget.tasklist.filter.currenttags, awful.util.tasklist_buttons)

    -- Create the wibox
    s.mywibox = awful.wibar({ position = "top", screen = s, height = 16, bg = theme.bg_wibar, fg = theme.fg_wibar, })

    s.tray = wibox.widget.systray()

    -- Add widgets to the wibox
    s.mywibox:setup {
        layout = wibox.layout.align.horizontal,
        { -- Left widgets
            layout = wibox.layout.fixed.horizontal,
            s.mytaglist,
            s.mypromptbox,
        },
        -- s.mytasklist, -- Middle widget
        nil,
        { -- Right widgets
            layout = wibox.layout.fixed.horizontal,
            -- Fs widget
            fs_widget,
            -- Temp
            temp_widget,
            -- CPU widget
            cpu_widget,
            -- Mem widget
            mem_widget,
            -- Clock
            clock_widget,
            -- tray
            image.systray_start,
            {
                s.tray,
                layout = wibox.layout.margin,
                color = "#000000",
                left = 5, right = 5
            },
            image.systray_end,
            -- Layout box
            s.mylayoutbox,
        },
    }
end

return theme
