from PIL import Image 
import os

from path import Path

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from page import Page 

class Picture():
    def __init__(self, picture_path: Path, page = None, raw: Path|None = None):
        self.large: Path = picture_path
        self.small: Path = Path(picture_path.get_dirpath(), picture_path.get_name()[:-4] + "_small.jpg")
        self.export_file: Path = Path(picture_path, ".out.pp3")
        self.raw: Path|None = raw
        self.page: Page = page

    def render(self) -> tuple[str, str, str]:
        if not self.small.exist():
            self.gen_small()
        return self.large.get_name(), self.small.get_name(), self.export_file.get_name() if self.export_file.exist() else None
    
    def gen_small(self):
        im = Image.open(self.large.get_absolute_path()).convert("RGB")
        im.save(self.small.get_absolute_path(), quality=95, optimize=True)

        