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 = "Jetbrains Mono Bold 8",
    mono_bold    = "Jetbrains Mono ExtraBold 8"
}

fonts.widget = "Jetbrains Mono ExtraBold 8"

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

    transparency = "30",
}

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

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

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

theme.fg_widget                                 = colors.dark

theme.taglist_bg_focus                          = colors.dark
theme.taglist_fg_focus                          = colors.accent
theme.taglist_bg_occupied                       = colors.dark .. "40"
theme.taglist_fg_occupied                       = colors.dark
theme.taglist_bg_empty                          = colors.tramsparent
theme.taglist_fg_empty                          = colors.dark
theme.taglist_bg_urgent                         = "#C92132"
theme.taglist_fg_urgent                         = "#282828"

theme.tasklist_bg_normal                        = "#32302f"
theme.tasklist_fg_normal                        = colors.dark
theme.tasklist_bg_focus                         = "#32302f"
theme.tasklist_fg_focus                         = colors.dark
theme.tasklist_bg_urgent                        = "#C92132"
theme.tasklist_fg_urgent                        = colors.attention

theme.border_width                              = 0
theme.border_normal                             = "#111111"
theme.border_focus                              = "#000000"
theme.border_marked                             = "#CC9393"

theme.titlebar_bg_normal                        = "#212028"
theme.titlebar_bg_focus                         = "#1c1b22"
theme.titlebar_fg_normal                        = colors.white
theme.titlebar_fg_focus                         = colors.white

theme.menu_height                               = 24
theme.menu_width                                = 200

theme.notification_font                         = fonts.mono_regular
theme.notification_bg                           = colors.dark .. "dd"
theme.notification_fg                           = theme.fg_normal
theme.notification_border_width                 = 0
theme.notification_border_color                 = colors.accent
theme.notification_shape                        = gears.shape.rectangle
theme.notification_opacity                      = 1
theme.notification_margin                       = 30
--theme.warning                                   = theme.dir .. "/icons/status/warning.png"
theme.notification_max_width                    = 500
theme.notification_icon_size                    = 64
--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                     = false
theme.tasklist_plain_task_name                  = true
theme.useless_gap                               = 3
theme.master_width_factor                       = 0.66

-- 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_close_button_focus_hover         = theme.dir .. "/icons/titlebar/close-hover.png"
theme.titlebar_close_button_focus_press         = theme.dir .. "/icons/titlebar/close-press.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 {
        layout = wibox.layout.margin,
        top = 3, bottom = 3, left = 1, right = 1,
        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"),
    mic_on  = icon("widget", "mic-on"),
    mic_off = icon("widget", "mic-off"),
}

image.icons.mic = image.icons.mic_on

-- 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.mono_bold .. "\" color=\"" .. colors.dark .. "\"> %H:%M </span>")
local clock_widget = wibox.container.background(wibox.container.margin(wibox.widget { clock, layout = wibox.layout.align.horizontal }, 0, 1), colors.transparent)

local date = awful.widget.textclock("<span font=\"" .. fonts.mono_bold .. "\" color=\"" .. colors.dark .. "\"> %a %d %b </span>")
local date_widget = wibox.container.background(wibox.container.margin(wibox.widget { date, layout = wibox.layout.align.horizontal }, 0, 1), colors.transparent)

-- Calendar
local calendar = lain.widget.calendar({
    cal = "cal --color=always",
    attach_to = { date_widget },
    followtag = true,
    notification_preset = {
        font     = fonts.mono_regular,
        fg       = theme.fg_normal,
        bg       = theme.bg_normal,
        position = "top_middle",
        screen   = awful.mouse.screen,
    }
})

-- 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.widget { image.icons.mem, mem.widget, layout = wibox.layout.align.horizontal }


-- 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.widget { image.icons.cpu, cpu.widget, layout = wibox.layout.align.horizontal }


-- Coretemp (lain, average)
local temp_icon = wibox.widget.imagebox(theme.widget_temp)
local temp = lain.widget.temp({
    tempfile = "/sys/class/thermal/thermal_zone2/temp",
    settings = function()
        widget:set_markup(markup.font(fonts.widget, markup.fg.color(theme.fg_widget, string.format(" %.1f° ", coretemp_now))))
    end
})
local temp_widget = wibox.widget { image.icons.temp, temp.widget, layout = wibox.layout.align.horizontal }


-- 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 },
    followtag = true,
    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.widget { image.icons.hdd, fs.widget, layout = wibox.layout.align.horizontal }

-- -- Mic
local mic = lain.widget.alsa({
    channel = "Capture",
    settings = function ()
        if volume_now.status == "on" then
            image.icons.mic = image.icons.mic_on
        else
            image.icons.mic = image.icons.mic_off
        end

        widget:set_markup(markup.font(fonts.widget, markup.fg.color(theme.fg_widget, string.format(" %s [%d%%]", volume_now.status, volume_now.level))))
    end
})
local mic_widget =  wibox.widget { image.icons.mic, mic.widget, layout = wibox.layout.align.horizontal }

function theme.connect(s)
    -- Quake application
    s.quake = lain.util.quake({ 
        app = "ghostwriter", 
        followtag = true,
        width  = 0.5,
        height = 1,
    })

    -- 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[1],
        awful.layout.layouts[1], 
        awful.layout.layouts[8], 
        awful.layout.layouts[7], 
        awful.layout.layouts[6], 
        awful.layout.layouts[1],
    }
    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 {
        screen  = s,
        filter  = awful.widget.taglist.filter.all,
        style   = {
            shape = function(cr, width, height) return gears.shape.parallelogram(cr, width, height, width - 8) end
        },
        layout   = {
            spacing = 4,
            layout  = wibox.layout.fixed.horizontal,
        },
        widget_template = {
            {
                {
                    {
                        id     = 'text_role',
                        widget = wibox.widget.textbox,
                    },
                    layout = wibox.layout.fixed.horizontal,
                },
                left  = 16,
                right = 16,
                widget = wibox.container.margin
            },
            id     = 'background_role',
            widget = wibox.container.background,
        },
        buttons = 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 = 26, bg = theme.bg_wibar, fg = theme.fg_wibar, type = dock })

    s.tray = wibox.widget.systray()

    s.mytasklist = awful.widget.tasklist {
        screen   = s,
        filter   = awful.widget.tasklist.filter.focused,
        layout   = {
            spacing = 10,
            layout  = wibox.layout.flex.horizontal
        },
        widget_template = {
            {
                {
                    {
                        id     = 'icon_role',
                        widget = wibox.widget.imagebox,
                    },
                    top = 5,
                    bottom = 5,
                    right = 10,
                    widget  = wibox.container.margin,
                },
                {
                    id     = 'text_role',
                    widget = wibox.widget.textbox,
                },
                layout = wibox.layout.fixed.horizontal,
            },
            left  = 20,
            right = 20,
            widget = wibox.container.margin
        },
    }

    -- s.tray:setup {
    --     layout = wibox.layout.margin,
    --     widget = wibox.container.background,
    --     bg = "#ff0000"
    -- }

    -- Add widgets to the wibox
    s.mywibox:setup {
        layout = wibox.layout.align.horizontal,
        widget = wibox.container.background,
        bg = "#ffa90060",
        expand = 'none',
        { -- Left widgets
            layout = wibox.layout.margin,
            left = 8,
            {
                layout = wibox.layout.fixed.horizontal,
                s.mytaglist,
                s.mytasklist,
                s.mypromptbox,
            }
        },
        -- s.mytasklist, -- Middle widget
        {
            layout = wibox.layout.fixed.horizontal,
            date_widget,
        },
        { -- Right widgets
            layout = wibox.layout.margin,
            top = 2, bottom = 2,
            {
                layout = wibox.layout.fixed.horizontal,
                spacing = 8,
                -- stat
                {
                    widget = wibox.container.background,
                    shape  = function (cr, w, h) gears.shape.rounded_rect(cr, w, h, 4) end,
                    color  = "#000000",
                    bg     = "#00000000",
                    {
                        layout = wibox.layout.margin,
                        left = 8, right = 8,
                        {
                            layout = wibox.layout.fixed.horizontal,
                            spacing = 8,
                            -- Mic widget
                            mic_widget,
                            -- Fs widget
                            fs_widget,
                            -- Temp
                            temp_widget,
                            -- CPU widget
                            cpu_widget,
                            -- Mem widget
                            mem_widget,
                        }
                    }
                },
                -- tray
                {
                    widget = wibox.container.background,
                    shape = function (cr, w, h) gears.shape.rounded_rect(cr, w, h, 4) end,
                    bg = "#FF9800",
                    {
                        layout = wibox.layout.margin,
                        left = 8, right = 8, top = 2, bottom = 2,
                        {
                            layout = wibox.layout.fixed.horizontal,
                            spacing = 8,
                            s.tray,
                            clock_widget,
                        }
                    }
                },
                -- Layout box
                {
                    layout = wibox.layout.margin,
                    right = 8,
                    {
                        widget = wibox.container.background,
                        bg = "#00000000",
                        shape = function (cr, w, h) gears.shape.rounded_rect(cr, w, h, 4) end,
                        {
                            layout = wibox.layout.margin,
                            s.mylayoutbox,
                            left = 8, 
                            right = 8,
                            top = 2,
                            bottom = 2,
                        }
                    }
                },
            }
        },
    }
end

return theme
