#!/usr/bin/env python
from PIL import Image

from _media_file import MediaFile
from _raw_photo import extract_jpg_thumb
from _video import VideoFile
from _hashing import hash_path

class MediaPreview:
    def __init__(self,path_file_name,media_files):
        # super(MediaPreview, self).__init__()
        self.path_file_name = path_file_name
        self.media_files_list = media_files
        # self.media_file = MediaFile(self.path_file_name)
        self.source_path_hash = hash_path(self.path_file_name)
        # print(f'_preview.py,MediaPreview:\n\tpath_file_name: {self.path_file_name}\n\thash: {self.source_path_hash}\n\tmedia_files_list: {self.media_files_list}\n')
        self.thumbnail = 'thumbnail.jpg'
        self.thumbnail_ratio = self.media_files_list[self.source_path_hash]['image_meta']['photo']['size']['ratio']
        self.file_type = self.media_files_list[self.source_path_hash]['file_type']

        if self.media_files_list[self.source_path_hash]['file_type'] == 'image':
            self._img_preview()
        elif self.media_files_list[self.source_path_hash]['file_type'] == 'video':
            self._video_preview()

        self.mpixels = self.media_files_list[self.source_path_hash]['image_meta']['photo']['size']['megapixels']

    def _img_preview(self):
        if self.media_files_list[self.source_path_hash]['image_meta']['photo']['is_jpg'] is True:
            self._jpg_preview()
        else:
            self._raw_preview()

    def _jpg_preview(self):
        self.gen_thumb_from_jpg()

    def gen_thumb_from_jpg(self):
        """Generates a thumbnail image from the given input image."""
        thumb_width = 500
        thumb_size = (
            thumb_width,
            int(thumb_width // self.media_files_list[self.source_path_hash]['image_meta']['photo']['size']['ratio'])
        )
        try:
            with Image.open(self.path_file_name) as img:
                img.thumbnail(thumb_size)
                img.save(self.thumbnail, "JPEG")
        except IOError:
            print(f"Error: Cannot create thumbnail for '{self.path_file_name}'")

    def _raw_preview(self):
        self.thumbnail = extract_jpg_thumb(self.path_file_name)

    def _video_preview(self):
        vid = VideoFile(file=self.path_file_name)
        self.thumbnail = vid.gen_video_thumbnail()

        self.width = self.media_files_list[self.source_path_hash]['video_meta']['video']['size']['width']
        self.height = self.media_files_list[self.source_path_hash]['video_meta']['video']['size']['height']
        self.video_framerate = round(
            int(self.media_files_list[self.source_path_hash]['video_meta']['video']['r_frame_rate'].split('/')[0]) /
            int(self.media_files_list[self.source_path_hash]['video_meta']['video']['r_frame_rate'].split('/')[1]),2)
        self.video_bit_depth = self.media_files_list[self.source_path_hash]['video_meta']['video']['bits_per_raw_sample']
        self.video_duration = self.media_files_list[self.source_path_hash]['video_meta']['video']['duration']
        self.video_encoding = self.media_files_list[self.source_path_hash]['video_meta']['video']['encoding_brand']
        self.video_codec = self.media_files_list[self.source_path_hash]['video_meta']['video']['codec_long_name']
        self.video_profile = self.media_files_list[self.source_path_hash]['video_meta']['video']['profile']
        self.video_pix_format = self.media_files_list[self.source_path_hash]['video_meta']['video']['pix_fmt']