# -*- coding: utf-8 -*-
# Copyright (c) 2010 matt
# Copyright (c) 2010 Dieter Plaetinck
# Copyright (c) 2010, 2012 roger
# Copyright (c) 2011-2012 Florian Mounier
# Copyright (c) 2011 Mounier Florian
# Copyright (c) 2011 Timo Schmiade
# Copyright (c) 2012 Mikkel Oscar Lyderik
# Copyright (c) 2012, 2014 Tycho Andersen
# Copyright (c) 2012 Craig Barnes
# Copyright (c) 2013 Tao Sauvage
# Copyright (c) 2013 Tom Hunt
# Copyright (c) 2014 Justin Bronder
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# depends on python-mpd


# TODO: check if UI hangs in case of network issues and such
# TODO: some kind of templating to make shown info configurable
# TODO: best practice to handle failures? just write to stderr?

from __future__ import division

import re
import time

import mpd

from .. import utils, pangocffi
from . import base
from libqtile.log_utils import logger


class Mpd(base.ThreadPoolText):
    """A widget for the Music Player Daemon (MPD)"""
    orientations = base.ORIENTATION_HORIZONTAL
    defaults = [
        ("host", "localhost", "Host to connect to, can be either an IP "
                              "address or a UNIX socket path"),
        ("port", 6600, "Port to connect to"),
        ("password", None, "Password to use"),
        ("fmt_playing", "%a - %t [%v%%]", "Format string to display when "
                                          "playing/paused"),
        ("fmt_stopped", "Stopped [%v%%]", "Format strings to display when "
                                          "stopped"),
        ("msg_nc", "Mpd off", "Which message to show when we're not "
                              "connected"),
        ("do_color_progress", True, "Whether to indicate progress in song by "
                                    "altering message color"),
        ("foreground_progress", "ffffff", "Foreground progress colour"),
        ("reconnect", False, "Attempt to reconnect if initial connection failed"),
        ("reconnect_interval", 1, "Time to delay between connection attempts."),
        ("update_interval", 0.5, "Update Time in seconds.")
    ]

    def __init__(self, **config):