import warnings
from UniTAP.dev.modules.capturer.result_object import ResultObject
from UniTAP.dev.ports.modules.internal_utils.image_formats import PictureFileFormat, VideoFileFormat
from UniTAP.dev.ports.modules.internal_utils.image_utils import save_video_to_bin, save_video_to_mp4
from UniTAP.utils.uicl_api import video_frame_save_to_file, ImageFileFormat


class ResultVideoObject(ResultObject):
    """
    Class `ResultVideoObject` inherited from class `ResultObject`.
    Class `ResultVideoObject` allows saving captured frames to image `save_image_to_file`.
    Also has all the `ResultObject` functionality.
    """
    def __init__(self):
        super().__init__()

    def save_image_to_file(self, file_format: PictureFileFormat, path: str, index: int):
        """

        Saving selected video frame to file. Supported file formats describe in `PictureFileFormat`.

        Args:
            file_format (`PictureFileFormat`) - file format
            path (str) - path to save
            index (int) - number of video frame in list
        """
        if len(self.buffer) > 0:
            if file_format == PictureFileFormat.BIN:
                video_frame_save_to_file(video_frame=self.buffer[index], path=path, file_type=ImageFileFormat.IFF_BIN)
            elif file_format == PictureFileFormat.BMP:
                video_frame_save_to_file(video_frame=self.buffer[index], path=path, file_type=ImageFileFormat.IFF_BMP)
            elif file_format == PictureFileFormat.PPM:
                video_frame_save_to_file(video_frame=self.buffer[index], path=path, file_type=ImageFileFormat.IFF_PPM)
            elif file_format == PictureFileFormat.DSC:
                video_frame_save_to_file(video_frame=self.buffer[index], path=path, file_type=ImageFileFormat.IFF_DSC)
            else:
                raise ValueError(f"Incorrect image format. Available formats: "
                                 f"{PictureFileFormat.BIN.name}, {PictureFileFormat.BMP.name}, "
                                 f"{PictureFileFormat.PPM.name}.\n"
                                 f"Transferred image format: {file_format.name}")
        else:
            warnings.warn("Buffer size is equal 0.")

    def __save_to_video_file(self, file_format: VideoFileFormat, path: str):
        """
        Will be implemented later.
        """
        if len(self.buffer) > 0:
            if file_format == VideoFileFormat.BIN:
                save_video_to_bin(path=path, data=self.buffer)
            elif file_format == VideoFileFormat.MP4:
                save_video_to_mp4(path=path, data=self.buffer)
            else:
                raise ValueError(f"Incorrect video format. Available formats: "
                                 f"{VideoFileFormat.BIN.name}, {VideoFileFormat.MP4.name}.\n"
                                 f"Transferred video format: {file_format.name}")
        else:
            warnings.warn("Buffer size is equal 0.")

    def __str__(self):
        return f"Start capture time: {self.start_capture_time}\n" \
               f"End capture time: {self.end_capture_time}\n" \
               f"Timestamp: {self.timestamp.__str__()}\n"
