from pathlib import Path
import markdown
# from markdown.extensions.wikilinks import WikiLinkExtension
from markdown.extensions.toc import TocExtension

SOURCE_DIR = Path("docs")       # input folder
OUTPUT_DIR = Path("www")       # output folder

def my_url_builder(label, base, end):
    url = (base + label + end).lower().replace(' ', '_')
    return url

def convert_markdown_to_html(md_path: Path, html_path: Path):
    html_path.parent.mkdir(parents=True, exist_ok=True)
    md = markdown.Markdown(extensions=[
        # WikiLinkExtension(base_url='./', end_url='.html', build_url=my_url_builder),
        TocExtension(permalink=True),
        'tables',
        'fenced_code',
        'sane_lists',
        'prependnewline',
        'meta',
    ])
    html = md.convert(md_path.read_text(encoding="utf-8"))
    title = md.Meta.get("title", (md_path.name,))[0] # type: ignore
    prev_page = md.Meta.get("prev", ("",))[0] # type: ignore
    next_page = md.Meta.get("next", ("",))[0] # type: ignore

    prev_link = f'<a href="/{my_url_builder(prev_page, './', '.html')}">Prev</a>'
    next_link = f'<a href="/{my_url_builder(next_page, './', '.html')}">Next</a>'

    html_page = f"""<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>YREA SLS | {title}</title>
    <link rel="stylesheet" href="styles.css">
    <link id="icon" rel="shortcut icon" href="sls-on-white.svg">
    <script src="dark_mode.js"></script>
</head>
<body>
<header>
    <div>
        <p class="title">
            {title}
        </p>
        <div class="logo"><a href="/">
            <img src="YREA.svg" height="48">
            <span></span>
            <img src="sls.svg" height="42">
        </a></div>
        <p class="nav">
            {f'{prev_link} |' if prev_page else ''}
            <a href="/">Home</a>
            {f'| {next_link}' if next_page else ''}
        </p>
    </div>
    <hr/>
</header>
<main>
{html}
</main>
<footer>
    <div>
        <small>
            <span>&copy; 2025 Kyler Olsen</span>
            <a href="https://purplecello.org/contact.html">Contact</a> |
            <a id="dark">Dark Mode</a> |
            {f'{prev_link}' if prev_page else ''}
            <a href="/">Home</a>
            {f'{next_link}' if next_page else ''}
        </small>
    </div>
</footer>
</body>
</html>
"""
    html_path.write_text(html_page, encoding="utf-8")
    print(f"Converted: {md_path} → {html_path}")

def convert_all(source_dir: Path, output_dir: Path):
    for svg_file in source_dir.rglob("*.svg"):
        rel_path = svg_file.relative_to(source_dir)
        dest_path = output_dir / rel_path
        dest_path.parent.mkdir(parents=True, exist_ok=True)
        dest_path.write_bytes(svg_file.read_bytes())
        print(f"Copied: {svg_file} → {dest_path}")
    for js_file in source_dir.rglob("*.js"):
        rel_path = js_file.relative_to(source_dir)
        dest_path = output_dir / rel_path
        dest_path.parent.mkdir(parents=True, exist_ok=True)
        dest_path.write_bytes(js_file.read_bytes())
        print(f"Copied: {js_file} → {dest_path}")
    for css_file in source_dir.rglob("*.css"):
        rel_path = css_file.relative_to(source_dir)
        dest_path = output_dir / rel_path
        dest_path.parent.mkdir(parents=True, exist_ok=True)
        dest_path.write_bytes(css_file.read_bytes())
        print(f"Copied: {css_file} → {dest_path}")
    for md_file in source_dir.rglob("*.md"):
        rel_path = md_file.relative_to(source_dir)
        html_path = output_dir / rel_path.with_suffix(".html")
        convert_markdown_to_html(md_file, html_path)
    for md_file in (source_dir / '..' / "stack_lang_spec.md",):
        rel_path = md_file.relative_to(source_dir / '..')
        html_path = output_dir / rel_path.with_suffix(".html")
        convert_markdown_to_html(md_file, html_path)

def convert_all_watch_files(source_dir: Path, output_dir: Path):
    import time
    from watchdog.observers import Observer
    from watchdog.events import FileSystemEventHandler

    class ChangeHandler(FileSystemEventHandler):
        def on_modified(self, event):
            if event.src_path.endswith(".md"): # type: ignore
                md_path = Path(event.src_path) # type: ignore
                rel_path = md_path.relative_to(source_dir)
                html_path = output_dir / rel_path.with_suffix(".html")
                convert_markdown_to_html(md_path, html_path)
            elif event.src_path.endswith(".css"): # type: ignore
                css_path = Path(event.src_path) # type: ignore
                rel_path = css_path.relative_to(source_dir)
                dest_path = output_dir / rel_path
                dest_path.parent.mkdir(parents=True, exist_ok=True)
                dest_path.write_bytes(css_path.read_bytes())
                print(f"Copied: {css_path} → {dest_path}")

    event_handler = ChangeHandler()
    observer = Observer()
    observer.schedule(event_handler, str(source_dir), recursive=True)
    observer.start()
    print("Watching for changes... Press Ctrl+C to stop.")
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

if __name__ == "__main__":
    from sys import argv
    convert_all(SOURCE_DIR, OUTPUT_DIR)
    if 'watch' in argv:
        convert_all_watch_files(SOURCE_DIR, OUTPUT_DIR)
