import gi
import struct
import os

gi.require_version('Gst', '1.0')
from gi.repository import Gst, GLib

class BrightnessManager:
    def __init__(self):
        self.backlight_path = "/sys/class/backlight/main_backlight/brightness"
        self.led_multi_path = "/sys/class/leds/display-indicator/multi_intensity"
        self.led_trig_path = "/sys/class/leds/display-indicator/trigger"
        self.led_br_path = "/sys/class/leds/display-indicator/brightness"

        # Backlight state
        self.bl_current = 10
        self.bl_direction = 1  # 1 for increasing, -1 for decreasing

        # Hex to RGB Tuples: 5F016F, FF33BB, FF80D4, FFADE4
        self.colors = [
            (95, 1, 111),   # #5F016F
            (255, 51, 187), # #FF33BB
            (255, 128, 212),# #FF80D4
            (255, 173, 228) # #FFADE4
        ]

        self.color_index = 0
                                
        self.setup_led()

    def setup_led(self):
        try:
            with open(self.led_trig_path, 'w') as f: f.write("none")
            with open(self.led_br_path, 'w') as f: f.write("255")
            self.change_led_colour()
        except Exception as e:
            print(f"LED Setup Error: {e}")

    def change_led_colour(self):
        r, g, b = self.colors[self.color_index]
        
        try:
            with open(self.led_multi_path, 'w') as f:
                f.write(f"{r} {g} {b}")
            print(f"LED changed to color index {self.color_index}: {r} {g} {b}")
        except Exception as e:
            print(f"Failed to write LED color: {e}")

        # Increment index for next time
        self.color_index = (self.color_index + 1) % len(self.colors)
        return True # Keep GLib timer alive

    def cycle_backlight(self):
        """Standard backlight cycle on touch"""
        self.bl_current += self.bl_direction
        if self.bl_current >= 20:
            self.bl_current = 20
            self.bl_direction = -1
        elif self.bl_current <= 0:
            self.bl_current = 0
            self.bl_direction = 1
        try:
            with open(self.backlight_path, 'w') as f:
                f.write(str(self.bl_current))
            print(f"Touch! Backlight: {self.bl_current}")    
            
        except Exception as e:
            print(f"Backlight Error: {e}")

def handle_touch(source, condition, manager):
    # Linux input_event struct: long, long, unsigned short, unsigned short, unsigned int
    # We read 24 bytes (standard for 64-bit systems like i.MX8M)
    EVENT_SIZE = 24
    data = source.read(EVENT_SIZE)
    if len(data) < EVENT_SIZE:
        return True

    # Unpack the binary data
    # type (H), code (H), value (I) are the last 3 fields
    _, _, ev_type, ev_code, ev_value = struct.unpack('qqHHI', data)
    
    # EV_KEY (1) and BTN_TOUCH (330). ev_value 1 is press, 0 is release.
    # We trigger the change only on the 'press' (1)
    if ev_type == 1 and ev_code == 330 and ev_value == 1:
        manager.cycle_backlight()

    return True

def play_kiosk_video():
    Gst.init(None)

    brightness_manager = BrightnessManager()

    VIDEO_PATH = "file:///root/python/vid.mp4"

    SINK_STR = (
        "videoconvert ! "
        "video/x-raw,format=BGRx ! "
        "kmssink driver-name=mxsfb-drm connector-id=37 plane-id=31 can-scale=false" )

    # Create the playbin element
    pipeline = Gst.ElementFactory.make("playbin", "player")
    pipeline.set_property("uri", VIDEO_PATH)
                
    # Set the custom video sink
    video_sink = Gst.parse_bin_from_description(SINK_STR, True)
    pipeline.set_property("video-sink", video_sink)
                                
    # Set audio to fakesink (prevents errors if no audio hardware is found)
    audio_sink = Gst.ElementFactory.make("fakesink", "audio-fake")
    pipeline.set_property("audio-sink", audio_sink)

    # --- INPUT MONITORING ---
    try:
        # Open the touch device in binary read mode
        touch_fd = open("/dev/input/event2", "rb")
        # Add to GLib loop so it doesn't block the video
        GLib.io_add_watch(touch_fd, GLib.IO_IN, handle_touch, brightness_manager)
    except FileNotFoundError:
        print("Warning: /dev/input/event2 not found. Brightness control disabled.")
    
    GLib.timeout_add(30000, brightness_manager.change_led_colour)

    # --- LOOPING LOGIC ---
    bus = pipeline.get_bus()
    bus.add_signal_watch()

    def on_message(bus, msg):
        t = msg.type
        if t == Gst.MessageType.EOS:
            print("End of video. Looping...")
            pipeline.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT, 0)
        elif t == Gst.MessageType.ERROR:
            err, debug = msg.parse_error()
            print(f"Error: {err}")
            loop.quit()
        return True

    bus.connect("message", on_message)

    # Start playback
    print(f"Starting playback: {VIDEO_PATH}")
    pipeline.set_state(Gst.State.PLAYING)

    loop = GLib.MainLoop()
    try:
        loop.run()
    except KeyboardInterrupt:
        print("\nStopping player...")
    finally:
        pipeline.set_state(Gst.State.NULL)

if __name__ == "__main__":
    play_kiosk_video()
