import logging
import logging.handlers
import re
import traceback


class Logger:
    def __init__(self, name):
        self.logger = logging.getLogger(name)

    def warning(self, msg, obj: Exception = None):
        self.logger.warning(self.format_message(msg, obj))

    def info(self, msg, obj: Exception = None):
        self.logger.info(self.format_message(msg, obj))

    def error(self, msg, obj: Exception = None):
        self.logger.error(self.format_message(msg, obj))

    def debug(self, msg, obj: Exception = None):
        self.logger.debug(self.format_message(msg, obj))

    def log_chat(self, conn_id, channel, sender, msg):
        if sender:
            self.info(f"({conn_id}) [{channel}] {sender}: {self.format_chat_message(msg)}")
        else:
            self.info(f"({conn_id}) [{channel}] {self.format_chat_message(msg)}")

    def log_tell(self, conn_id, direction, sender, msg):
        self.info(f"({conn_id}) {direction.capitalize()} {sender}: {self.format_chat_message(msg)}")

    def format_chat_message(self, msg):
        msg = re.sub(r"<a\s+href=\".+?[^\\]\">", "[link]", msg, flags=re.UNICODE | re.DOTALL)
        msg = re.sub(r"<a\s+href='.+?'>", "[link]", msg, flags=re.UNICODE | re.DOTALL)
        msg = re.sub(r"<a\s+href=user://\w+?>(\w+?)</a>", "\1", msg, flags=re.UNICODE | re.DOTALL)

        msg = re.sub(r"<font\s+.+?>", "", msg, flags=re.UNICODE)
        msg = re.sub("</font>", "", msg, flags=re.UNICODE)
        msg = re.sub("</a>", "[/link]", msg, flags=re.UNICODE)
        return msg

    def format_message(self, msg, obj):
        if obj:
            return msg + "\n" + traceback.format_exc()
        else:
            return msg
