from enum import Enum


def clear_all_variables():
    global_vars = globals()

    for var in list(global_vars):
        if not var.startswith("__") and not callable(global_vars[var]):
            global_vars[var] = None

    init_variables()


def init_variables():
    global_vars = globals()

    global boundaries
    boundaries = set()

    global patches
    patches = set()

    global loops
    loops = []

    global temp_bm_verts
    temp_bm_verts = set()

    global temp_bm_edges
    temp_bm_edges = set()

    global snap_bvh_trees
    snap_bvh_trees = []

    global last_update_hovering_timestamp
    last_update_hovering_timestamp = 0

    global last_update_snap_timestamp
    last_update_snap_timestamp = 0

    global mode
    mode = Mode.NORMAL

    global is_snap_surface
    is_snap_surface = True

    global is_auto_search_pattern
    is_auto_search_pattern = True

    global is_snap_opposite
    is_snap_opposite = True


is_running = False
running_exception = None  # 存放外部发生的错误，让他可以raise到用户界面

# 绘制曲线中
drawing_verts_snap_vertex = None
drawing_path_boundary = None


patches = set()  # 建成的补丁
boundaries = set()  # 建成的边
loops = (
    []
)  # boundary环路列表，元素是loop，loop是元组的列表，元组第一个元素是boundary，第二个元素是方向

# 记录插件创建的点和边，防止误删搞坏用户的网格，使用set，提高in语句的性能
# TODO: 網格更新的時候沒有及時更新，但是暂时没问题，不考虑这个，只在最后获取顶点数的时候有问题
temp_bm_verts = set()
temp_bm_edges = set()

# 全局设置
is_snap_surface = True
is_snap_opposite = True
is_auto_search_pattern = True

snap_bvh_trees = []  # 要吸附的bvh_tree
obj = None  # 编辑物体
obj_bm = None  # 编辑物体的bm
obj_me = None  # 编辑物体的data即mesh对象
obj_bvh_tree = None  # 编辑物体的data即mesh对象

hovering_loop = None
hovering_boundary = None

temp_merge_boundary_1 = None  # 临时变量，用于临时储存合并boundary

last_update_hovering_timestamp = 0
last_update_snap_timestamp = 0

last_mouse_co_2d = None

last_echo_time = 0


class Mode(Enum):
    NORMAL = 1
    DRAWING = 2
    MERGE_BOUNDARY = 3


mode = Mode.NORMAL

mirror_path = []

is_drawing_along_mirror = False
is_drawing_along_mirror_persist = False

is_changing_mesh = False


is_redraw_boundary = False
last_boundary_data = None
is_redraw_pattern = False
last_pattern_data = None
is_redraw_hovering = False
last_hovering_data = None
