--   This file is part of Reno desktop.
--
--  Reno desktop is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
--
--  Reno desktop is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License along with Reno desktop. If not, see <https://www.gnu.org/licenses/>. 
-- Widget for interacting with mpd
-- MPC IS REQUIRED
local awmtk2 = require("awmtk2")
local wibox = require("wibox")
local gears = require("gears")
local awful = require("awful")
local beautiful = require("beautiful")
local ask = require("asckey")

return function(args)
    local style = awmtk2.create_style("soundclown",
        awmtk2.generic.oneline_widget,args.style,args.vertical)
    local templates = awmtk2.create_template_lib("soundclown",awmtk2.templates,args.templates)
    local t = awmtk2.build_templates(templates,style,args.vertical)
    local test = os.execute("mpc help")
    if type(test) == "number" then
        test = (test == 0)
    end
    if not test then
        return
    end
    local controls_widget = {
        t.button(t.icon({
            image = beautiful["mpc-previous-symbolic"],
        }),{
            id = "prev"
        }),
        t.button(t.icon({
            image = beautiful["mpc-play-symbolic"],
            id = "statusicon"
        }),{
            id = "play"
        }),
        t.button(t.icon({
            image = beautiful["mpc-next-symbolic"],
        }),{
            id = "next"
        }),
        spacing = style.base.spacing,
        layout = wibox.layout.flex.horizontal
    }
    local display_widget = t.container({
        {
            t.textbox({
                markup = "MPC Loading..",
                id = "display"
            }),
            widget = wibox.container.scroll.horizontal,
            step_function = wibox.container.scroll.step_functions
               .linear_back_and_forth,
            speed = 50,
        },
        widget = wibox.container.constraint,
        height = style.base.height,
        width = style.base.width,
        strategy = "exact"
    })
    local widget = wibox.widget({
        display_widget,
        ((not args.hide_controls) and controls_widget),
        spacing = style.base.spacing,
        layout = (args.vertical and wibox.layout.fixed.vertical) or
            wibox.layout.fixed.horizontal
    })
    local display = widget:get_children_by_id("display")[1]
    local bprev = widget:get_children_by_id("prev")[1]
    bprev:connect_signal("button::press",style.button.onpress)
    bprev:connect_signal("button::release",style.button.onrelease)
    local function prev()
        awful.spawn("mpc cdprev")
    end
    bprev:connect_signal("button::press",prev)
    local pause_state = true
    local icon = widget:get_children_by_id("statusicon")[1]
    local bplay = widget:get_children_by_id("play")[1]
    bplay:connect_signal("button::press",style.button.onpress)
    bplay:connect_signal("button::release",style.button.onrelease)
    local function play()
        pause_state = (not pause_state)
        if pause_state == false then
            icon.image = beautiful["mpc-pause-symbolic"]
            awful.spawn("mpc pause")
        else
            icon.image = beautiful["mpc-play-symbolic"]
            awful.spawn("mpc play")
        end
    end
    bplay:connect_signal("button::press",play)
    local bnext = widget:get_children_by_id("next")[1]
    bnext:connect_signal("button::press",style.button.onpress)
    bnext:connect_signal("button::release",style.button.onrelease)
    local function nextb()
        awful.spawn("mpc next")
    end
    bnext:connect_signal("button::press",nextb)
    local update_ready = true
    local function update_mpd_status()
        awful.spawn.easy_async("mpc",function(out)
            local status = " "
            if not out:match("playing") then
                if not out:match("paused") then
                    pause_state = true
                    icon.image = beautiful["mpc-play-symbolic"]
                    display:set_markup(status)
                    update_ready = true
                    return
                else
                    status = status.."[PAUSED] "
                end
            end
            status = status..out:match("#%d*").." "..
                     out:match("[^\n]*").." "..
                     out:match("%d*:%d*/%d*:%d*%s*%(%d*%%%)")
            display:set_markup(status)
            update_ready = true
        end)
    end
    update_mpd_status()
    gears.timer {
        timeout = args.polling_delay or 1,
        autostart = true,
        callback = function()
            if not update_ready then return end
            update_ready = false
            update_mpd_status()
        end
    }
    root.keys(gears.table.join(
        root.keys(),
        ask.k(":mpc.prev",prev,
            {description = "switch to previous MPD track",group="widgets"}),
        ask.k(":mpc.play",play,
            {description = "play/pause MPD",group="widgets"}),
        ask.k(":mpc.next",nextb,
            {description = "switch to next MPD track",group="widgets"})
    ))
    return widget
end
