import ctypes

# Load the shared library
lib = ctypes.CDLL("libetacpu.so")

# Define the function signature
lib.get_cpu_info.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
lib.get_cpu_info.restype = None

# Allocate buffers for the strings
vendor_str = ctypes.create_string_buffer(100)
cpu_codename = ctypes.create_string_buffer(100)


# Call the function
lib.get_cpu_info(vendor_str, cpu_codename)
generation = cpu_codename.value.decode("utf-8")


class CPU:
    def __init__(self, model, vendor, microcode, core_no, thread_no, generation):
        self.model = model
        self.vendor = vendor
        self.microcode = microcode
        self.core_no = core_no
        self.thread_no = thread_no
        self.generation = generation


def get():
    with open("/proc/cpuinfo", "r") as cpuinfo:

        lines = cpuinfo.readlines()
        cpu_model_line = None
        cpu_vendor_line = None
        cpu_microcode_line = None
        cpu_core_line = None
        cpu_thread_line = None
        for line in lines:
            if "model name" in line:
                cpu_model_line = line
            if "vendor_id" in line:
                cpu_vendor_line = line
            if "microcode" in line:
                cpu_microcode_line = line
            if "cpu core" in line:
                cpu_core_line = line
            if "siblings" in line:
                cpu_thread_line = line
        cpu_model = cpu_model_line.split("\t:")[1].strip()
        cpu_vendor = cpu_vendor_line.split("\t:")[1].strip()
        cpu_microcode = cpu_microcode_line.split("\t:")[1].strip()
        cpu_microcode = int(cpu_microcode, 16)
        cpu_core = cpu_core_line.split("\t:")[1].strip()
        cpu_thread = cpu_thread_line.split("\t:")[1].strip()
        return CPU(
            cpu_model,
            cpu_vendor,
            cpu_microcode,
            cpu_core,
            cpu_thread,
            generation=generation,
        )
