"""

NAVTEX
 - Meldungen anzeigen

"""

import cairo
from .page import Page

class Navtex(Page):

    def __init__(self, pageno, cfg, appdata, boatdata):
        super().__init__(pageno, cfg, appdata, boatdata)
        self.disabled = self.app.navtex is None
        self.ids = self.app.navtex.get_ids()
        if len(self.ids) > 0:
            self.current = 1
            self.msgid = self.ids[self.current - 1]
        else:
            self.current = 0
            self.msgid = None
        self.buttonlabel[1] = 'PREV'
        self.buttonlabel[2] = 'NEXT'
        self.buttonlabel[5] = 'MORE'
        self.skip = 0

    def handle_key(self, buttonid):
        if buttonid == 1:
            if self.current > 1:
                self.current -= 1
            else:
                self.current = len(self.ids)
            self.msgid = self.ids[self.current - 1]
            self.skip = 0
            return True
        if buttonid == 2:
            if self.current < len(self.ids):
                self.current += 1
            else:
                self.current = 1
            self.msgid = self.ids[self.current - 1]
            self.skip = 0
            return True
        if buttonid == 5:
            self.skip += 1
            return True
        return False

    def draw(self, ctx):
        # Title
        ctx.select_font_face("Ubuntu", cairo.FontSlant.NORMAL, cairo.FontWeight.BOLD)
        ctx.set_font_size(24)
        ctx.move_to(8, 40)
        ctx.show_text("NAVTEX")

        ctx.set_line_width(1)
        ctx.move_to(4.5, 42.5)
        ctx.line_to(395.5, 42.5)
        ctx.move_to(4.5, 272.5)
        ctx.line_to(395.5, 272.5)

        ctx.move_to(4.5, 32.5)
        ctx.line_to(4.5, 272.5)
        ctx.move_to(396.5, 32.5)
        ctx.line_to(396.5, 272.5)

        #ctx.rectangle(4.5, 20.5, 392, 250)
        ctx.stroke()

        ctx.set_font_size(16)

        if self.disabled:
            ctx.move_to(8, 75)
            ctx.show_text("Feature ist disabled by configuration")
            return

        ctx.move_to(150, 40)
        self.draw_text_ralign(ctx, 392, 38, "Message {} of {}".format(self.current, len(self.ids)))

        ctx.select_font_face("AtariST8x16SystemFont")

        ctx.move_to(8, 59)
        if self.current == 0:
            ctx.show_text("NIL")
            return

        # 48 Zeichen je Zeile möglich
        # Max. 14 Zeilen auf dem Bildschirm
        rawmsg = self.app.navtex.get_message(self.msgid).splitlines()
        output = []
        for line in rawmsg:
            if len(line) <= 48:
                output.append(line)
            else:
                i = 0
                j = 48
                while i < len(line):
                    output.append(line[i:j])
                    i += 48
                    j += 48
        x = 8
        y = 59
        n = 0
        for line in output:
            if n >= self.skip:
                ctx.move_to(x, y)
                ctx.show_text(line)
                y += 16
            n += 1
            if n >= 14 + self.skip:
                break
