""" Diagram to visualize serguzim.net """
# pragma pylint: disable=pointless-statement

import glob
import mimetypes
import os
from urllib.request import urlretrieve

from diagrams import Cluster, Diagram

from diagrams.custom import Custom

from diagrams.oci.storage import ObjectStorage

from diagrams.onprem.database import Influxdb, Postgresql
from diagrams.onprem.inmemory import Redis
from diagrams.onprem.monitoring import Grafana, Prometheus
from diagrams.onprem.network import Caddy
from diagrams.onprem.vcs import Gitea

import requests


ASSETS_DIR = "./diagram_assets/"
os.makedirs(ASSETS_DIR, exist_ok=True)


def add_to_groups(service, *groups):
    """ Add a service to groups like 'db_users' """
    for group in groups:
        group.append(service)

def get_custom_icon(name, url):
    """ Download icon for service to location. Reuses existing icons. """

    icon_no_extension = os.path.join(ASSETS_DIR, name)

    exisiting_icons = glob.glob(icon_no_extension + ".*")
    if exisiting_icons:
        return exisiting_icons[0]

    response = requests.head(url, allow_redirects=True)
    extension = mimetypes.guess_extension(response.headers['Content-Type'])

    icon = icon_no_extension + extension
    urlretrieve(url, icon)
    return icon


with Diagram("serguzim.net", show=False):

    node001 = Cluster("node001.serguzim.net")
    node002 = Cluster("node002.serguzim.net")

    exoscale_objectstore = ObjectStorage("Exoscale Object Storage")

    with node002:
        services = []
        db_users = []
        objectstore_users = []

        proxy = Caddy()

        ICON_URL = "https://avatars.githubusercontent.com/u/105618662"
        analytics = Custom("analytics.serguzim.me", get_custom_icon("umami", ICON_URL))
        add_to_groups(analytics, services, db_users)

        ICON_URL = "https://avatars.githubusercontent.com/u/84780935"
        ci = Custom("ci.serguzim.me", get_custom_icon("woodpecker-ci", ICON_URL))
        add_to_groups(ci, services, db_users)

        db_time = Influxdb("tick.serguzim.me")
        add_to_groups(db_time, services, db_users)

        db = Postgresql("db.serguzim.me")

        graph = Grafana("graph.serguzim.me")
        add_to_groups(graph, services, db_users)

        ICON_URL = "https://avatars.githubusercontent.com/u/40275816"
        harbor = Custom("registry.serguzim.me", get_custom_icon("harbor", ICON_URL))
        add_to_groups(harbor, services, db_users, objectstore_users)

        ICON_URL = "https://avatars.githubusercontent.com/u/23747925"
        mail = Custom("mail.serguzim.me", get_custom_icon("mailcow", ICON_URL))
        add_to_groups(mail, services)

        ICON_URL = "https://avatars.githubusercontent.com/u/8418310"
        matrix = Custom("matrix.serguzim.me", get_custom_icon("matrix", ICON_URL))
        add_to_groups(matrix, services, db_users, objectstore_users)

        prometheus = Prometheus("prometheus.serguzim.me")
        add_to_groups(prometheus, services)

        ICON_URL = "https://github.com/TandoorRecipes/recipes/raw/develop/cookbook/static/assets/logo_color512.png"
        recipies = Custom("recipies.serguzim.me", get_custom_icon("tandoor", ICON_URL))
        add_to_groups(recipies, services, db_users)

        vcs = Gitea("git.serguzim.me\nregistry.serguzim.me")
        add_to_groups(vcs, services, db_users, objectstore_users)

        webdis = Redis("webdis.huck.serguzim.me")
        add_to_groups(webdis, services)

        db_users >> db
        proxy >> services
        objectstore_users >> exoscale_objectstore

    with node001:
        services = []

        proxy = Caddy()

        ICON_URL = "https://icon-icons.com/downloadimage.php?id=168974&root=2699/PNG/256/&file=minecraft_logo_icon_168974.png"
        minecraft = Custom("minecraft.serguzim.me", get_custom_icon("minecraft", ICON_URL))
        add_to_groups(minecraft, services)

        proxy >> services