##
# @file      main.py
# @brief     Example for drawing progress bars in various ways
#

import gui
import vts

current_screen = 0
def select_cb(b):
    global current_screen
    current_screen = b[8]

buttons_l = [
    gui.SUBLIST,
    [gui.CTRL_BUTTON, 80, 400, 200, 50, 28, 'Built-in', select_cb, 0],
    [gui.CTRL_BUTTON, 300, 400, 200, 50, 28, 'Rectangle', select_cb, 1],
    [gui.CTRL_BUTTON, 520, 400, 200, 50, 28, 'Rounded', select_cb, 2],
]

bar_value = [0x8000]

def builtin_bar():
    gui_l = [
        [gui.CTRL_PROGRESS, 50, 100, 700, 30, bar_value, 0xffff],
        [gui.CTRL_TEXT, 200, 265, 28, gui.OPT_CENTERX, 'Value'],
        [gui.CTRL_SLIDER, 50, 300, 300, 20, 0xffff, bar_value],
        buttons_l,
    ]
    gui.show(gui_l)

def rectangle_bar():
    def vsync_cb(l):
        rectangle_l[4][1][1] = gui.DL_VERTEX2F(51 + (698 * bar_value[0]) / 0xffff, 129)

    rectangle_l = [
        gui.SUBLIST,
        [gui.DL_COLOR_RGB(16, 16, 64)],
        [gui.PRIM_RECTS, [gui.DL_VERTEX2F(50, 100), gui.DL_VERTEX2F(750, 130)]],
        [gui.DL_COLOR_RGB(32, 192, 32)],
        [gui.PRIM_RECTS, [gui.DL_VERTEX2F(51, 101), gui.DL_VERTEX2F(400, 129)]],
        [gui.DL_COLOR_RGB(255, 255, 255)],
        buttons_l,
    ]

    gui_l = [
        [gui.EVT_VSYNC, vsync_cb],
        rectangle_l,
        [gui.CTRL_TEXT, 200, 265, 28, gui.OPT_CENTERX, 'Value'],
        [gui.CTRL_SLIDER, 50, 300, 300, 20, 0xffff, bar_value],
        buttons_l,
    ]
    gui.show(gui_l)

def rounded_bar():
    def vsync_cb(l):
        line_hw = 1 + line_hw_slider[0] * (14 / 0xffff)
        x = 51 + line_hw + bar_value[0] * ((698 - 2 * line_hw) / 0xffff)
        bar_extra_h = bar_extra_h_slider[0] * (30 / 0xffff)
        rounded_l[1][1] = gui.DL_LINE_WIDTH(line_hw)
        rounded_l[2][1][0] = gui.DL_VERTEX2F(50 + line_hw, 100 + line_hw)
        rounded_l[2][1][1] = gui.DL_VERTEX2F(750 - line_hw, 100 + line_hw)
        rounded_l[2][1][2] = gui.DL_VERTEX2F(750 - line_hw, 130 - line_hw + bar_extra_h)
        rounded_l[2][1][3] = gui.DL_VERTEX2F(50 + line_hw, 130 - line_hw + bar_extra_h)
        rounded_l[2][1][4] = gui.DL_VERTEX2F(50 + line_hw, 100 + line_hw)
        rounded_l[3][1][0] = gui.DL_VERTEX2F(50 + line_hw, 100 + line_hw)
        rounded_l[3][1][1] = gui.DL_VERTEX2F(750 - line_hw, 130 - line_hw + bar_extra_h)
        rounded_l[5][1][0] = gui.DL_VERTEX2F(51 + line_hw, 101 + line_hw)
        rounded_l[5][1][1] = gui.DL_VERTEX2F(x, 101 + line_hw)
        rounded_l[5][1][2] = gui.DL_VERTEX2F(x, 129 - line_hw + bar_extra_h)
        rounded_l[5][1][3] = gui.DL_VERTEX2F(51 + line_hw, 129 - line_hw + bar_extra_h)
        rounded_l[5][1][4] = gui.DL_VERTEX2F(51 + line_hw, 101 + line_hw)
        rounded_l[6][1][0] = gui.DL_VERTEX2F(51 + line_hw, 101 + line_hw)
        rounded_l[6][1][1] = gui.DL_VERTEX2F(x, 129 - line_hw + bar_extra_h)

    line_hw = 5
    rounded_l = [
        gui.SUBLIST,
        [gui.DL_COLOR_RGB(16, 16, 64), gui.DL_LINE_WIDTH(line_hw * 2)],
        [gui.PRIM_LINE_STRIP, [
            gui.DL_VERTEX2F(50 + line_hw, 100 + line_hw),
            gui.DL_VERTEX2F(750 - line_hw, 100 + line_hw),
            gui.DL_VERTEX2F(750 - line_hw, 130 - line_hw),
            gui.DL_VERTEX2F(50 + line_hw, 130 - line_hw),
            gui.DL_VERTEX2F(50 + line_hw, 100 + line_hw),
        ]],
        [gui.PRIM_RECTS, [
            gui.DL_VERTEX2F(50 + line_hw, 100 + line_hw),
            gui.DL_VERTEX2F(750 - line_hw, 130 - line_hw),
        ]],
        [gui.DL_COLOR_RGB(32, 192, 32)],
        [gui.PRIM_LINE_STRIP, [
            gui.DL_VERTEX2F(51 + line_hw, 101 + line_hw),
            gui.DL_VERTEX2F(749 - line_hw, 101 + line_hw),
            gui.DL_VERTEX2F(749 - line_hw, 129 - line_hw),
            gui.DL_VERTEX2F(51 + line_hw, 129 - line_hw),
            gui.DL_VERTEX2F(51 + line_hw, 101 + line_hw),
        ]],
        [gui.PRIM_RECTS, [
            gui.DL_VERTEX2F(51 + line_hw, 101 + line_hw),
            gui.DL_VERTEX2F(749 - line_hw, 129 - line_hw),
        ]],
        [gui.DL_COLOR_RGB(255, 255, 255)],
        buttons_l,
    ]

    line_hw_slider = [0x8000]
    bar_extra_h_slider = [0]
    gui_l = [
        [gui.EVT_VSYNC, vsync_cb],
        rounded_l,
        [gui.CTRL_TEXT, 600, 225, 28, gui.OPT_CENTERX, 'Height'],
        [gui.CTRL_SLIDER, 450, 260, 300, 20, 0xffff, bar_extra_h_slider],
        [gui.CTRL_TEXT, 600, 305, 28, gui.OPT_CENTERX, 'Roundness'],
        [gui.CTRL_SLIDER, 450, 340, 300, 20, 0xffff, line_hw_slider],
        [gui.CTRL_TEXT, 200, 265, 28, gui.OPT_CENTERX, 'Value'],
        [gui.CTRL_SLIDER, 50, 300, 300, 20, 0xffff, bar_value],
        buttons_l,
    ]
    gui.show(gui_l)

while True:
    [builtin_bar, rectangle_bar, rounded_bar][current_screen]()
    current_screen = -1
    while current_screen == -1:
        pass
