#!/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.

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.elphase import *  # pylint: disable=wildcard-import,unused-wildcard-import

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.perle import *  # pylint: disable=wildcard-import,unused-wildcard-import


def inventory_perle_psmu(parsed, what_state, params=None):
    for unit, values in parsed.items():
        if values[what_state][1] != "not present":
            yield unit, params


def check_perle_psmu_powersupplies(item, params, parsed):
    if item in parsed:
        state, state_readable = parsed[item]["psustate"]
        yield state, "Status: %s" % state_readable
        for res in check_elphase(item, params, parsed):
            yield res


check_info["perle_psmu"] = {
    # section is already migrated!
    "inventory_function": lambda info: inventory_perle_psmu(info, "psustate", {}),
    "check_function": check_perle_psmu_powersupplies,
    "service_description": "Power supply %s",
    "has_perfdata": True,
    "group": "el_inphase",
}


def check_perle_psmu_fans(item, _no_params, parsed):
    if item in parsed:
        state, state_readable = parsed[item]["fanstate"]
        return state, "Status: %s" % state_readable


check_info["perle_psmu.fan"] = {
    "inventory_function": lambda info: inventory_perle_psmu(info, "fanstate", {}),
    "check_function": check_perle_psmu_fans,
    "service_description": "Fan %s",
}
