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

# No factory default because of different defaultlevels
carel_temp_defaultlevels = {
    "Room": (30, 35),
    "Outdoor": (60, 70),
    "Delivery": (60, 70),
    "Cold Water": (60, 70),
    "Hot Water": (60, 70),
    "Cold Water Outlet": (60, 70),
    "Circuit 1 Suction": (60, 70),
    "Circuit 2 Suction": (60, 70),
    "Circuit 1 Evap": (60, 70),
    "Circuit 2 Evap": (60, 70),
    "Circuit 1 Superheat": (60, 70),
    "Circuit 2 Superheat": (60, 70),
    "Cooling Set Point": (60, 70),
    "Cooling Prop. Band": (60, 70),
    "Cooling 2nd Set Point": (60, 70),
    "Heating Set Point": (60, 70),
    "Heating 2nd Set Point": (60, 70),
    "Heating Prop. Band": (60, 70),
}


def carel_sensors_parse(info):

    oid_parse = {
        "1.0": "Room",
        "2.0": "Outdoor",
        "3.0": "Delivery",
        "4.0": "Cold Water",
        "5.0": "Hot Water",
        "7.0": "Cold Water Outlet",
        "10.0": "Circuit 1 Suction",
        "11.0": "Circuit 2 Suction",
        "12.0": "Circuit 1 Evap",
        "13.0": "Circuit 2 Evap",
        "14.0": "Circuit 1 Superheat",
        "15.0": "Circuit 2 Superheat",
        "20.0": "Cooling Set Point",
        "21.0": "Cooling Prop. Band",
        "22.0": "Cooling 2nd Set Point",
        "23.0": "Heating Set Point",
        "24.0": "Heating 2nd Set Point",
        "25.0": "Heating Prop. Band",
    }

    parsed = {}
    for oidend, value in info:
        sensor_name = oid_parse.get(oidend)
        if sensor_name is not None and value is not None and value != "0" and value != "-9999":
            parsed[sensor_name] = float(value) / 10

    return parsed


def inventory_carel_sensors_temp(parsed):
    for sensor in parsed:
        levels = carel_temp_defaultlevels[sensor]
        yield sensor, {"levels": levels}


def check_carel_sensors_temp(item, params, parsed):
    if item in parsed:
        return check_temperature(parsed[item], params, "carel_sensors_temp_%s" % item)


check_info["carel_sensors"] = {
    "parse_function": carel_sensors_parse,
    "inventory_function": inventory_carel_sensors_temp,
    "check_function": check_carel_sensors_temp,
    "service_description": "Temperature %s",
    "group": "temperature",
    "has_perfdata": True,
    "snmp_info": (".1.3.6.1.4.1.9839.2.1", [OID_END, "2"]),
    "snmp_scan_function": lambda oid: (
        "pCO" in oid(".1.3.6.1.2.1.1.1.0") or oid(".1.3.6.1.2.1.1.1.0").endswith("armv4l")
    )
    and oid(".1.3.6.1.4.1.9839.1.1.0"),
}
