#!/usr/bin/env python

from PIL import Image
from _raw_photo import get_raw_image_dimensions
from _image_tag import ImageTag

# https://exiftool.org/TagNames/EXIF.html
# https://exiftool.org/TagNames/Sony.html

class PhotoFile:
    def __init__(self,path_file_name,*args,**kwargs):
        super(PhotoFile, self).__init__(*args, **kwargs)
        self.path_file_name         = path_file_name
        self.args                   = args
        self.kwargs                 = kwargs
        self.img                    = None
        self.image_tag              = ImageTag(path_file_name=self.path_file_name)
        self.is_jpg                 = self.is_photo_jpeg()
        self.is_raw                 = self.is_photo_raw()
        self.size_width             = self.get_photo_width()
        self.size_height            = self.get_photo_height()
        self.size                   = self.get_photo_size()
        self.mpixels                = self.get_photo_megapixels()
        self.size_ratio             = self.get_photo_size_ratio()
        self.dpi                    = self.get_dpi()
        self.iso                    = self.get_iso()
        self.aperture               = self.get_aperture()
        self.camera_brand           = self.get_camera_brand()
        self.camera_model           = self.get_camera_model()
        self.lens_make              = self.get_lens_make()
        self.lens_model             = self.get_lens_model()
        self.focal_length           = self.get_focal_length()
        self.photo_meta             = {}
        self.photo_capture_date     = self.get_photo_capture_date()


    def get_photo_capture_date(self):
        return self.image_tag.date_time_original

    def is_photo_raw(self):
        # TODO: Actually inspect the mime type instead of relying on an extension.
        if self.path_file_name.lower().endswith('jpg') \
                or self.path_file_name.lower().endswith('.jpeg'):
            self.is_raw             = False
            self.is_jpg             = True
            self.img = Image.open(self.path_file_name)
        else:
            self.is_raw             = True
            self.is_jpg             = False

        return self.is_raw

    def is_photo_jpeg(self):
        # TODO: Stop making assumption. #Life-Lessons
        self.is_photo_raw()
        return self.is_jpg

    def get_photo_width(self):
        if self.is_jpg:
            width = self.img.width
        elif self.is_raw:
            width = get_raw_image_dimensions(self.path_file_name)[1]
        else:
            width = None
        return width

    def get_photo_height(self):
        if self.is_jpg:
            height = self.img.height
        elif self.is_raw:
            height = get_raw_image_dimensions(self.path_file_name)[0]
        else:
            height = None
        return height

    def get_photo_size(self):
        if self.size_width is None:
            self.size_width         = self.get_photo_width()
        if self.size_height is None:
            self.size_height        = self.get_photo_height()
        return f'{self.size_width}x{self.size_height}'

    def get_photo_size_ratio(self):
        if self.size_width is None:
            self.size_width         = self.get_photo_width()
        if self.size_height is None:
            self.size_height        = self.get_photo_height()
        return float(self.size_width / self.size_height)

    def get_photo_megapixels(self):
        if self.size_width is None:
            self.size_width         = self.get_photo_width()
        if self.size_height is None:
            self.size_height        = self.get_photo_height()
        return self.size_width * self.size_height

    def get_dpi(self):
        return self.image_tag.get_image_tag("xresolution")

    def get_iso(self):
        return self.image_tag.get_image_tag( 'iso')

    def get_aperture(self):
        return self.image_tag.get_image_tag('fnumber')

    def get_camera_brand(self):
        tag = self.image_tag.get_image_tag('Make')
        return tag

    def get_camera_model(self):
        tag = self.image_tag.get_image_tag('cameramodelname')
        if tag is None:
            tag = self.image_tag.get_image_tag('image model')
        return tag

    def get_camera_firmware(self):
        tag = self.image_tag.get_image_tag('CameraFirmware')
        return tag

    def get_camera_serial_number(self):
        tag = self.image_tag.get_image_tag('CameraSerialNumber')
        return tag

    def get_lens_make(self):
        return self.image_tag.get_image_tag('lensmake')

    def get_lens_model(self):
        return self.image_tag.get_image_tag('lensmodel')

    def get_lens_serial(self):
        pass

    def get_focal_length(self):
        return self.image_tag.get_image_tag('focallength')

    def get_photographer(self):
        pass

    def get_orientation(self):
        pass

    def get_sony_raw_type(self):
        pass

    def get_exposure_program(self):
        pass

    def get_photo_meta(self):
        photo_meta = {
            'photo': {
                'aperture': self.aperture,
                'camera_brand': self.camera_brand,
                'camera_model': self.camera_model,
                'dpi': self.dpi,
                'is_jpg': self.is_jpg,
                'is_raw': self.is_raw,
                'iso': self.iso,
                'lens_make': self.lens_make,
                'lens_model': self.lens_model,
                'lens_focal_length': self.focal_length,
                'size': {
                    'height': self.size_height,
                    'width': self.size_width,
                    'width_height': self.size,
                    'megapixels': self.mpixels,
                    'ratio': self.size_ratio
                }
            }
        }
    
        return photo_meta