from bundlewrap.exceptions import BundleError

repo.libs.tools.require_bundle(node, 'systemd')

files = {
    '/etc/network/interfaces': {
        'delete': True,
    },
}

svc_systemd = {
    'systemd-networkd': {
        'before': {
            'git_deploy:',
        },
    },
    'systemd-resolved': {
        'running': False,
        'enabled': False,
    },
}

directories = {
    '/etc/systemd/network': {
        'purge': True,
        'needed_by': {
            'svc_systemd:systemd-networkd',
        },
        'triggers': {
            'svc_systemd:systemd-networkd:restart',
        },
    },
}

mac_host_prefix = '%04x' % (node.magic_number % 65534)
generated_mac = f'52:54:00:{mac_host_prefix[0:2]}:{mac_host_prefix[2:4]}:{{}}'
all_interfaces_use_dhcp = True

for interface, config in node.metadata.get('interfaces').items():
    if config.get('dhcp', False):
        if 'vlans' in config:
            raise BundleError(f'{node.name} interface {interface} cannot use vlans and dhcp!')
    else:
        all_interfaces_use_dhcp = False

    if '.' in interface:
        vlan_id = int(interface.split('.')[1])
        vlan_hex = '%02x' % (vlan_id % 255)
        files[f'/etc/systemd/network/{interface}.netdev'] = {
            'source': 'template-iface-vlan.netdev',
            'content_type': 'mako',
            'context': {
                'interface': interface,
                'vlan': vlan_id,
                'mac': generated_mac.format(vlan_hex)
            },
            'needed_by': {
                'svc_systemd:systemd-networkd',
            },
            'triggers': {
                'svc_systemd:systemd-networkd:restart',
            },
        }
    elif interface.startswith('dummy'):
        files[f'/etc/systemd/network/{interface}.netdev'] = {
            'source': 'template-dummy.netdev',
            'content_type': 'mako',
            'context': {
                'name': interface,
            },
            'needed_by': {
                'svc_systemd:systemd-networkd',
            },
            'triggers': {
                'svc_systemd:systemd-networkd:restart',
            },
        }

    if not config.get('ignore', False):
        files[f'/etc/systemd/network/{interface}.network'] = {
            'source': 'template-iface.network',
            'content_type': 'mako',
            'context': {
                'interface': interface,
                'config': config,
            },
            'needed_by': {
                'svc_systemd:systemd-networkd',
            },
            'triggers': {
                'svc_systemd:systemd-networkd:restart',
            },
        }

if not all_interfaces_use_dhcp:
    files['/etc/resolv.conf'] = {
        'content_type': 'mako',
    }

for bond, config in node.metadata.get('systemd-networkd/bonds', {}).items():
    files[f'/etc/systemd/network/{bond}.netdev'] = {
        'source': 'template-bond.netdev',
        'content_type': 'mako',
        'context': {
            'bond': bond,
            'mode': config.get('mode', '802.3ad'),
            'prio': config.get('priority', '32768'),
        },
        'needed_by': {
            'svc_systemd:systemd-networkd',
        },
        'triggers': {
            'svc_systemd:systemd-networkd:restart',
        },
    }

    files[f'/etc/systemd/network/{bond}.network'] = {
        'source': 'template-bond.network',
        'content_type': 'mako',
        'context': {
            'bond': bond,
            'match': config['match'],
        },
        'needed_by': {
            'svc_systemd:systemd-networkd',
        },
        'triggers': {
            'svc_systemd:systemd-networkd:restart',
        },
    }

for brname, config in node.metadata.get('systemd-networkd/bridges', {}).items():
    filename = '{}-match-{}'.format(
        brname,
        '-'.join(sorted(config['match'])),
    )

    files[f'/etc/systemd/network/{brname}.netdev'] = {
        'source': 'template-bridge.netdev',
        'content_type': 'mako',
        'context': {
            'bridge': brname,
        },
        'needed_by': {
            'svc_systemd:systemd-networkd',
        },
        'triggers': {
            'svc_systemd:systemd-networkd:restart',
        },
    }

    files[f'/etc/systemd/network/{filename}.network'] = {
        'source': 'template-bridge.network',
        'content_type': 'mako',
        'context': {
            'bridge': brname,
            'match': config['match'],
        },
        'needed_by': {
            'svc_systemd:systemd-networkd',
        },
        'triggers': {
            'svc_systemd:systemd-networkd:restart',
        },
    }

    if config.get('vlans', set()):
        files[f'/etc/systemd/network/{brname}.network'] = {
            'source': 'template-bridge-vlan.network',
            'content_type': 'mako',
            'context': {
                'bridge': brname,
                'vlans': config.get('vlans', set()),
            },
            'needed_by': {
                'svc_systemd:systemd-networkd',
            },
            'triggers': {
                'svc_systemd:systemd-networkd:restart',
            },
        }
