 # -*- coding:UTF-8 -*-

import LCD_1in44
import LCD_Config
import threading
from PIL import Image,ImageDraw,ImageFont,ImageColor
import time

class RaDisplay:

    __on = False

    def __init__(self, Components):
        """
        Initialisiert das RaDisplay

        :param Components: RaComponents Element
        :type Components: RaComponents
        """
        self.components = Components
        self.__initDisplay()
        self.logoThread = threading.Thread(target = self.__showLogo)
        self.lock = threading.Lock()
        self.logoThread.start()


    def setCamera(self, Camera):
        self.__camera = Camera


    def On(self):
        if self.__on == True:
            return
        print("Display: On")
        self.autoThread = threading.Thread(target = self.__autoRefreshThread)
        self.__on = True
        if self.components.Camera!=None:
            self.components.Camera.startCamera()
        self.lock = threading.Lock()
        self.disp.LCD_Backlight(True)
        self.autoThread.start()


    def Off(self):
        if self.__on == False:
            return
        print("Display: Off")
        self.disp.LCD_Backlight(False)
        print("Display: Off1")
        self.__on = False
        self.autoThread.join()
        print("Display: Off2")
        if self.components.Camera!=None:
            self.components.Camera.stopCamera()
        print("Display: Off3")


    def isVisible(self):
        return self.__on


    def waitForLogoEnd(self):
        self.logoThread.join()
        del self.logoThread


    def __initDisplay(self):
        self.disp = LCD_1in44.LCD()
        Lcd_ScanDir = LCD_1in44.SCAN_DIR_DFT  #SCAN_DIR_DFT = D2U_L2R
        self.disp.LCD_Init(Lcd_ScanDir)
        self.disp.LCD_Clear()


    def __showLogo(self):
        with self.lock:
            self.On()
            image = Image.open('img/logo.bmp')
            self.disp.LCD_ShowImage(image,0,0)
            time.sleep(3.4)
            self.RequestUpdate()
            self.Off()
        

    def RequestUpdate(self):
        if self.components.Menu is not None and self.components.Menu.isVisible() == True:
            frame = self.components.Menu.getFrame()
            if frame is not False:
                self.Update(frame)

        elif self.__on == True and self.components.Camera is not None:
            frame = self.components.Camera.getFrame()
            if frame is not None:
                img = Image.fromarray(frame)
                img = img.crop((420,0,1080,1080))
                img = img.resize((128,128))

                if self.components.Camera.isRecording() == True:
                    draw = ImageDraw.Draw(img)
                    draw.ellipse((100, 100, 120, 120), fill="red")

                #img = img.crop((0,0,128,128))
                self.Update(img)

        else:
            image = Image.new("RGB", (128, 128), "BLACK")
            self.Update(image)


    def __autoRefreshThread(self):
        with self.lock:
            while self.__on:
                time.sleep(0.05)
                self.RequestUpdate()
        self.RequestUpdate()


    def Update(self, img : Image):
        self.disp.LCD_ShowImage(img,0 ,0)


    def draw_recording_indicator(self, image):
        """
        Zeichnet einen roten Kreis auf dem Display als Indikator für laufende Aufnahmen.
        """
        image = Image.new("RGB", (self.LCD.width, self.LCD.height))  # Leeres Bild erstellen
        draw = ImageDraw.Draw(image)  # Zeichnen-Objekt erstellen
        
        # Koordinaten für das Zeichnen des roten Kreises (untere rechte Ecke)
        circle_x = self.LCD.width - 12
        circle_y = self.LCD.height - 12
        circle_radius = 6
        
        # Kreis zeichnen
        draw.ellipse((circle_x - circle_radius, circle_y - circle_radius,
                      circle_x + circle_radius, circle_y + circle_radius), fill="red")
        
        self.LCD.LCD_ShowImage(image)  # Bild auf dem Display anzeigen
