"""LoggingController class module."""

from typing import List, Optional

from .log_manager import LogManager
from ..constants import LogLevel, TextColor


class LoggingController:
    """
    Handles all logging responsibilities using LogManager.
    """

    def __init__(self, log_manager: LogManager) -> None:
        """Initialize with a given LogManager."""
        self._log_manager = log_manager

    def log(
        self,
        level: LogLevel,
        message: str,
        color: TextColor
    ) -> Optional[str]:
        """
        Logs a message at the given level with color.
        Returns a timestamp if message was actually logged.
        """
        return self._log_manager.log(level, message, color)

    def debug(self, message: str, color: TextColor = TextColor.RESET) -> None:
        """Logs a debug-level message."""
        self.log(LogLevel.DEBUG, message, color)

    def info(self, message: str, color: TextColor = TextColor.RESET) -> None:
        """Logs an info-level message."""
        self.log(LogLevel.INFO, message, color)

    def warning(self, message: str, color: TextColor = TextColor.RESET) -> None:
        """Logs a warning-level message."""
        self.log(LogLevel.WARNING, message, color)

    def error(self, message: str, color: TextColor = TextColor.RESET) -> None:
        """Logs an error-level message."""
        self.log(LogLevel.ERROR, message, color)

    def critical(self, message: str, color: TextColor = TextColor.RESET) -> None:
        """Logs a critical-level message."""
        self.log(LogLevel.CRITICAL, message, color)

    def log_multiline(self,
                      level: LogLevel,
                      text_block: str,
                      color: TextColor = TextColor.RESET) -> None:
        """
        Logs multiple lines from a text block, splitting by newlines.
        """
        self._log_manager.log_multiline(level, text_block, color)

    def clear_logs(self) -> None:
        """Clears all in-memory logs."""
        self._log_manager.clear_logs()

    def close_file(self) -> None:
        """Closes the file manager if in use."""
        self._log_manager.close_file()

    def get_lines(self) -> List[str]:
        """Fetches the current log lines."""
        return self._log_manager.get_lines()

    @property
    def log_manager(self) -> LogManager:
        """Returns the underlying LogManager."""
        return self._log_manager

    @log_manager.setter
    def log_manager(self, manager: LogManager) -> None:
        """Sets a new LogManager instance."""
        self._log_manager = manager
