from PIL import Image

from utilities.animator import Animator
from utilities.tracked_display import tracked_overrides_flights
from setup import colours

LOGO_SIZE = 16
DEFAULT_IMAGE = "default"

class FlightLogoScene:
    @Animator.KeyFrame.add(0)
    def logo_details(self):
        if tracked_overrides_flights(self.overhead):
            return

        # Guard against no data
        if len(self._data) == 0:
            return

        # Clear the whole area
        self.draw_square(
            0,
            0,
            LOGO_SIZE,
            LOGO_SIZE,
            colours.BLACK,
        )

        icao = self._data[self._data_index]["owner_icao"]
        if icao in ("", "N/A"):
            icao = DEFAULT_IMAGE

        # Open the file
        try:
            image = Image.open(f"logos/{icao}.png")
        except FileNotFoundError:
            image = Image.open(f"logos/{DEFAULT_IMAGE}.png")


        # Make image fit our screen.
        try:
            resample = Image.Resampling.LANCZOS  # Pillow 10+
        except AttributeError:
            resample = Image.ANTIALIAS          # Pillow <10
        image.thumbnail((LOGO_SIZE, LOGO_SIZE), resample)
        # Draw pixel-by-pixel (avoids Pillow/rgbmatrix unsafe_ptrs crash)
        rgb = image.convert("RGB")
        pixels = rgb.load()
        w, h = rgb.size
        for py in range(h):
            for px in range(w):
                r, g, b = pixels[px, py]
                self.canvas.SetPixel(px, py, r, g, b)
