"""
Wind / Windlupe

TODO
 - Windlupe:
   Wenn der Wind achterlich kommt, dann andere Ansicht / Skala verwenden

"""
import os
import cairo
import math
from .page import Page

class Wind(Page):

    def __init__(self, pageno, cfg, appdata, boatdata):
        super().__init__(pageno, cfg, appdata, boatdata)
        self.buttonlabel[1] = 'MODE'
        self.mode = 'L' # (W)ind (L)ens
        try:
            self.symbol = cairo.ImageSurface.create_from_png(os.path.join(cfg['imgpath'], "front.png"))
        except:
            self.symbol = None
            print("Warning: Missing image: {}".format(os.path.join(cfg['imgpath'], "front.png")))

    def handle_key(self, buttonid):
        if buttonid == 1:
            if self.mode == 'W':
                self.mode = 'L'
            else:
                self.mode = 'W'
            return True
        return False

    def draw_wind(self, ctx):
        # Name
        ctx.set_font_size(40) 
        ctx.move_to(20, 100)
        ctx.show_text("Apparent Wind")

    def draw_lens(self, ctx):
        if self.symbol:
            ctx.save()
            ctx.set_source_surface(self.symbol, 140, 30)
            ctx.paint()
            ctx.restore()

        ctx.set_line_width(2)

        # Analoginstrument
        cx = 200
        cy = 150
        r = 135
        ctx.arc(cx, cy, r, math.radians(110), math.radians(250))
        ctx.stroke()
        ctx.arc(cx, cy, r, math.radians(-70), math.radians(70))
        ctx.stroke()

        # Windstärke als Digitalwert
        ctx.select_font_face("DSEG7 Classic")
        ctx.set_font_size(40)
        self.draw_text_center(ctx, cx, cy + 80, "14.3", fix1=True)

    def draw(self, ctx):
        ctx.select_font_face("Ubuntu", cairo.FontSlant.NORMAL, cairo.FontWeight.BOLD)
        if self.mode == 'W':
            self.draw_wind(ctx)
        else:
            self.draw_lens(ctx)
