##
# @module    screen_base
# @brief     Base_screen
# @author    Ryan Davison-Perace
# @date      Dec 2019
# @copyright Racelogic Ltd., 2019
#

import gui
import PY_VTS.gui.colours as Colour
from PY_VTS.mgr.GV import *

class Screen_Base:

    dark_mode = False
    text_col = [Colour.BLACK]
    background_col = [Colour.WHITE]

    tags = {}
    tags_l = [gui.SUBLIST]

    class Tag:
        Num_Tags = 0
        def __init__(self,func,Key):
            self.Tag = self.inc_tag()
            self.Key = Key
            self.func = func
            self.gui_l = [gui.PARAM_TAG_REGISTER, self.func]

        @classmethod
        def inc_tag(cls):
            cls.Num_Tags += 1
            return cls.Num_Tags


    def __init__(self):

        self.gui_l = []
        self.vsync_active = False
        self.vsync_cbs = {}
        self.swiping = False
        self.swipe_r = 50
        self.swipe_action_cbs = {}
        self.title = [gui.CTRL_TEXT, 30, 7, 8, 0, '']

        self.gui_l_preheader = [gui.SUBLIST]

        self.gui_l = [
            [gui.EVT_SWIPE, self.swipe_r, self.swipe_cb],
            [gui.EVT_VSYNC, self.vsync_cb],

            # [gui.EVT_PRESS, view_press_cb],

            self.tags_l,



            [self.background_col],
            [gui.DL_BEGIN(gui.PRIM_RECTS), gui.DL_VERTEX2F(0,0), gui.DL_VERTEX2F(800,480)],

            #self.gui_l_preheader,

            [Colour.HEADERS],
            [gui.DL_BEGIN(gui.PRIM_RECTS), gui.DL_VERTEX2F(0,0), gui.DL_VERTEX2F(800,65)],
            [gui.DL_BEGIN(gui.PRIM_RECTS), gui.DL_VERTEX2F(0,415), gui.DL_VERTEX2F(800,480)],

            [Colour.WHITE],
            self.title,
        ]

    def teardown(self):     # TO BE OVERWRITTEN BY INHERITED CLASS
        pass

    def setup(self):     # TO BE OVERWRITTEN BY INHERITED CLASS
        pass

    def swipe_cb(self, gui_l, start):
        self.swiping = start

    def swipe_vsync(self,*args, **kwargs):
        if (self.swiping):
            si = gui.swipe_info()
            flags = 0
            if si.dx <= -self.swipe_r:
                flags |= 1
            elif si.dx >= self.swipe_r:
                flags |= 2
            if si.dy <= -self.swipe_r:
                flags |= 4
            elif si.dy >= self.swipe_r:
                flags |= 8
            if (flags == 1):
                if 'left' in self.swipe_action_cbs:
                    self.swipe_action_cbs['left']()
                    self.swiping = False
            elif (flags == 2):
                if 'right' in self.swipe_action_cbs:
                    self.swipe_action_cbs['right']()
                    self.swiping = False
            elif (flags == 4):
                if 'up' in self.swipe_action_cbs:
                    self.swipe_action_cbs['up']()
                    self.swiping = False
            elif (flags == 8):
                if 'down' in self.swipe_action_cbs:
                    self.swipe_action_cbs['down']()
                    self.swiping = False

    def add_swipe_cb(self, func,dir):
        if not self.swipe_vsync in self.vsync_cbs:
            self.add_vsync_cb(self.swipe_vsync,"swipe")
        if dir.lower() == "up":
            self.swipe_action_cbs["up"] = func
        elif dir.lower() == "down":
            self.swipe_action_cbs["down"] = func
        elif dir.lower() == "left":
            self.swipe_action_cbs["left"] = func
        elif dir.lower() == "right":
            self.swipe_action_cbs["right"] = func

    def del_swipe_cb(self, key, dir):
        if dir.lower() == "up":
            del self.swipe_action_cbs["up"]
        elif dir.lower() == "down":
            del self.swipe_action_cbs["down"]
        elif dir.lower() == "left":
            del self.swipe_action_cbs["left"]
        elif dir.lower() == "right":
            del self.swipe_action_cbs["right"]

        if not self.swipe_action_cbs:
            self.del_vsync_cb("swipe")

    @classmethod
    def add_tag(cls, func, key):
        cls.tags[key] = cls.Tag(func, key)
        cls.tags_l.append(cls.tags[key].gui_l)

    @classmethod
    def del_tag(cls, key):
        if cls.tags[key]:
            del cls.tags_l[cls.tags[key].Tag]
            del cls.tags[key]

    def set_title(self,Title):
        self.title[5] = Title

    def darkmode_toggle_cb(self):
        self.dark_mode = not self.dark_mode
        if self.dark_mode:
            self.text_col[0] = Colour.WHITE
            self.background_col[0] = Colour.BLACK
        else:
            self.text_col[0] = Colour.BLACK
            self.background_col[0] = Colour.WHITE
        gui.redraw()

    def vsync_cb(self, *args, **kwargs):
        if not self.vsync_active:
            self.vsync_active = True
            for func in self.vsync_cbs:
                self.vsync_cbs[func](*args, **kwargs)
            self.vsync_active = False
        gui.redraw()

    def add_vsync_cb(self, func, key):
        self.vsync_cbs[key] = func

    def del_vsync_cb(self, key):
        if self.vsync_cbs[key]:
            del self.vsync_cbs[key]
