import pyMeow as pm

# ESP 오프셋
class ESPOffsets:
    name = 0x205
    health = 0xEC
    armor = 0xF0
    team = 0x30C
    pos = 0x4      # Head position
    fpos = 0x28    # Foot position

# 색상
class Colors:
    cyan = None
    orange = None
    white = None
    black = None

def init_colors():
    """오버레이 초기화 후 색상을 초기화합니다."""
    Colors.cyan = pm.get_color("cyan")
    Colors.orange = pm.get_color("orange")
    Colors.white = pm.get_color("white")
    Colors.black = pm.get_color("black")

class Entity:
    def __init__(self, proc, addr):
        self.proc = proc
        self.addr = addr
        
        self.health = pm.r_int(proc, addr + ESPOffsets.health)
        if self.health <= 0 or self.health > 10000:
            raise Exception("Entity is not alive or invalid.")
        self.name = pm.r_string(proc, addr + ESPOffsets.name)
        self.armor = pm.r_int(proc, addr + ESPOffsets.armor)
        self.team = pm.r_int(proc, addr + ESPOffsets.team)
        self.color = Colors.cyan if self.team else Colors.orange
        self.pos3d = pm.r_vec3(proc, self.addr + ESPOffsets.pos)
        self.fpos3d = pm.r_vec3(proc, self.addr + ESPOffsets.fpos)
        self.pos2d = self.fpos2d = None
        self.head = self.width = self.center = None

    def wts(self, vm):
        try:
            self.pos2d = pm.world_to_screen(vm, self.pos3d)
            self.fpos2d = pm.world_to_screen(vm, self.fpos3d)
            self.head = self.fpos2d["y"] - self.pos2d["y"]
            self.width = self.head / 2
            self.center = self.width / 2
            return True
        except:
            return False

    def draw_box(self):
        pm.draw_rectangle(
            posX=self.pos2d["x"] - self.center,
            posY=self.pos2d["y"] - self.center / 2,
            width=self.width,
            height=self.head + self.center / 2,
            color=pm.fade_color(self.color, 0.3),
        )
        pm.draw_rectangle_lines(
            posX=self.pos2d["x"] - self.center,
            posY=self.pos2d["y"] - self.center / 2,
            width=self.width,
            height=self.head + self.center / 2,
            color=self.color,
            lineThick=1.2,
        )

    def draw_name(self):
        text_size = pm.measure_text(self.name, 15) / 2
        pm.draw_text(
            text=self.name,
            posX=self.pos2d["x"] - text_size,
            posY=self.pos2d["y"],
            fontSize=15,
            color=Colors.white,
        )

    def draw_health(self):
        pm.draw_circle_sector(
            centerX=self.pos2d["x"] - self.center,
            centerY=self.pos2d["y"] - self.center / 2,
            radius=self.center / 3 + 2,
            startAngle=0,
            endAngle=360,
            segments=0,
            color=Colors.black,
        )
        pm.draw_circle_sector(
            centerX=self.pos2d["x"] - self.center,
            centerY=self.pos2d["y"] - self.center / 2,
            radius=self.center / 3,
            startAngle=0,
            endAngle=360 / 100 * min(self.health, 100),
            segments=0,
            color=self.color,
        )

def process_entities(proc, ent_buffer, v_matrix, enable_esp):
    valid_entities = []
    for addr in ent_buffer:
        if addr == 0: continue
        try:
            ent = Entity(proc, addr)
            valid_entities.append(ent)
            if enable_esp and ent.wts(v_matrix):
                ent.draw_box()
                ent.draw_name()
                ent.draw_health()
        except:
            continue
    return valid_entities
