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

# .1.3.6.1.4.1.2606.4.2.3.1.0 2
# .1.3.6.1.4.1.2606.4.2.3.2.0 CMC-TC-IOU
# .1.3.6.1.4.1.2606.4.2.3.3.0 60263
# .1.3.6.1.4.1.2606.4.2.3.4.0 1


def parse_cmctc_ports(info):
    def parse_single_port(port_info):
        type_map = {
            "1": "not available",
            "2": "IO",
            "3": "Access",
            "4": "Climate",
            "5": "FCS",
            "6": "RTT",
            "7": "RTC",
            "8": "PSM",
            "9": "PSM8",
            "10": "PSM metered",
            "11": "IO wireless",
            "12": "PSM6 Schuko",
            "13": "PSM6C19",
            "14": "Fuel Cell",
            "15": "DRC",
            "16": "TE cooler",
            "17": "PSM32 metered",
            "18": "PSM8x8",
            "19": "PSM6x6 Schuko",
            "20": "PSM6x6C19",
        }

        status_map = {
            "1": "ok",
            "2": "error",
            "3": "configuration changed",
            "4": "quit from sensor unit",
            "5": "timeout",
            "6": "unit detected",
            "7": "not available",
            "8": "supply voltage low",
        }

        _, device_type, description, serial_number, device_status = port_info

        parsed = {
            "type": type_map.get(device_type),
            "status": status_map.get(device_status),
            "serial": serial_number,
        }

        if parsed["status"] == "not available":
            return None

        return description, parsed

    parsed = {}
    # cmctc_lcp uses port numbers the range 3-6.
    # Therefore, we start counting at 3 here as well
    # to stay consistent.
    for number, port_info in enumerate(info, 3):
        parsed_port = parse_single_port(port_info)
        if parsed_port:
            description, entry = parsed_port
            name = "%d %s" % (number, description)
            parsed[name] = entry

    return parsed


def inventory_cmctc_ports(parsed):
    for entry in parsed:
        yield entry, {}


def check_cmctc_ports(item, _no_params, parsed):
    port = parsed.get(item)
    if not port:
        return None

    status_map = {
        "ok": 0,
        "configuration changed": 1,
        "unit detected": 1,
        "error": 2,
        "quit from sensor unit": 2,
        "timeout": 2,
        "not available": 2,
        "supply voltage low": 2,
    }

    state = status_map.get(port["status"], 3)
    infotext = ("Status: %(status)s, " "Device type: %(type)s, " "Serial number: %(serial)s") % port

    return state, infotext


check_info["cmctc_ports"] = {
    "parse_function": parse_cmctc_ports,
    "inventory_function": inventory_cmctc_ports,
    "check_function": check_cmctc_ports,
    "service_description": "Port %s",
    "snmp_scan_function": cmctc_snmp_scan_function,
    "snmp_info": (
        ".1.3.6.1.4.1.2606.4.2",  # RITTAL-CMC-TC-MIB
        [
            "3",  # cmcTcStatusSensorUnit1
            "4",  # cmcTcStatusSensorUnit2
            "5",  # cmcTcStatusSensorUnit3
            "6",  # cmcTcStatusSensorUnit4
        ],
        [
            OID_END,
            "1",  # cmcTcUnit[1-4]TypeOfDevice
            "2",  # cmcTcUnit[1-4]Text
            "3",  # cmcTcUnit[1-4]Serial
            "4",  # cmcTcUnit[1-4]Status
        ],
    ),
}
