from bundlewrap.metadata import atomic

defaults = {
    'apt': {
        'packages': {
            'openjdk-17-jre': {},
        },
    },
    'backups': {
        'paths': {
            '/home/minecraft',
        },
    },
    'icinga2_api': {
        'minecraft': {
            'services': {
                'MINECRAFT SERVER': {
                    'command_on_monitored_host': '/usr/local/share/icinga/plugins/check_systemd_unit minecraft',
                },
            },
        },
    },
    'minecraft': {
        'java_opts': {
            # https://aikar.co/2018/07/02/tuning-the-jvm-g1gc-garbage-collector-flags-for-minecraft/
            '-Daikars.new.flags': 'true',
            '-Dusing.aikars.flags': 'https://mcflags.emc.gs',
            '-XX:+AlwaysPreTouch': None,
            '-XX:+DisableExplicitGC': None,
            '-XX:+ParallelRefProcEnabled': None,
            '-XX:+PerfDisableSharedMem': None,
            '-XX:+UnlockExperimentalVMOptions': None,
            '-XX:G1HeapRegionSize': '16M',
            '-XX:G1HeapWastePercent': 5,
            '-XX:G1MaxNewSizePercent': 50,
            '-XX:G1MixedGCCountTarget': 4,
            '-XX:G1MixedGCLiveThresholdPercent': 90,
            '-XX:G1NewSizePercent': 40,
            '-XX:G1RSetUpdatingPauseTimePercent': 5,
            '-XX:G1ReservePercent': 15,
            '-XX:InitiatingHeapOccupancyPercent': 20,
            '-XX:MaxGCPauseMillis': 200,
            '-XX:MaxTenuringThreshold': 1,
            '-XX:SurvivorRatio': 32,
            '-javaagent:/home/minecraft/jolokia.jar=port=25576,host=localhost': None,
        },
    },
    'telegraf': {
        'input_plugins': {
            'builtin': {
                'jolokia2_agent': [{
                    'urls': ['http://localhost:25576/jolokia'],
                    'interval': '10s',
                    'metric': [
                        {
                            'name': 'minecraft_tick',
                            'mbean': 'net.minecraft.server:type=Server',
                            'paths': [
                                'averageTickTime',
                            ],
                        },
                        {
                            'name': 'minecraft_memory',
                            'mbean': 'java.lang:type=Memory',
                            'paths': [
                                'HeapMemoryUsage',
                                'NonHeapMemoryUsage',
                                'ObjectPendingFinalizationCount',
                            ],
                        },
                        {
                            'name': 'minecraft_gc',
                            'mbean': 'java.lang:name=*,type=GarbageCollector',
                            'paths': [
                                'CollectionTime',
                                'CollectionCount',
                            ],
                            'tag_keys': ['name'],
                        },
                        {
                            'name': 'minecraft_mempool',
                            'mbean': 'java.lang:name=*,type=MemoryPool',
                            'paths': [
                                'CollectionUsage',
                                'PeakUsage',
                                'Usage',
                            ],
                            'tag_keys': ['name'],
                        },
                        {
                            'name': 'minecraft_os',
                            'mbean': 'java.lang:type=OperatingSystem',
                            'paths': [
                                'OpenFileDescriptorCount',
                                'ProcessCpuLoad',
                                'ProcessCpuTime',
                            ],
                        },
                    ],
                }],
            },
        },
    },
    'users': {
        'minecraft': {},
    },
    'zfs': {
        'datasets': {
            'tank/minecraft': {},
            'tank/minecraft/overworld': {
                'mountpoint': '/home/minecraft/world',
                'compression': 'on',
                'needed_by': {
                    'directory:/home/minecraft/world',
                },
            },
            'tank/minecraft/nether': {
                'mountpoint': '/home/minecraft/world_nether',
                'compression': 'on',
                'needed_by': {
                    'directory:/home/minecraft/world_nether',
                },
            },
            'tank/minecraft/end': {
                'mountpoint': '/home/minecraft/world_the_end',
                'compression': 'on',
                'needed_by': {
                    'directory:/home/minecraft/world_the_end',
                },
            },
        },
    },
}


@metadata_reactor.provides(
    'minecraft/java_opts',
)
def heap_to_java_opts(metadata):
    heap_mb = metadata.get('minecraft/heap_mb')

    return {
        'minecraft': {
            'java_opts': {
                f'-Xms{heap_mb}M': None,
                f'-Xmx{heap_mb}M': None,
            },
        },
    }


@metadata_reactor.provides(
    'firewall/port_rules/25565',
)
def firewall(metadata):
    return {
        'firewall': {
            'port_rules': {
                '25565': atomic(metadata.get('minecraft/restrict-to', set())),
            },
        },
    }