#!/bin/python
import requests
import sys
import os

apps = {"broadlink": "68", "zwave": "60", "huee": "62", "webapp": "59", "yeelight": "87", "octoprint": "99", "postgresql": "102", "webapi": "104", "chromecast": "148", "mqtt": "152", "webgrid": "167"}
headers = {"PRIVATE-TOKEN": sys.argv[1]}

def get_data(id_number):
    branch_name = "main"
    commit = requests.get(f'https://git.cmtec.se/api/v4/projects/{id_number}/repository/commits/{branch_name}', headers=headers).json()

    # Backwards compatibility for old branch default name (master)
    message = commit.get("message", "")
    if message == "404 Commit Not Found":
        branch_name = "master"
        commit = requests.get(f'https://git.cmtec.se/api/v4/projects/{id_number}/repository/commits/{branch_name}', headers=headers).json()

    date = commit.get("committed_date")
    name = commit.get("committer_name")
    email = commit.get("committer_email")
    readme = requests.get(f'https://git.cmtec.se/api/v4/projects/{id_number}/repository/files/README.md/raw?ref={branch_name}', headers=headers).text
    changelog = requests.get(f'https://git.cmtec.se/api/v4/projects/{id_number}/repository/files/CHANGELOG.md/raw?ref={branch_name}', headers=headers).text

    return (date, name, email, readme, changelog)

def create_files(path, readme, changelog):
    if not os.path.exists("content/" + path):
        os.makedirs("content/" + path)
    with open('content/'+path+'/_index.md', 'w') as file:
        file.write(readme)
    with open('content/'+path+'/changelog.md', 'w') as file:
        file.write(changelog)


# LibHAASP
date,name,email,readme,changelog = get_data("64")
meta_readme = f'+++\ntitle = "libHAASP"\nhead = "<label>Core Modules</label>"\nweight = 5\ndate = "{date}"\nlastmodifierdisplayname = "{name}"\nlastmodifieremail = "{email}"\n+++\n'
meta_changelog = f'+++\ntitle = "Changelog"\ndate = "{date}"\nlastmodifierdisplayname = "{name}"\nlastmodifieremail = "{email}"\n+++\n'
create_files("libhaasp", meta_readme + readme, meta_changelog + changelog)

# Core 
date,name,email,readme,changelog = get_data("58")
meta_readme = f'+++\ntitle = "Core"\nweight = 6\ndate = "{date}"\nlastmodifierdisplayname = "{name}"\nlastmodifieremail = "{email}"\n+++\n'
meta_changelog = f'+++\ntitle = "Changelog"\ndate = "{date}"\nlastmodifierdisplayname = "{name}"\nlastmodifieremail = "{email}"\n+++\n'
create_files("core", meta_readme + readme, meta_changelog + changelog)

# CLI 
date,name,email,readme,changelog = get_data("69")
meta_readme = f'+++\ntitle = "CLI"\nweight = 7\ndate = "{date}"\nlastmodifierdisplayname = "{name}"\nlastmodifieremail = "{email}"\n+++\n'
meta_changelog = f'+++\ntitle = "Changelog"\ndate = "{date}"\nlastmodifierdisplayname = "{name}"\nlastmodifieremail = "{email}"\n+++\n'
create_files("cli", meta_readme + readme, meta_changelog + changelog)

# Applications
weight = 10
for app in sorted(apps.keys()):
    date,name,email,readme,changelog = get_data(apps[app])
    if weight == 10:
        meta_readme = f'+++\nweight = "{str(weight)}"\nhead = "<label>App Modules</label>"\ntitle = "{app[0].upper()}{app[1:]}"\ndate = "{date}"\nlastmodifierdisplayname = "{name}"\nlastmodifieremail = "{email}"\n+++\n'
    else:
        meta_readme = f'+++\nweight = "{str(weight)}"\ntitle = "{app[0].upper()}{app[1:]}"\ndate = "{date}"\nlastmodifierdisplayname = "{name}"\nlastmodifieremail = "{email}"\n+++\n'
    meta_changelog = f'+++\ntitle = "Changelog"\ndate = "{date}"\nlastmodifierdisplayname = "{name}"\nlastmodifieremail = "{email}"\n+++\n'
    create_files(app, meta_readme + readme, meta_changelog + changelog)
    weight = weight + 1

