from micropython import const  # NOQA
import display_driver_framework

_WIDTH = const(480)
_HEIGHT = const(320)

_BUFFER_SIZE = _WIDTH * _HEIGHT * 3
import lcd_bus  # NOQA

print("Intializing LCD Bus")
bus = lcd_bus.SDLBus(flags=0)

print("Creating buffers")
buf1 = bus.allocate_framebuffer(_BUFFER_SIZE, 0)
buf2 = bus.allocate_framebuffer(_BUFFER_SIZE, 0)

import lvgl as lv  # NOQA

import sdl_display  # NOQA

print("Initializing LVGL")
lv.init()
print("Creating Display")
display = sdl_display.SDLDisplay(
    data_bus=bus,
    display_width=480,
    display_height=320,
    frame_buffer1=buf1,
    frame_buffer2=buf2,
    color_space=lv.COLOR_FORMAT.RGB888
)
print(display)
print("Initializing display")
display.init()

# print("Adding display to display driver")
# display_driver_framework.DisplayDriver._displays.append(display)

import sdl_pointer

print("Creating SDL pointer")
mouse = sdl_pointer.SDLPointer()
print(mouse)

print("Starting task handler")
import task_handler
th = task_handler.TaskHandler()

print("Activating screen")
scrn = lv.screen_active()
print("Setting screen style")
scrn.set_style_bg_color(lv.color_hex(0x000000), 0)
print("Creating Slider")
slider = lv.slider(scrn)
print("Setting slider size")
slider.set_size(400, 25)
print("Centering slider")
slider.center()


def on_value_changed(_):
    print('VALUE_CHANGED:', slider.get_value())

print("Adding callback to slider")
slider.add_event_cb(on_value_changed, lv.EVENT.VALUE_CHANGED, None)

# import time

# while True:
#     lv.tick_inc(5)
#     lv.timer_handler()
#     time.sleep_ms(5)