"""

Satelliteninfos

  - Sat mit Fix: ausgefüllter Kreis
  - Sat ohne fix: leerer Kreis
  - Slots für 12 SNR-Balken

"""

import cairo
import math
from .page import Page

class SkyView(Page):

    def __init__(self, pageno, cfg, appdata, boatdata):
        super().__init__(pageno, cfg, appdata, boatdata)

    def pol2cart(azimut, elevation):
        '''
        Polar to Cartesian coordinates within the horizon circle.
        azimuth in radians
        x = math.sin(azimut) * elevation * self.radius
        y = math.cos(azimut) * elevation * self.radius
        '''
        pass
        # (x, y) = self.pol2cart(sat.az, sat.el)

    def draw(self, ctx):

        # Name
        ctx.select_font_face("Ubuntu", cairo.FontSlant.NORMAL, cairo.FontWeight.BOLD)
        #ctx.set_font_size(32)
        #self.draw_text_center(ctx, 200, 40, "Satellite Info")

        # Spezialseite
        cx = 130
        cy = 150
        r = 125
        r1 = r / 2

        ctx.set_line_width(1.5)
        ctx.arc(cx, cy, r, 0, 2*math.pi)
        ctx.stroke()
        ctx.arc(cx, cy, r1, 0, 2*math.pi)
        ctx.stroke()
        ctx.set_dash([4, 4], 0)
        ctx.move_to(cx, cy - r)
        ctx.line_to(cx, cy + r)
        ctx.move_to(cx - r, cy)
        ctx.line_to(cx + r, cy)
        ctx.stroke()
        ctx.set_dash([], 0)

        # Signal/Noise Balken
        ctx.set_font_size(16) 
        ctx.move_to(325, 34)
        ctx.show_text("SNR")
        ctx.stroke()

        ctx.set_line_width(1.5)
        ctx.rectangle(270, 20, 125, 257)

        # Beispieldaten
        ctx.set_line_width(0.5)
        for s in range(12):
            y = 30 + (s + 1) * 20
            ctx.move_to(275, y)
            ctx.show_text(f"{s:02d}")
            ctx.rectangle(305, y-12, 85, 14)
            ctx.stroke()

        ctx.set_line_width(1.0)
        for s in self.bd.sat.values():
            x = cx + math.sin(s.azimuth) * s.elevation * r
            y = cy + math.cos(s.azimuth) * s.elevation * r
            ctx.arc(x, y, 4, 0, 2*math.pi)
            ctx.move_to(x+4, y+4)
            ctx.show_text(f"{s.prn_num}")
            ctx.stroke()

        # Satellitenliste mit SNR-Balken sortiert nach nummer
        for prn_num in sorted(self.bd.sat):
            print(prn_num)
