#!/usr/bin/env python3
# Python Requirements
# python3
# requests
# shutil

import requests
import os
import sys
from typing import List

path: str = sys.argv[1]
api: str = "https://api.dezentrale.cloud/spaceapi/v14"
space_open: str = path + "/assets/logo_open"
space_closed: str = path + "/assets/logo_closed"
space_unknown: str = path + "/assets/logo_unknown"
space_logo: str = path + "/assets/logo"

extensions: List[str] = [".png", ".svg"]

# every extension needs to be linked
def link(src: str, dest: str):
    for ext in extensions:
        try:
            os.symlink(src + ext, dest + ext)
            print("linked " + dest + ext + " to " + src + ext)
        except:
            print("cannot link " + dest + ext + " to " + src + ext)

def delete(file: str):
    for ext in extensions:
        try:
            os.remove(file + ext)
            print("deleted " + file + ext)
        except:
            try:
                os.unlink(file + ext)
                print("unlinked "+ file + ext)
            except:
                print(file + ext + " is already deleted and unlinked")

if __name__ == "__main__":
    delete(space_logo)
    try:
        resp: requests.Response = requests.get(api)
        resp_dict: dict = resp.json()
        
        door_open: bool = resp_dict.get("state").get("open")
        print(door_open)
        
        path = ""
        if door_open:
            path = space_open
        else:
            path = space_closed
        # the linking seems to be the wrong way around, but it links correct
        # the link is pointing from dest to src (https://docs.python.org/3/library/os.html)
        link(path, space_logo)
    except:
        link(space_unknown, space_logo)
