import os

from cv_analysis.config import get_config
from cv_analysis.utils.display import save_image

CV_CONFIG = get_config()


class VisualLogger:
    def __init__(self, level, output_folder):
        self.level = level
        self.output_folder = output_folder
        if not os.path.exists(self.output_folder):
            os.mkdir(self.output_folder)

    def _save(self, img, name):
        output_path = os.path.join(self.output_folder, name)
        save_image(img, output_path)

    def info(self, img, name):
        if self._level_is_info():
            self._save(img, name)

    def debug(self, img, name):
        if self._level_is_debug():
            self._save(img, name)

    def all(self, img, name):
        if self._level_is_debug():
            self._save(img, name)

    def _level_is_info(self):
        return self.level in {"INFO", "DEBUG", "ALL"}

    def _level_is_debug(self):
        return self.level in {"DEBUG", "ALL"}

    def _level_is_all(self):
        return self.level == "ALL"


vizlogger = VisualLogger(CV_CONFIG.visual_logging_level, CV_CONFIG.visual_logging_output_folder)
