from micropython import const  # NOQA
import display_driver_framework

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

_BUFFER_SIZE = _WIDTH * _HEIGHT * 3
import lcd_bus  # NOQA

bus = lcd_bus.SDLBus(flags=0)

buf1 = bus.allocate_framebuffer(_BUFFER_SIZE, 0)
buf2 = bus.allocate_framebuffer(_BUFFER_SIZE, 0)

import lvgl as lv  # NOQA

import sdl_display  # NOQA

lv.init()
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
)

display.init()

import sdl_pointer

mouse = sdl_pointer.SDLPointer()
print(mouse)

import task_handler
th = task_handler.TaskHandler()

scrn = lv.screen_active()
scrn.set_style_bg_color(lv.color_hex(0x000000), 0)

tabview = lv.tabview(scrn)
tabview.set_tab_bar_position(lv.DIR.LEFT)
tabview.set_tab_bar_size(80)

tab_buttons = tabview.get_tab_bar()
tab_buttons.set_style_text_color(lv.color_hex(0xFFFFFF), 0)
#f58700 dark orange
tab_buttons.set_style_border_side(lv.BORDER_SIDE.RIGHT, lv.PART.ITEMS | lv.STATE.CHECKED)
tab_buttons.set_style_text_font(lv.font_montserrat_16,0)
home_tab = tabview.add_tab(lv.SYMBOL.HOME)
target_tab = tabview.add_tab(lv.SYMBOL.LIST)
settings_tab = tabview.add_tab(lv.SYMBOL.SETTINGS)

# Button on settings

btn = lv.button(settings_tab)
btn.align(lv.ALIGN.CENTER, 0, 0)
btn.set_size(150, 100)
btn_label = lv.label(btn)
btn_label.align(lv.ALIGN.CENTER, 0, 0)
btn_label.set_style_text_font(lv.font_montserrat_14, 0)
btn_label.set_style_text_align(lv.STATE.DEFAULT, lv.TEXT_ALIGN.CENTER)
btn_label.set_text("Button")
g_btn_count = 0

def btn_event_cb(e: lv.event_t):
    btn = e.get_target_obj()
    label = btn.get_child(0)
    if e.get_code() == lv.EVENT.CLICKED:
        global g_btn_count 
        g_btn_count += 1
        label.set_text(f"Button: {g_btn_count}")
    
btn.add_event_cb(btn_event_cb, lv.EVENT.ALL, None)


# lv_obj_set_style_border_side(tab_buttons, LV_BORDER_SIDE_RIGHT, LV_PART_ITEMS | LV_STATE_CHECKED);

#     /*Add 3 tabs (the tabs are page (lv_page) and can be scrolled*/
#     lv_obj_t * tab1 = lv_tabview_add_tab(tabview, "Tab 1");
#     lv_obj_t * tab2 = lv_tabview_add_tab(tabview, "Tab 2");
#     lv_obj_t * tab3 = lv_tabview_add_tab(tabview, "Tab 3");
#     lv_obj_t * tab4 = lv_tabview_add_tab(tabview, "Tab 4");
#     lv_obj_t * tab5 = lv_tabview_add_tab(tabview, "Tab 5");

#     lv_obj_set_style_bg_color(tab2, lv_palette_lighten(LV_PALETTE_AMBER, 3), 0);
#     lv_obj_set_style_bg_opa(tab2, LV_OPA_COVER, 0);

#     /*Add content to the tabs*/
#     lv_obj_t * label = lv_label_create(tab1);
#     lv_label_set_text(label, "First tab");

#     label = lv_label_create(tab2);
#     lv_label_set_text(label, "Second tab");

#     label = lv_label_create(tab3);
#     lv_label_set_text(label, "Third tab");

#     label = lv_label_create(tab4);
#     lv_label_set_text(label, "Forth tab");

#     label = lv_label_create(tab5);
#     lv_label_set_text(label, "Fifth tab");

#     lv_obj_remove_flag(lv_tabview_get_content(tabview), LV_OBJ_FLAG_SCROLLABLE);


# # Button
# btn = lv.button(scrn)
# btn.set_pos(10, 10)
# btn.set_size(120, 50)
# btn_label = lv.label(btn)
# btn_label.set_text("Button")
# g_btn_count = 0

# def btn_event_cb(e: lv.event_t):
#     btn = e.get_target_obj()
#     label = btn.get_child(0)
#     if e.get_code() == lv.EVENT.CLICKED:
#         global g_btn_count 
#         g_btn_count += 1
#         label.set_text(f"Button: {g_btn_count}")
    
# btn.add_event_cb(btn_event_cb, lv.EVENT.ALL, None)
