from sys import argv
import subprocess
from PIL import Image
from jinja2 import Environment, FileSystemLoader
from markdown import markdown
from glob import glob
from json import dump, dumps, load
from os import path
from subprocess import call
import re

numbers = re.compile("[0-9]+")
out_dir = "/var/www/html/"
base_url = "https://helixnebula.space/"
template_environment = Environment(
    loader=FileSystemLoader("templates/"))

with open("metadata.json", "r") as f:
    metadata = load(f)

for placename in metadata.keys():
    read_path = f"{out_dir}{placename}/"
    photos = [
            path.basename(photo).split('/')[-1] for photo in 
            glob(read_path + "*")
        ]
    try:
        photos.remove("index.html")
    except ValueError:
        pass
    metadata[placename]["count"] = len(photos)
    metadata[placename]["photos"] = photos
    metadata[placename]["photos"].sort(key=lambda path: int(numbers.search(path).group(0)))

with open(f"{out_dir}photos.json", "w") as f:
    dump(metadata, f)

def covers(overwrite=False):
    print("running covers")
    for placename, info in metadata.items():
        read_path = f"{out_dir}{placename}/{info['cover']}"
        write_path = f"{out_dir}cover/cover_{info['cover']}"
        if not overwrite and path.exists(write_path):
            continue
        command = ["convert", read_path, "-strip", "-interlace", "Plane", "-gaussian-blur", "0.05",
                   "-auto-orient", "-resize", "700x525>", "-quality", "80%", write_path]
        subprocess.run(command)

def thumbnails(overwrite=False):
    print("running thumbnails")
    for placename in metadata.keys():
        for photo in metadata[placename]["photos"]:
            read_path = f"{out_dir}{placename}/{photo}"
            write_path = f"{out_dir}thumbnail/thumbnail_{photo}"
            if not overwrite and path.exists(write_path):
                continue
            command = ["convert", read_path, "-strip", "-interlace", "Plane", "-gaussian-blur", "0.05",
                       "-auto-orient", "-resize", "300x200>", "-quality", "65%", write_path]
            subprocess.run(command)

def compressed(overwrite=False):
    print("running compressed")
    for placename in metadata.keys():
        for photo in metadata[placename]["photos"]:
            read_path = f"{out_dir}{placename}/{photo}"
            write_path = f"{out_dir}compressed/compressed_{photo}"
            if not overwrite and path.exists(write_path):
                continue
            command = ["convert", read_path, "-auto-orient", "-strip", "-resize", "1200>", "-quality", "90%", write_path]
            subprocess.run(command)

def render_index():
    template = template_environment.get_template("main")
    data = {key: value for key, value in sorted(metadata.items(), key=lambda item: item[1]['title'])}
    photo_counts = {placename: metadata[placename]["count"] for placename in metadata.keys()}
    total_count = 0
    posts = [
            path.basename(post).split('/')[-1][:-3] for post in 
            glob("md/*")
        ]
    posts.sort()
    for _, info in metadata.items():
        total_count = info["count"] + total_count
    with open(out_dir + "index.html", "w") as f:
        f.write(template.render({
            "metadata": data,
            "album_count": len(metadata.keys()),
            "photo_counts": photo_counts,
            "total_count": total_count,
            "posts": posts
        }))

def render_places():
    template = template_environment.get_template("place")
    for placename, info in metadata.items():
        working_path = out_dir + placename
        photos = metadata[placename]["photos"]
        photos_json = dumps(photos)
        count = len(photos)
        widths = []
        heights = []
        for photo in photos:
            photo_path = out_dir + "thumbnail/thumbnail_" + photo
            img = Image.open(photo_path)
            width, height = img.size
            widths.append(width)
            heights.append(height)
        photo_specs = zip(widths, heights, photos)
        try:
            with open("md/" + placename + ".md", "r") as f:
                md = markdown(f.read())
        except FileNotFoundError:
            md = ""
        with open(working_path + "/index.html", "w") as f:
            f.write(template.render({
                "count": count,
                "placename": placename,
                "info": info,
                "markdown": md,
                "photos": photo_specs,
                "photos_json": photos_json
            }))

def copy_files():
    call("cp js/* html/js/", shell=True)
    call("cp style.css html/style.css", shell=True)

if __name__ == "__main__":
    try:
        overwrite = "overwrite" in argv
        covers(overwrite=overwrite)
        thumbnails(overwrite=overwrite)
        compressed(overwrite=overwrite)
    except KeyboardInterrupt:
        exit() 
    render_index()
    render_places()
    copy_files()