import os, re, shutil
from jinja2 import Environment, FileSystemLoader
import pandas as pd


def add_download_button(row):
    item_id = re.sub(r'<a href.*/([0-9]+)".*', "\\1", row[1])
    download_button = (
        f'<button id="btn_{item_id}"><i class="fa fa-download"></i> {item_id}</button>'
    )
    print_button = f'<a href="http://localhost:31337/print/{item_id}"><button id="prn_{item_id}"><i class="fa fa-print"></i> {item_id}</button></a>'
    return row + [download_button, print_button]


def generate_dashboard():
    """Generate dashboard from zasoby.csv file"""
    print("Generating HTML dashboard")
    website_folder = "fegen/docs"
    data = pd.read_csv("zasoby.csv")
    env = Environment(loader=FileSystemLoader("fegen/template"))
    print("Removing old website files")
    print("Creating a new website")
    shutil.copytree("fegen/template/static", f"{website_folder}/static", dirs_exist_ok=True)
    template = env.get_template("_main_layout.html")
    with open(f"{website_folder}/index.html", "w+", encoding="utf-8") as file:
        header_row = data.columns.values.tolist() + ["label", "print"]
        rows = map(
            add_download_button,
            data.values.tolist(),
        )
        html = template.render(
            title="Baza Zasobów Hackerspace Trójmiasto",
            t_header=header_row,
            t_body=rows,
        )
        file.write(html)


if __name__ == "__main__":
    from discourse import DiscourseDatabase

    DiscourseDatabase()
    generate_dashboard()
    print("Done!")
