import time

from .logger import logger


def addlogger(name: str):
    logger.add(
        sink=f"../logs/exploits/{name}.log",
        format="<d>{time:HH:mm:ss.SS}</d> | <level>{level:^8}</level> | {message}",
        enqueue=True,
        filter=lambda record: record["extra"]["file"] == f"{name}.log",
        level="DEBUG",
        colorize=True,
    )


def launchAttack(exploit, target_ip: str, gametickQueue):
    log = logger.bind(file=f"exploits.log")
    gametickQueue.get()
    level = "INFO"
    start = time.time()
    flags_iterable = exploit(target_ip)
    if isinstance(flags_iterable, str):
        flags_iterable: list[str] = [flags_iterable]
    for flag in flags_iterable:
        save_flags(flag, exploit.__module__)  # type: ignore

    elapsed = time.time() - start
    gametickQueue.task_done()

    log.log(level, f"{exploit.__module__} took {elapsed:.2f} seconds")
    return
