import os
import sys
import time
import fitz  # PyMuPDF
from PIL import Image


root_dir = '.'
tiny_width = 100

nargv = len(sys.argv)
if nargv >= 2: root_dir = sys.argv[1]
if nargv >= 3: tiny_res = int(sys.argv[2])


def find_pdfs(root_dir):
    print("root_dir=", root_dir)
    for cur_dir, dirs, files in os.walk(root_dir):
        print("  cur_dir=", cur_dir)
#        print("    dirs=", dirs)
#        print("    files=", files)
        for file in files:
            if file.lower().endswith('.pdf'):
                path = os.path.join(cur_dir, file)
#                print(f"  added [{path}]")
                yield path

def convert_pdf_to_jpeg(pdf_path, tiny_width = None):
    abspath = os.path.abspath(pdf_path)
    filename = os.path.basename(abspath)
    ext = os.path.splitext(abspath)[1]
    filebody = os.path.splitext(os.path.basename(abspath))[0]
    dir_name = os.path.dirname(abspath)
#    print(f"  convert pdf_path: abspath: {abspath} dir:{dir_name} filebody: {filebody}")

    mtime_pdf = os.path.getmtime(abspath)
    mtime_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(mtime_pdf))

    print(f"Convert [{abspath}] (modified on {mtime_str})")
    outpath = os.path.join(dir_name, f"{filebody}.jpg")
    tinypath = os.path.join(dir_name, f"tiny-{filebody}.jpg")
    if os.path.exists(outpath) and os.path.exists(tinypath):
        filename_img  = os.path.basename(outpath)
        filename_tiny = os.path.basename(tinypath)

        mtime_img  = os.path.getmtime(outpath)
        mtime_tiny = os.path.getmtime(tinypath)
        if mtime_img < mtime_pdf:
            print(f"  jpeg file [{filename_img}] (mtime_img) is older than pdf file [{filename}] (mtime_pdf)")
        elif mtime_tiny < mtime_pdf:
            print(f"  jpeg file [{filename_tiny}] (mtime_tiny) is older than pdf file [{filename}] (mtime_pdf)")
        else:
            print(f"  jpeg files [{filename_img}] and [{filename_tiny}] are up to date. Skip.")
            return

    pdf_document = fitz.open(pdf_path)
    page = pdf_document.load_page(0)
    pix = page.get_pixmap()
    print(f"  Save to [{outpath}]")
    pix.save(outpath)

# thumnail
    if tiny_width is None: return

    width = pix.width
    height = pix.height
    scale = tiny_width / width
    new_height = int(height * scale)

    matrix = fitz.Matrix(scale, scale)
    pix = page.get_pixmap(matrix = matrix)
    print(f"  Save thumnail to [{tinypath}]")
    pix.save(tinypath)


def main():
    for pdf_path in find_pdfs(root_dir):
        convert_pdf_to_jpeg(pdf_path, tiny_width)

if __name__ == "__main__":
    main()
