from json import dumps

JOLOKIA_VERSION = '1.7.1'
JOLOKIA_SHA1 = 'd9cf8215598480c98c03a2409344aba20d29884b'

java_opts = set()

for k, v in node.metadata.get('minecraft/java_opts').items():
    if v is None:
        java_opts.add(k)
    else:
        java_opts.add(f'{k}={v}')

# World
directories['/home/minecraft/world'] = {
    'owner': 'minecraft',
    'group': 'minecraft',
}

directories['/home/minecraft/world_nether'] = {
    'owner': 'minecraft',
    'group': 'minecraft',
}

directories['/home/minecraft/world_the_end'] = {
    'owner': 'minecraft',
    'group': 'minecraft',
}

# The actual server
files['/usr/local/lib/systemd/system/minecraft.service'] = {
    'content_type': 'mako',
    'context': {
        'java_opts': ' '.join(sorted(java_opts)),
    },
    'triggers': {
        'action:systemd-reload',
        'svc_systemd:minecraft:restart',
    },
}

files['/home/minecraft/eula.txt'] = {
    'content': 'eula=true\n',
}

files['/home/minecraft/server.properties'] = {
    # get it from data/minecraft/files/{node.name}
    'source': node.name,
    'triggers': {
        'svc_systemd:minecraft:restart',
    },
}

version, build_nr = node.metadata.get('minecraft/version')

files['/home/minecraft/minecraft_server.jar'] = {
    'content_type': 'download',
    'source': f'https://api.papermc.io/v2/projects/paper/versions/{version}/builds/{build_nr}/downloads/paper-{version}-{build_nr}.jar',
    'content_hash': node.metadata.get('minecraft/sha1', None),
    'triggers': {
        'svc_systemd:minecraft:restart',
    },
}

svc_systemd['minecraft'] = {
    'needs': {
        'directory:/home/minecraft/world',
        'directory:/home/minecraft/world_nether',
        'directory:/home/minecraft/world_the_end',
        'file:/home/minecraft/eula.txt',
        'file:/home/minecraft/minecraft_server.jar',
        'file:/home/minecraft/server.properties',
        'file:/usr/local/lib/systemd/system/minecraft.service',
        'pkg_apt:openjdk-17-jre',
    },
}

# Permissions
files['/home/minecraft/ops.json'] = {
    'content': dumps(
        [
            {
                'uuid': uuid,
                'name': name,
                'level': 4
            } for name, uuid in sorted(node.metadata.get('minecraft/ops', {}).items())
        ],
        indent=4,
    ),
    'triggers': {
        'svc_systemd:minecraft:restart',
    },
}

if node.metadata.get('minecraft/allowlist', {}):
    files['/home/minecraft/whitelist.json'] = {
        'content': dumps(
            [
                {
                    'uuid': uuid,
                    'name': name,
                } for name, uuid in sorted(node.metadata.get('minecraft/allowlist').items())
            ],
            indent=4,
        ),
        'triggers': {
            'svc_systemd:minecraft:restart',
        },
    }

# Monitoring
files['/home/minecraft/jolokia.jar'] = {
    'source': f'https://search.maven.org/remotecontent?filepath=org/jolokia/jolokia-jvm/{JOLOKIA_VERSION}/jolokia-jvm-{JOLOKIA_VERSION}.jar',
    'content_type': 'download',
    'content_hash': JOLOKIA_SHA1,
    'triggers': {
        'svc_systemd:minecraft:restart',
    },
}