import cv2
from lib.singleton import Singleton

class AppStatus(Singleton):
    """アプリケーションの状態を管理するシングルトンクラス"""
    def __init__(self):
        if hasattr(self, '_initialized') and self._initialized:
            return  # すでに初期化済みなら何もしない

        self.status = {}
        self._initialized = True

    def reset(self):
        """状態をリセット"""
        self.status.clear()

    def set_status(self, key, value):
        self.status[key] = value
    
    def get_status(self, key, default=None):
        return self.status.get(key, default)

    

    @property
    def is_video(self)-> bool:
        """入力動画ファイルかどうか判定"""
        return self.get_status('is_video', False)
    
    @is_video.setter
    def is_video(self, value: bool):
        self.set_status('is_video', value)

    @property
    def current_media_path(self) -> str:
        """現在のメディアファイルのパス"""
        return self.get_status('current_media_path', "")
 
    @current_media_path.setter
    def current_media_path(self, path: str):
        self.set_status('current_media_path', path) 

    @property
    def original_image(self)-> cv2.typing.MatLike :
        """元画像(処理用データの基準)"""
        return self.get_status('original_image', None)
    
    @original_image.setter
    def original_image(self, img: cv2.typing.MatLike):
        self.set_status('original_image', img)

    @property
    def processed_image(self)-> cv2.typing.MatLike :
        """処理後の画像"""
        return self.get_status('processed_image', None)

    @processed_image.setter
    def processed_image(self, img: cv2.typing.MatLike):
        self.set_status('processed_image', img)

    @property
    def processed_scale(self) -> float:
        """画像のスケール"""
        return self.get_status('processed_scale', 1.0)

    @processed_scale.setter
    def processed_scale(self, value: float):
        self.set_status('processed_scale', value)

    @property
    def detected_objects(self) -> list:
        """検出結果のリスト"""
        return self.get_status('detected_objects', [])

    @detected_objects.setter
    def detected_objects(self, dets: list):
        self.set_status('detected_objects', dets)

    @property
    def detection_count(self) -> int:
        """検出結果の数"""
        return len(self.detected_objects) if self.detected_objects else 0 
    
    
    @property
    def overlay_image(self) -> cv2.typing.MatLike:
        """オーバーレイ画像"""
        return self.get_status('overlay_image', None)

    @overlay_image.setter
    def overlay_image(self, img: cv2.typing.MatLike):
        self.set_status('overlay_image', img)


