"""

Rotationssensor AS5600 mit Funktion "Kiel"
WIP

Idee: 
  - Zusätzlich Anzeigemöglichkeit für die Tiefe eines variablen Kiels
  - Mode-Taste

"""

import cairo
import math
from .page import Page

class Keel(Page):

    def __init__(self, pageno, cfg, boatdata):
        super().__init__(pageno, cfg, boatdata)
        # Wert für Kielrotation
        self.valref = self.bd.getRef("xdrRotK")

    def draw(self, ctx):

        # Mitte oben Instrument (Halbkreis)
        cx = 200
        cy = 150

        # Radius Kielposition
        r = 110 

        # Titel
        ctx.select_font_face("Ubuntu", cairo.FontSlant.NORMAL, cairo.FontWeight.BOLD)
        ctx.set_font_size(32)
        ctx.move_to(100, 70)
        ctx.show_text("Keel Position")
        ctx.set_font_size(24)
        ctx.move_to(175, 110)
        ctx.show_text(self.valref.unit)
        ctx.stroke()


        # Halbkreis für Skala
        ctx.set_source_rgb(*self.fgcolor)
        ctx.set_line_width(3)
        ctx.arc(cx, cy, r + 10, 0, math.pi)
        ctx.stroke()
        
        # Skala mit Strichen, Punkten und Beschriftung
        char = {
          90: "45",
          120: "30",
          150: "15",
          180: "0",
          210: "15",
          240: "30",
          270: "45"
        }
        # Zeichnen in 10°-Schritten
        ctx.set_font_size(16)
        for i in range(90, 271, 10):
            fx = math.sin(i / 180 * math.pi)
            fy = math.cos(i / 180 * math.pi)
            if i in char:
                x = cx + (r - 30) * fx
                y = cy - (r - 30) * fy
                self.draw_text_center(ctx, x, y, char[i])
                ctx.stroke()
            if i % 30 == 0:
                ctx.move_to(cx + (r - 10) * fx, cy - (r - 10) * fy)
                ctx.line_to(cx + (r + 10) * fx, cy - (r + 10) * fy)
                ctx.stroke()
            else:
                x = cx + r * fx
                y = cy - r * fy
                ctx.arc(x, y, 2, 0, 2*math.pi)
                ctx.fill()

        # Boot und Wasserlinie
        ctx.arc(cx, cy - 10, 28, 0, math.pi)
        ctx.fill()
        ctx.set_line_width(4)
        ctx.move_to(150, cy)
        ctx.line_to(250, cy)
        ctx.stroke()

        #ctx.arc(200, 150, r + 10, 0, 2*math.pi)
        #ctx.fill()
        #ctx.set_source_rgb(*self.bgcolor)
        #ctx.arc(200, 150, r + 7, 0, 2* math.pi)
        #ctx.rectangle(0, 30, 299, 122)
        #ctx.fill()

        angle = -15
        #angle = self.valref.value
        #TODO  Angle limits to +/-45°
        if angle < -45:
            angle = -45
        elif angle > 45:
            angle = 45
        angle *= 2 # stretched scale

        # Kiel
        p = ((cx - 6, cy), (cx + 6, cy), (cx + 2, cy + r - 50), (cx - 2, cy + r - 50))
        keel = self.rotate((cx, cy), p, angle)
        ctx.move_to(*keel[0])
        for point in keel[1:]:
            ctx.line_to(*point)
        ctx.fill()

        # Kiel-Bombe
        x, y  = self.rotate((cx, cy), ((cx, cy + r -50),), angle)[0]
        ctx.arc(x, y, 5, 0, 2*math.pi)
        ctx.fill()