--   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")

return function(args)
    local style = awmtk2.create_style("soundclown",awmtk2.default,args.style)
    local templates = awmtk2.create_template_lib("soundclown",awmtk2.templates,args.templates)
    local t = awmtk2.build_templates(templates,style)
    local test = os.execute("mpc help")
    if type(test) == "number" then
        test = (test == 0)
    end
    if not test then
        return
    end
    local widget = wibox.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"
        }),
        ((not args.hide_controls) and {
            t.button({
                image = beautiful["mpc-previous-symbolic"],
                widget = wibox.widget.imagebox
            },{
                id = "prev"
            }),
            t.button({
                image = beautiful["mpc-play-symbolic"],
                widget = wibox.widget.imagebox,
                id = "statusicon"
            },{
                id = "play"
            }),
            t.button({
                image = beautiful["mpc-next-symbolic"],
                widget = wibox.widget.imagebox
            },{
                id = "next"
            }),
            spacing = style.base.spacing,
            layout = wibox.layout.fixed.horizontal
        }),
        spacing = style.base.spacing,
        layout = 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)
    bprev:connect_signal("button::press",function()
        awful.spawn("mpc cdprev")
    end)
    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)
    bplay:connect_signal("button::press",function()
        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)
    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)
    bnext:connect_signal("button::press",function()
        awful.spawn("mpc next")
    end)
    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
                    state = true
                    icon.image = beautiful["mpc-play-symbolic"]
                    display:set_markup(status)
                    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)
        end)
    end
    update_mpd_status()
    gears.timer {
        timeout = args.polling_delay or 1,
        autostart = true,
        callback = function() 
            update_mpd_status()
        end
    }
    return widget
end
