import markdown
import argparse
import logging
import os
import shutil
from pathlib import Path

class SiteGenerator():

    def __init__(self, source_dir: Path, build_dir: Path):
        self.source_dir = source_dir
        self.build_dir = build_dir

        self.templates = {}
        self.index_name = "index.html"
        self.pages_dir_name = "pages"
        self.static_dir_name = "static"
        self.image_dir_name = "images"
        self.template_dir_name = "template"

    def generate(self):
        logging.info(f"Building sources from {self.source_dir} to {self.build_dir}")

        self.clean_build_dir()

        self.scan_source_dir()

    def process_pages(self, path: Path):
        for dir_entry in path.iterdir():
            if dir_entry.is_file() and dir_entry.suffix == ".md":
                with open(dir_entry, 'r') as f:
                    content = f.read()

                output_content = self.process_page(content)
                output_path = self.build_dir / Path(str(dir_entry.name) + ".html")
                logging.info(f"Writing content to {output_path}")
                with open(output_path, 'w') as f:
                    f.write(output_content)

    def process_page(self, page_md: str):
        html_content = markdown.markdown(page_md)
        if "header" in self.templates:
            html_content = self.templates["header"] + html_content
        if "footer" in self.templates:
            html_content += self.templates["footer"]
        return html_content

    def clean_build_dir(self):
        if os.path.exists(self.build_dir):
            os.rmdir(self.build_dir)
        os.makedirs(self.build_dir)  

    def process_images(self, path: Path):
        static_dir = self.build_dir / self.static_dir_name
        images_dir = static_dir / self.image_dir_name
        os.makedirs(images_dir)  

        for dir_entry in path.iterdir():
            if dir_entry.is_file():
                shutil.copy(dir_entry, images_dir)

    def process_static_dir(self, path: Path):

        static_dir = self.build_dir / self.static_dir_name
        os.makedirs(static_dir)

        for dir_entry in path.iterdir():
            if dir_entry.is_file():
                shutil.copy(dir_entry, static_dir / dir_entry.name)
            elif dir_entry.is_dir():
                if dir_entry.name == "images":
                    self.process_images(dir_entry)
                else:
                    shutil.copytree(dir_entry, static_dir / dir_entry.name)

    def scan_templates(self, path: Path):
        for dir_entry in path.iterdir():
            if dir_entry.is_file() and dir_entry.suffix == ".html":
                with open(dir_entry, 'r') as f:
                    self.templates[dir_entry.stem] = f.read()

    def scan_source_dir(self):
        for dir_entry in self.source_dir.iterdir():
            if dir_entry.is_dir():
                if dir_entry.name == self.pages_dir_name:
                    self.process_pages(dir_entry)
                elif dir_entry.name == self.static_dir_name:
                    self.process_static_dir(dir_entry)
                elif dir_entry.name == self.template_dir_name:
                    self.scan_templates(dir_entry)
            elif dir_entry.is_file():
                if dir_entry.name == self.index_name:
                    shutil.copy(dir_entry, self.build_dir / dir_entry.name)

if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument("source_dir")
    parser.add_argument("--build_dir", default = os.getcwd() + "/build")
    args = parser.parse_args()

    generator = SiteGenerator(Path(args.source_dir), Path(args.build_dir))
    generator.generate()



