#!/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.perle import *  # pylint: disable=wildcard-import,unused-wildcard-import


def inventory_perle_chassis_slots(info):
    return [
        (index, None)
        for index, _name, _modelname, _serial, _bootloader, _fw, _alarms_str, _diagstate, ty, _descr in info
        if ty != "0"
    ]


def check_perle_chassis_slots(item, _no_params, info):
    map_diagstates = {
        "0": (0, "passed"),
        "1": (2, "media converter module's PHY is not functional"),
        "2": (1, "firmware download required"),
    }

    for (
        index,
        name,
        _modelname,
        _serial,
        _bootloader,
        _fw,
        alarms_str,
        diagstate,
        _ty,
        _descr,
    ) in info:
        if item == index:
            state, state_readable = map_diagstates[diagstate]
            yield state, "[%s] Diagnostic result: %s" % (name, state_readable)
            yield perle_check_alarms(alarms_str)


check_info["perle_chassis_slots"] = {
    # section is already migrated!
    "inventory_function": inventory_perle_chassis_slots,
    "check_function": check_perle_chassis_slots,
    "service_description": "Chassis status slot %s",
}
