# Vector Graphics Library
# vgllib.py
import pygame
import uuid
import time
import threading
import copy
import queue

class Aux():
    def gettime():
        return round(time.time(), 1)
    def getprogress(task):
        return (Aux.gettime() - task["start"] / task["duration"])
    def tuple_scale(tup, mul):
        return (tup[0]*mul, tup[1]*mul)
    def pixelizer(arg, base):
        if not isinstance(base, tuple):
            base = base.size
        if isinstance(arg, int):
            return arg * base
        else:
            if len(arg) == 2:
                return (round(arg[0] * base[0]), round(arg[1] * base[1])) 
            if len(arg) == 4:
                return (round(arg[0] * base[0]), round(arg[1] * base[1]), round(arg[2] * base[0]), round(arg[3] * base[1])) 
    def eventproc(events_dict, event, window_size):
        """events = {
            "cursor": {
                "position": (15,12),
                "relative": (13,11),
                "poscale": (0.1,0.5),
                "relscale": (0.05,0.1),
            },
            "wheel": 1, # 远离用户为 1, 靠近为 -1, 无动作为 0
            "click": [1, 2], # 被按下的按钮
            #"focus": 1, # 或 0
            "key": ["ctrl", "k"],
            "delta": ""
        }"""
        if event.type == pygame.MOUSEMOTION:
            events_dict["cursor"]["position"] = event.pos
            events_dict["cursor"]["relative"] = event.rel
            events_dict["cursor"]["poscale"] = (round(event.pos[0] / window_size[0], 3), round(event.pos[1] / window_size[1],3))
            events_dict["cursor"]["relscale"] = (round(event.rel[0] / window_size[0], 3), round(event.rel[1] / window_size[1], 3))
            events_dict["delta"] = "cursor"
        elif event.type == pygame.MOUSEWHEEL:
            events_dict["wheel"] = event.y  # 远离用户为正，靠近为负
            events_dict["delta"] = "wheel"
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button not in events_dict["click"]:
                events_dict.setdefault("click", []).append(event.button)
            events_dict["delta"] = "click"
        elif event.type == pygame.MOUSEBUTTONUP:
            if event.button in events_dict["click"]:
                events_dict["click"].remove(event.button)
            events_dict["delta"] = "click"
        elif event.type == pygame.KEYDOWN:
            keys = events_dict.setdefault("key", [])
            key_name = pygame.key.name(event.key).lower()
            # 简单的修饰键处理, 为兼容部分精简键盘 故合并左右功能键
            if key_name in ["left ctrl", "right ctrl"]:
                if "ctrl" not in keys:
                    keys.append("ctrl")
            elif key_name in ["left shift", "right shift"]:
                if "shift" not in keys:
                    keys.append("shift")
            elif key_name in ["left alt", "right alt"]:
                if "alt" not in keys:
                    keys.append("alt")
            elif len(key_name) == 1:  # 处理字母和数字
                keys.append(key_name)
            events_dict["delta"] = "key"
        elif event.type == pygame.KEYUP:
            events_dict["delta"] = "key"
            if "key" in events_dict:
                key_name = pygame.key.name(event.key).lower()
                if key_name in ["left ctrl", "right ctrl"] and "ctrl" in events_dict["key"]:
                    events_dict["key"].remove("ctrl")
                elif key_name in ["left shift", "right shift"] and "shift" in events_dict["key"]:
                    events_dict["key"].remove("shift")
                elif key_name in ["left alt", "right alt"] and "alt" in events_dict["key"]:
                    events_dict["key"].remove("alt")
                elif len(key_name) == 1 and key_name in events_dict["key"]:
                    events_dict["key"].remove(key_name)
        else:
            #print("不重要事件")
            return 0
        return 1
class Anchor(object):
    """
    "锚定点" 对象
    """
    poscale = (0, 0)
    fixed = 3
    def __init__(self, *args):
        if isinstance(args[0], tuple) or isinstance(args[0], Anchor):
            if isinstance(args[0], Anchor):
                self.poscale = args[0].poscale
            else:
                self.poscale = args[0]
        else:
            self.poscale = (args[0], args[1])
    def x(self):
        return self.poscale[0]
    def y(self):
        return self.poscale[1]
    def __add__(self, other):
        if isinstance(other, tuple):
            return Anchor((self.poscale[0] + other[0], self.poscale[1] + other[1]))
        elif isinstance(other, Anchor):
            return Anchor((self.poscale[0] + other.poscale[0], self.poscale[1] + other.poscale[1]))
        else:
            raise TypeError("不支持的加法操作数类型")
    def __mul__(self, other):
        "向屏幕中心倍增/倍缩"
        if isinstance(other, tuple):
            return Anchor((0.5 + other[0] * (self.poscale[0] - 0.5), 0.5 + other[1] * (self.poscale[1] - 0.5)))
        elif isinstance(other, (int, float)):
            return Anchor((0.5 + other * (self.poscale[0] - 0.5), 0.5 + other * (self.poscale[1] - 0.5)))
        else:
            raise TypeError("不支持的乘法操作数类型")
    def __imul__(self, other):
        result = self.__mul__(other)
        self.poscale = result.poscale
        return self
    def __truediv__(self, other):
        "乘以倒数"
        if isinstance(other, (int, float)):
            if other == 0:
                return Anchor((0.5, 0.5))
            else:
                return self.__mul__(1 / other)
        elif isinstance(other, Anchor) or isinstance(other, tuple):
            divisor_x = other[0] if isinstance(other, tuple) else other.x()
            divisor_y = other[1] if isinstance(other, tuple) else other.y()
            # TODO: 错误代码, 应当予以修正
            if divisor_x == 0 and divisor_y == 0:
                return Anchor((0.5, 0.5))
            if divisor_x == 0 and divisor_y == 0:
                return Anchor((0.5, 0.5))
            else:
                return self.__mul__((1 / divisor_x, 1 / divisor_y))
        else:
            raise TypeError("不支持的除法操作数类型")
    def __itruediv__(self, other):
        "就地除法"
        result = self.__truediv__(other)
        self.poscale = result.poscale
        return self
    def __str__(self):
        if self.fixed is not None:
            return f"({round(self.poscale[0], self.fixed)}, {round(self.poscale[1], self.fixed)})"
        else:
            return f"({self.poscale[0]}, {self.poscale[1]})"
class Element(object):
    is_hide = False
    is_template = True
    attached = None
    attached_frame = None
    attached_window = None
    tasks = dict()
    def __init__(self, name="Element"):
        self.name = name
    def attach(self, frame_object, clone_name=""):
        if clone_name == "":
            clone_name = str(uuid.uuid4())
        clone = copy.deepcopy(self)
        clone.is_template = False
        frame_object.elements[clone_name] = clone
        clone.attached_frame = frame_object
        clone.attached_window = frame_object.attached_window
        return clone
    def teleport(self, delta: tuple):
        if not self.is_template:
            pass
    def add_task(self, group, start_time, duration, **kwargs):
        if group not in self.tasks():
            self.tasks[group] = list() 
        self.tasks[group].append({"start": start_time, "duration":duration, "para":kwargs})
    def render():
        print("未配置渲染器")
        pass
    def update():
        pass

class Frame(object):
    surface = None
    elements = dict()
    is_hide = False
    is_template = True
    attached_window = None
    poscale: tuple = (0, 0) # 左上角, 对于每个实例的位置矢量
    def __init__(self, name="Frame"):
        self.name = name
    def attach(self, window_object, poscale, clone_name=""):
        if clone_name == "":
            clone_name = str(uuid.uuid4())
        clone = copy.deepcopy(self)
        clone.surface = pygame.Surface(window_object.size)
        clone.poscale = poscale
        clone.is_template = False
        clone.attached_window = window_object
        window_object.frames[clone_name] = clone
        return window_object.frames[clone_name]
    def render(self):
        for i in self.elements.values():
            i.render()
        self.attached_window.screen.blit(self.surface, self.poscale)

class Window(object):
    frames = dict()
    events = {
        "cursor": {
            "position": (0,0),
            "relative": (0,0),
            "poscale": (0,0),
            "relscale": (0,0),
        },
        "wheel": 0, # 远离用户为 1, 靠近为 -1, 无动作为 0
        "click": [], # 被按下的按钮
        "focus": 1, # 或 0
        "key": [],
        "delta": [] # 变化的键值, 避免多次调用
    }
    """{
        "cursor": {
            "position": (15,12),
            "relative": (13,11),
            "poscale": (0.1,0.5),
            "relscale": (0.05,0.1),
        },
        "wheel": 1, # 远离用户为 1, 靠近为 -1, 无动作为 0
        "click": [1, 2], # 被按下的按钮
        "focus": 1, # 或 0
        "key": ["ctrl", "k"],
        "delta": ["key"]
    }"""
    observers = list()
    def __init__(self, title="Vector Graphic Layer Window", size: tuple=(1024,768)):
        self.title = title
        self.size = size
        self.running = 1
        pygame.init()
        self.screen = pygame.display.set_mode(size)
        pygame.display.set_caption(title)
    def set_title(self, title):
        self.title = title
        pygame.display.set_caption(title)
    def start(self):
        self.thr = threading.Thread(target=self.main_loop, name="main_loop")
        self.thr.start()
    def kill(self):
        self.running = 0
    def main_loop(self):
        while self.running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = 0
                if_matters = Aux.eventproc(self.events, event, self.size)
                if if_matters:
                    #print(self.observers)
                    for i in self.observers:
                        i(self.events)
                    #print(self.events)
                
            for i in self.frames.values():
                i.render()
            pygame.display.flip()
            pygame.time.delay(10)
        pygame.quit()
    def observerize(self, func):
        def wrapper():
            self.observers.append(func)
        return wrapper
    def remove_observer(self, func):
        self.observers.remove(func)