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

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):
        """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"]
        }"""
        if event.type == pygame.MOUSEMOTION:
            events_dict.setdefault("cursor", {})["position"] = event.pos
            events_dict.setdefault("cursor", {})["relative"] = event.rel
            events_dict.setdefault("cursor", {})["poscale"] = (event.pos[0] / screen_width, event.pos[1] / screen_height)
            events_dict.setdefault("cursor", {})["relscale"] = (event.rel[0] / screen_width, event.rel[1] / screen_height)
        elif event.type == pygame.MOUSEWHEEL:
            if "wheel" not in events_dict:
                events_dict["wheel"] = 0
            events_dict["wheel"] += event.y  # 远离用户为正，靠近为负
        elif event.type == pygame.MOUSEBUTTONDOWN:
            events_dict.setdefault("click", []).append(event.button)
        elif event.type == pygame.MOUSEBUTTONUP:
            if "click" in events_dict and event.button in events_dict["click"]:
                events_dict["click"].remove(event.button)
        elif event.type == pygame.WINDOWEVENT and hasattr(pygame, 'WINDOWEVENT_FOCUS_GAINED') and event.event == pygame.WINDOWEVENT_FOCUS_GAINED:
            events_dict["focus"] = 1
        elif event.type == pygame.WINDOWEVENT and hasattr(pygame, 'WINDOWEVENT_FOCUS_LOST') and event.event == pygame.WINDOWEVENT_FOCUS_LOST:
            events_dict["focus"] = 0
        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)
        elif event.type == pygame.KEYUP:
            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 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": (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"]
    }
    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.type)
                if if_matters:
                    pass # 消息式调用
            for i in self.frames.values():
                i.render()
            pygame.display.flip()
            pygame.time.delay(10)
        pygame.quit()
    def observer(self):
        def decorator(func):
            def wrapper(*args, **kwargs):
                print("注册观察者")
                result = func(info=self.event, *args, **kwargs)  # 调用原始函数
                return result
            return wrapper
        return decorator