
from __future__ import absolute_import
from __future__ import division

from collections import defaultdict

_WINDOWS_VARIANT_DESKTOP = 'client'
_WINDOWS_VARIANT_SERVER = 'server'

_ARCH_PPC = 'ppc'
_ARCH_PPCLE = 'ppcle'
_ARCH_PPC64 = 'ppc64'
_ARCH_PPC64LE = 'ppc64le'
_ARCH_X86 = 'x86'
_ARCH_X86_64 = 'x86_64'


def translate_arch(arch):
    arch_map = {
        _ARCH_PPC: _ARCH_PPC,
        _ARCH_PPCLE: _ARCH_PPCLE,
        _ARCH_PPC64: _ARCH_PPC64,
        _ARCH_PPC64LE: _ARCH_PPC64LE,
        _ARCH_X86: _ARCH_X86,
        _ARCH_X86_64: _ARCH_X86_64,
        'i686': _ARCH_X86,
        'i586': _ARCH_X86,
        'i386': _ARCH_X86,
    }
    return arch_map.get(arch, 'unknown')


def translate_fsinfo(filesystem):
    filesystem = defaultdict(str, filesystem)
    return {
        "path": filesystem['mountpoint'],
        "total": str(filesystem['total-bytes']),
        "used": str(filesystem['used-bytes']),
        "fs": filesystem['type'],
    }


def translate_linux_osinfo(os_info):

    os_info = defaultdict(str, os_info)
    return {
        'guestOs': os_info['kernel-release'],
        'guestOsInfo': {
            'type': 'linux',
            'arch': translate_arch(os_info['machine']),
            'kernel': os_info['kernel-release'],
            'distribution': os_info['name'],
            'version': os_info['version-id'],
            'codename': os_info['variant'],
        }
    }


def translate_windows_osinfo(os_info):
    os_info = defaultdict(str, os_info)
    return {
        'guestOs': os_info['pretty-name'],
        'guestOsInfo': {
            'type': 'windows',
            'arch': translate_arch(os_info['machine']),
            'kernel': '',
            'distribution': '',
            'version': os_info['kernel-version'],
            'codename': os_info['pretty-name'],
        }
    }
