from datetime import datetime
from utilities.animator import Animator
from setup import colours, screen, frames
from config import NIGHT_START, NIGHT_END

LINE2_Y = 22

FLOWN_COLOUR     = colours.LIMEGREEN
REMAINING_COLOUR = colours.LIGHT_RED
PLANE_COLOUR     = colours.WHITE

NIGHT_START_TIME = datetime.strptime(NIGHT_START, "%H:%M")
NIGHT_END_TIME   = datetime.strptime(NIGHT_END,   "%H:%M")


def _calc_progress(data):
    dist_remaining = data.get("dist_remaining")
    total_distance = data.get("total_distance")
    if dist_remaining is None or not total_distance or total_distance <= 0:
        return 0.0
    dist_flown = total_distance - dist_remaining
    return max(0.0, min(1.0, dist_flown / total_distance))


def _draw_plane(canvas, x, y, is_live=True):
    c = PLANE_COLOUR if is_live else colours.LIGHT_RED
    for px in range(x, x + 4):
        canvas.SetPixel(px, y, c.red, c.green, c.blue)
    canvas.SetPixel(x + 2, y - 1, c.red, c.green, c.blue)
    canvas.SetPixel(x + 2, y + 1, c.red, c.green, c.blue)


PLANE_WIDTH = 4


class TrackedProgressScene(object):
    def __init__(self):
        super().__init__()
        self._tp_redraw = True
        self._tp_active = False

    def _clear_tracked_progress(self):
        self.draw_square(0, LINE2_Y - 3, screen.WIDTH, LINE2_Y + 1, colours.BLACK)
        self._tp_redraw = True
        self._tp_active = False

    @Animator.KeyFrame.add(0)
    def reset_tracked_progress(self):
        self._clear_tracked_progress()

    @Animator.KeyFrame.add(1)
    def tracked_progress(self, count):
        # Force redraw at brightness transition times
        now = datetime.now().replace(microsecond=0).time()
        if now == NIGHT_START_TIME.time() or now == NIGHT_END_TIME.time():
            self._tp_redraw = True
            return

        tracked = self.overhead.tracked_data
        if not tracked:
            if self._tp_active:
                self._clear_tracked_progress()
            return
        if tracked.get("pre_flight"):
            if self._tp_active:
                self._clear_tracked_progress()
            return
        if len(self._data) > 0:
            if not tracked.get("override_flights"):
                if self._tp_active:
                    self._clear_tracked_progress()
                return
        elif not tracked.get("override_idle", True):
            if self._tp_active:
                self._clear_tracked_progress()
            return
        if tracked.get("waiting_live"):
            self._clear_tracked_progress()
            return

        progress = _calc_progress(tracked)
        width  = screen.WIDTH
        mid_y  = LINE2_Y - 1
        usable = width - PLANE_WIDTH
        plane_x = max(0, min(usable, int(progress * usable)))

        self.draw_square(0, LINE2_Y - 3, width, LINE2_Y + 1, colours.BLACK)

        for x in range(plane_x):
            if (x // 2) % 2 == 0:
                self.canvas.SetPixel(x, mid_y,
                    FLOWN_COLOUR.red, FLOWN_COLOUR.green, FLOWN_COLOUR.blue)

        for x in range(plane_x + PLANE_WIDTH, width):
            if (x // 2) % 2 == 0:
                self.canvas.SetPixel(x, mid_y,
                    REMAINING_COLOUR.red, REMAINING_COLOUR.green, REMAINING_COLOUR.blue)

        _draw_plane(self.canvas, plane_x, mid_y, is_live=tracked.get("is_live", True))

        self._tp_redraw = False
        self._tp_active = True
