
from LUIObject import LUIObject
from LUISprite import LUISprite

from LUILayouts import LUIHorizontalStretchedLayout
from LUILabel import LUILabel

class LUIProgressbar(LUIObject):

    """ A simple progress bar """

    def __init__(self, parent=None, width=200, value=50, show_label=True, label_fmt="{percentage} %", **kwargs):
        """ Constructs a new progress bar. If show_label is True, a label indicating
        the current progress is shown """
        LUIObject.__init__(self, **kwargs)
        self.set_width(width)

        self._bg_layout = LUIHorizontalStretchedLayout(
            parent=self, prefix="ProgressbarBg", width="100%")

        self._fg_left = LUISprite(self, "ProgressbarFg_Left", "skin")
        self._fg_mid = LUISprite(self, "ProgressbarFg", "skin")
        self._fg_right = LUISprite(self, "ProgressbarFg_Right", "skin")
        self._fg_finish = LUISprite(self, "ProgressbarFg_Finish", "skin")

        self._show_label = show_label
        self._label_format = label_fmt
        self._progress_pixel = 0
        self._fg_finish.right = 0
        self._max = 100.0
        self._value = value

        if self._show_label:
            self._progress_label = LUILabel(parent=self, text=u"33 %")
            self._progress_label.centered = (True, True)

        self.set_value(value)
        self._update_progress()

        if parent is not None:
            self.parent = parent

    def set_max(self, _max):
        self._max = _max

    def get_max(self): return self._max

    def get_value(self):
        """ Returns the current value of the progress bar """
        return self._value

    def set_value(self, val):
        """ Sets the value of the progress bar """
        self._value = val
        val = max(0, min(self._max, val))
        self._progress_pixel = int(val / self._max * self.width)
        self._update_progress()

    value = property(get_value, set_value)

    def _update_progress(self):
        """ Internal method to update the progressbar """
        self._fg_finish.hide()

        if self._progress_pixel <= self._fg_left.width + self._fg_right.width:
            self._fg_mid.hide()
            self._fg_right.left = self._fg_left.width
        else:
            self._fg_mid.show()
            self._fg_mid.left = self._fg_left.width
            self._fg_mid.width = self._progress_pixel - self._fg_right.width - self._fg_left.width
            self._fg_right.left = self._fg_mid.left + self._fg_mid.width

            if self._progress_pixel >= self.width - self._fg_right.width:
                self._fg_finish.show()
                self._fg_finish.right = - (self.width - self._progress_pixel)
                self._fg_finish.clip_bounds = (0, self.width - self._progress_pixel, 0, 0)

        if self._show_label:
            string = self._label_format.replace('{value}', "{0}".format(int(self.get_value())))
            string = string.replace('{max}', "{0}".format(int(self._max)))
            string = string.replace('{percentage}', "{:.2f}".format((self._progress_pixel / self.width) * self._max))

            self._progress_label.set_text(string)
