##
# other modules
import sub_process
import sys
import aosd
import ConfigParser


# our modules
import func_module


# =================================

class msg(func_module.FuncModule):

    version = "0.0.1"
    api_version = "0.0.1"
    description = "Display messages"

#class msg():
    def scroll(self,osd, width, height, display_time,position):
        pos = position
        step = 1

        osd.set_position(pos, width, height)
        (x, y, _, _) = osd.get_geometry()
        osd.set_position_offset(width, height)
        osd.show()

        x -= 1
        y += height - 1;
        for i in range(1, height + 1, step):
            osd.loop_for(5)
            y -= step
            osd.set_geometry(x, y, width, i)

        osd.set_position(pos, width, height)
        osd.set_position_offset(-1, -1)
        (x, y, _, _) = osd.get_geometry()
    #time to display
        osd.loop_for(display_time)

        for i in range(height, 0, -step):
            y += step
            osd.set_geometry(x, y, width, i);
            osd.loop_for(1);

        osd.hide();

    def setup(self,font_color,font_type):
        osd = aosd.AosdText()
        osd.set_transparency(aosd.TRANSPARENCY_COMPOSITE)
        if osd.get_transparency() != aosd.TRANSPARENCY_COMPOSITE:
            osd.set_transparency(aosd.TRANSPARENCY_NONE)

        osd.geom_x_offset = 10
        osd.geom_y_offset = 0

        osd.back_color = "white"
        osd.back_opacity = 50

        osd.shadow_color = "black"
        osd.shadow_opacity = 127
        osd.shadow_x_offset = 2
        osd.shadow_y_offset = 2

        osd.fore_color = font_color
        osd.fore_opacity = 255

        osd.set_font(font_type)
        osd.wrap = aosd.PANGO_WRAP_WORD_CHAR
        osd.alignment = aosd.PANGO_ALIGN_LEFT
        osd.set_layout_width(osd.get_screen_wrap_width())
        return osd

    def set_string(self, osd, text):
        osd.set_text(text)
        return osd.get_text_size()

    def setup_config(self):
        module_config = ConfigParser.RawConfigParser()
        try:
            module_config.read('/usr/MythVantage/etc/msg.cfg')
        except:
            pass
        return module_config



    def display (self,flags):
        """
        parse the msg for display.
        """
        try:
            text,config_section=flags.split("|")
            if config_section == '':
                config_section="default"
        except:
            text=flags
            config_section="default"

        module_config = self.setup_config()

        display_time=5000
        position=6
        font_color="green"
        font_type="Times New Roman Italic 36"
        try:
            display_time =  int(module_config.get(config_section,"display_time"))
        except:
            pass
        try:
            position =  int(module_config.get(config_section,"position"))
        except:
            pass
        try:
            font_color = module_config.get(config_section,"font_color")
        except:
            pass
        try:
            font_type =  module_config.get(config_section,"font_type")
        except:
            pass


        cmd = sub_process.Popen("/usr/bin/wall %s" % text,stdout=sub_process.PIPE,shell=True)
        data = cmd.communicate()[0]
        osd = self.setup(font_color, font_type)
        width, height = self.set_string(osd, text)
        self.scroll(osd, width, height, display_time, position)

        return ("Message delivered")