import sys
import shutil
from progress.bar import Bar

from path import Path
from picture import Picture
from page import Page

def argument_parsing():
    if (len(sys.argv) < 2):
        print("error: missing argument", file=sys.stderr)
        exit(1)
    return sys.argv[1]

def scan_pages(folders: list[Path]) -> list[Page]:
    pages: list[Page] = []
    prev: Page = None
    with Bar("scaning...", max=len(folders)) as bar:
        for folder in folders:
            files: list[Path] = folder.get_files()

            page: Page = Page(folder.get_absolute_path(), folder.get_name(), prev=prev)

            raw: Path = Path(folder.get_absolute_path(), folder.get_name() + ".NEF")
            for file in files:
                if file.get_name().endswith(".png"):
                    page.add_picture(Picture(file, page=prev, raw=raw))

            if len(page.get_picture()) == 0:
                bar.next()
                continue

            pages.append(page)
            if prev is not None:
                prev.next = page
            prev = page

            bar.next()
    pages[-1].next = pages[0]
    pages[0].prev = pages[-1]
    return pages

def create_pages(pages: list[Page]) -> None:
    with Bar("generating...", max=len(pages)) as bar:
        for page in pages:
            page.create()
            bar.next()

def main():
    site_path: Path = Path(argument_parsing())
    pages: list[Page] = scan_pages(site_path.get_dirs())
    shutil.copy2("./src/templates/page.css", site_path.get_absolute_path())
    create_pages(pages)

if __name__ == "__main__":
    main()