import threading

from loguru import logger

from .reporters.pc_reporter import PcReporter
from .reporters.site_reporter import SiteReporter
from .reporters.wap_reporter import WapReporter


class Reporter:
    """举报器,目前有三个渠道,以后可以继续扩展"""

    def __init__(self, mode: list[str]):
        # 初始化其他的 reporter
        self.reporters = {
            "pc": PcReporter(),
            "wap": WapReporter(),
            "site": SiteReporter(),
        }

        self.worker_thread = None
        self.ev = threading.Event()
        self.status = 1

        self.mode = mode

    def start(self):
        self.worker_thread = threading.Thread(
            target=self.worker, name="REPORTER", daemon=True
        )
        self.worker_thread.start()

    def wait(self):
        self.worker_thread.join()

    # noinspection DuplicatedCode
    def cli_start(self):
        for mode in self.mode:
            if mode == "pc":
                self.reporters["pc"].run()
            elif mode == "wap":
                self.reporters["wap"].run()
            elif mode == "site":
                self.reporters["site"].run()
            else:
                logger.error(f"参数错误: {mode}")
                continue

    def stop(self):

        for mode in self.mode:
            if mode == "pc":
                self.reporters["pc"].stop()
            elif mode == "wap":
                self.reporters["wap"].stop()
            elif mode == "site":
                self.reporters["site"].stop()
            else:
                logger.error(f"参数错误: {mode}")
                continue

        self.status = 0
        self.ev.set()

    # noinspection DuplicatedCode
    def worker(self):
        while self.status:
            for mode in self.mode:
                if mode == "pc" and self.status:
                    self.reporters["pc"].run()
                elif mode == "wap" and self.status:
                    self.reporters["wap"].run()
                elif mode == "site" and self.status:
                    self.reporters["site"].run()
                else:
                    logger.error(f"参数错误: {mode}")
                    continue

            self.ev.wait(10)
