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

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

cmctc_lcp_sensors = {
    4: (None, "access"),
    12: (None, "humidity"),
    # User Sensors
    13: ("normally open", "user"),
    14: ("normally closed", "user"),
    # Leakage
    23: (None, "flow"),
    30: (None, "current"),
    31: (None, "status"),
    32: (None, "position"),
    # Blower
    40: ("1", "blower"),
    41: ("2", "blower"),
    42: ("3", "blower"),
    43: ("4", "blower"),
    44: ("5", "blower"),
    45: ("6", "blower"),
    46: ("7", "blower"),
    47: ("8", "blower"),
    # Server in/out
    48: ("Server in 1", "temp"),
    49: ("Server out 1", "temp"),
    50: ("Server in 2", "temp"),
    51: ("Server out 2", "temp"),
    52: ("Server in 3", "temp"),
    53: ("Server out 3", "temp"),
    54: ("Server in 4", "temp"),
    55: ("Server out 4", "temp"),
    # Overview Server
    56: ("Overview Server in", "temp"),
    57: ("Overview Server out", "temp"),
    # Water
    58: ("Water in", "temp"),
    59: ("Water out", "temp"),
    60: (None, "flow"),
    # Other stuff
    61: (None, "blowergrade"),
    62: (None, "regulator"),
}


def inventory_cmctc_lcp(info, sensortype):
    inventory = []
    for index, typeid, _status, _value, _high, _low, _warn, _description in info:
        typeid = saveint(typeid)
        if typeid in cmctc_lcp_sensors:
            item, st = cmctc_lcp_sensors[typeid]
            if st == sensortype:
                if item:
                    item = item + " - " + index
                else:
                    item = index
                inventory.append((item, None))
    return inventory


def check_cmctc_lcp(item, params, info, sensortype):
    map_sensor_state = {
        "1": (3, "not available"),
        "2": (2, "lost"),
        "3": (1, "changed"),
        "4": (0, "ok"),
        "5": (2, "off"),
        "6": (0, "on"),
        "7": (1, "warning"),
        "8": (2, "too low"),
        "9": (2, "too high"),
        "10": (2, "error"),
    }

    map_unit = {
        "access": "",
        "current": " A",
        "status": "",
        "position": "",
        "temp": " °C",
        "blower": " RPM",
        "blowergrade": "",
        "humidity": "%",
        "flow": " l/min",
        "regulator": "%",
        "user": "",
    }

    itemindex = item.split(" - ")[-1]
    for index, _typeid, statuscode, value, high, low, warn, description in info:
        if itemindex == index:
            unit = map_unit[sensortype]
            value = int(value)
            infotext = ""
            if description:
                infotext += "[%s] " % description
            state, extra_info = map_sensor_state[statuscode]
            yield state, "%s%d%s" % (infotext, value, unit)

            extra_state = 0
            if params:
                warn, crit = params
                perfdata = [(sensortype, value, warn, crit)]
                if value >= crit:
                    extra_state = 2
                elif value >= warn:
                    extra_state = 1

                if extra_state:
                    extra_info += " (warn/crit at %d/%d%s)" % (warn, crit, unit)
            else:
                perfdata = [(sensortype, value)]
                # Assumption: if high and low are both 0
                # then there are no device levels
                if not (int(high) == 0 and int(low) == 0) and int(high) > int(low):
                    if value >= int(high) or value <= int(low):
                        extra_state = 2
                        extra_info += " (device lower/upper crit at %s/%s%s)" % (low, high, unit)

            yield extra_state, extra_info, perfdata


def inventory_cmctc_lcp_temp(info):
    inventory = []
    for index, typeid, _status, _value, _high, _low, _warn, _description in info:
        typeid = saveint(typeid)
        if typeid in cmctc_lcp_sensors:
            item, st = cmctc_lcp_sensors[typeid]
            if st == "temp":
                if item:
                    item = item + " - " + index
                else:
                    item = index
                inventory.append((item, None))
    return inventory


def check_cmctc_lcp_temp(item, params, info):
    itemindex = item.split(" - ")[-1]
    for index, _typeid, statuscode, value, high, low, warn, _description in info:
        if itemindex == index:
            status = int(statuscode)
            levels = None if high == low == warn == "0" else (float(warn), float(high))
            levels_low = (
                None if high == low == warn == "0" else (float(low), float("-inf"))
            )  # no lower critical level specified
            return check_temperature(
                float(value),
                params,
                "cmctc_lcp_temp_%s" % item,
                dev_levels=levels,
                dev_levels_lower=levels_low,
                dev_status=cmctc_translate_status(status),
                dev_status_name="Unit: %s" % cmctc_translate_status_text(status),
            )
    return 3, "Sensor not found in SNMP output", []


snmp_scan_functions["cmctc_lcp"] = cmctc_snmp_scan_function

snmp_info["cmctc_lcp"] = (
    ".1.3.6.1.4.1.2606.4.2",
    [
        "3",  # cmcTcUnit1OutputTable
        "4",  # cmcTcUnit2OutputTable
        "5",  # cmcTcUnit3OutputTable
        "6",  # cmcTcUnit4OutputTable
    ],
    [
        "5.2.1.1",  # Index
        "5.2.1.2",  # Sensor Type
        "5.2.1.4",  # Status
        "5.2.1.5",  # Value
        "5.2.1.6",  # High
        "5.2.1.7",  # Low
        "5.2.1.8",  # Warn
        "7.2.1.2",  # Description
    ],
)

check_info["cmctc_lcp.access"] = {
    "check_function": lambda item, params, info: check_cmctc_lcp(item, params, info, "access"),
    "inventory_function": lambda info: inventory_cmctc_lcp(info, "access"),
    "has_perfdata": True,
    "service_description": "Access %s",
}

check_info["cmctc_lcp.blower"] = {
    "check_function": lambda item, params, info: check_cmctc_lcp(item, params, info, "blower"),
    "inventory_function": lambda info: inventory_cmctc_lcp(info, "blower"),
    "has_perfdata": True,
    "service_description": "Blower %s",
}

check_info["cmctc_lcp.blowergrade"] = {
    "check_function": lambda item, params, info: check_cmctc_lcp(item, params, info, "blowergrade"),
    "inventory_function": lambda info: inventory_cmctc_lcp(info, "blowergrade"),
    "has_perfdata": True,
    "service_description": "Blower Grade %s",
}

check_info["cmctc_lcp.current"] = {
    "check_function": lambda item, params, info: check_cmctc_lcp(item, params, info, "current"),
    "inventory_function": lambda info: inventory_cmctc_lcp(info, "current"),
    "has_perfdata": True,
    "service_description": "Current %s",
}

check_info["cmctc_lcp.flow"] = {
    "check_function": lambda item, params, info: check_cmctc_lcp(item, params, info, "flow"),
    "inventory_function": lambda info: inventory_cmctc_lcp(info, "flow"),
    "has_perfdata": True,
    "service_description": "Waterflow %s",
}

check_info["cmctc_lcp.humidity"] = {
    "check_function": lambda item, params, info: check_cmctc_lcp(item, params, info, "humidity"),
    "inventory_function": lambda info: inventory_cmctc_lcp(info, "humidity"),
    "has_perfdata": True,
    "service_description": "Humidity %s",
}

check_info["cmctc_lcp.position"] = {
    "check_function": lambda item, params, info: check_cmctc_lcp(item, params, info, "position"),
    "inventory_function": lambda info: inventory_cmctc_lcp(info, "position"),
    "has_perfdata": True,
    "service_description": "Position %s",
}

check_info["cmctc_lcp.regulator"] = {
    "check_function": lambda item, params, info: check_cmctc_lcp(item, params, info, "regulator"),
    "inventory_function": lambda info: inventory_cmctc_lcp(info, "regulator"),
    "has_perfdata": True,
    "service_description": "Regulator %s",
}

check_info["cmctc_lcp.status"] = {
    "check_function": lambda item, params, info: check_cmctc_lcp(item, params, info, "status"),
    "inventory_function": lambda info: inventory_cmctc_lcp(info, "status"),
    "has_perfdata": True,
    "service_description": "Status %s",
}

check_info["cmctc_lcp.user"] = {
    "check_function": lambda item, params, info: check_cmctc_lcp(item, params, info, "user"),
    "inventory_function": lambda info: inventory_cmctc_lcp(info, "user"),
    "has_perfdata": True,
    "service_description": "User Sensor %s",
}

# temperature check is standardised
check_info["cmctc_lcp.temp"] = {
    "check_function": check_cmctc_lcp_temp,
    "inventory_function": inventory_cmctc_lcp_temp,
    "has_perfdata": True,
    "service_description": "Temperature %s",
    "group": "temperature",
}
