#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# Example output:
# <<<win_video:sep(58)>>>
# Name                 : VirtualBox Graphics Adapter
# Description          : VirtualBox Graphics Adapter
# Caption              : VirtualBox Graphics Adapter
# AdapterCompatibility : Oracle Corporation
# VideoProcessor       :
# DriverVersion        : 4.3.10.0
# DriverDate           : 20140326000000.000000-000
# MaxMemorySupported   :


def inv_win_video(info):
    node = inv_tree_list("hardware.video:")
    array = {}
    first_varname = None
    for line in info:
        if len(line) > 2:
            line = [line[0], ":".join(line[1:])]
        varname, value = line
        varname = re.sub(" *", "", varname)
        value = re.sub("^ ", "", value)

        if first_varname and varname == first_varname:
            # Looks like we have a new instance
            node.append(array)
            array = {}

        if not first_varname:
            first_varname = varname

        if varname == "Name":
            array["name"] = value
        elif varname == "DriverVersion":
            array["driver_version"] = value
        elif varname == "DriverDate":
            try:
                array["driver_date"] = int(
                    time.mktime(time.strptime(value.split(".")[0], "%Y%m%d%H%M%S")))
            except ValueError:
                pass
        elif varname == "AdapterRAM":
            array["graphic_memory"] = saveint(value)

    node.append(array)


inv_info['win_video'] = {
    "inv_function": inv_win_video,
}
