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

# .1.3.6.1.4.1.12148.9.3.1.0 --> ELTEK-DISTRIBUTED-MIB::batteryName.0
# .1.3.6.1.4.1.12148.9.3.2.0 5485 --> ELTEK-DISTRIBUTED-MIB::batteryVoltage.0
# .1.3.6.1.4.1.12148.9.3.3.0 0 --> ELTEK-DISTRIBUTED-MIB::batteryCurrent.0
# .1.3.6.1.4.1.12148.9.3.4.0 19 --> ELTEK-DISTRIBUTED-MIB::batteryTemp.0
# .1.3.6.1.4.1.12148.9.3.5.0 0 --> ELTEK-DISTRIBUTED-MIB::batteryBreakerStatus.0

#   .--breaker status------------------------------------------------------.
#   |   _                    _                   _        _                |
#   |  | |__  _ __ ___  __ _| | _____ _ __   ___| |_ __ _| |_ _   _ ___    |
#   |  | '_ \| '__/ _ \/ _` | |/ / _ \ '__| / __| __/ _` | __| | | / __|   |
#   |  | |_) | | |  __/ (_| |   <  __/ |    \__ \ || (_| | |_| |_| \__ \   |
#   |  |_.__/|_|  \___|\__,_|_|\_\___|_|    |___/\__\__,_|\__|\__,_|___/   |
#   |                                                                      |
#   +----------------------------------------------------------------------+
#   |                             main check                               |
#   '----------------------------------------------------------------------'


def parse_eltek_battery(info):
    voltage, current, temp, breaker_status = info[0]
    return {
        "supply": {
            "Supply": {
                "voltage": float(voltage) / 100,
                "current": float(current),
            }
        },
        "temp": float(temp),
        "breaker": breaker_status,
    }


def inventory_eltek_battery(parsed):
    if "breaker" in parsed:
        return [(None, None)]


def check_eltek_battery(_no_item, _no_params, parsed):
    if "breaker" in parsed:
        map_status = {
            "0": (0, "normal"),
            "1": (2, "alarm"),
        }
        state, state_readable = map_status[parsed["breaker"]]
        return state, "Status: %s" % state_readable


check_info["eltek_battery"] = {
    "parse_function": parse_eltek_battery,
    "inventory_function": inventory_eltek_battery,
    "check_function": check_eltek_battery,
    "service_description": "Battery Breaker Status",
    "snmp_info": (
        ".1.3.6.1.4.1.12148.9.3",
        [
            "2",  # batteryVoltage
            "3",  # batteryCurrent
            "4",  # batteryTemp
            "5",  # batteryBreakerStatus
        ],
    ),
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.12148.9"),
}

# .
#   .--temperature---------------------------------------------------------.
#   |      _                                      _                        |
#   |     | |_ ___ _ __ ___  _ __   ___ _ __ __ _| |_ _   _ _ __ ___       |
#   |     | __/ _ \ '_ ` _ \| '_ \ / _ \ '__/ _` | __| | | | '__/ _ \      |
#   |     | ||  __/ | | | | | |_) |  __/ | | (_| | |_| |_| | | |  __/      |
#   |      \__\___|_| |_| |_| .__/ \___|_|  \__,_|\__|\__,_|_|  \___|      |
#   |                       |_|                                            |
#   '----------------------------------------------------------------------'

# suggested by customer
factory_settings["eltek_battery_temp_default_variables"] = {
    "levels": (27.0, 35.0),
}


def inventory_eltek_battery_temp(parsed):
    if "temp" in parsed:
        return [("Battery", {})]


def check_eltek_battery_temp(item, params, parsed):
    # For temp checks we need an item but we have only one
    if "temp" in parsed:
        return check_temperature(parsed["temp"], params, "eltek_battery_temp_Battery")


check_info["eltek_battery.temp"] = {
    "inventory_function": inventory_eltek_battery_temp,
    "check_function": check_eltek_battery_temp,
    "service_description": "Temperature %s",
    "has_perfdata": True,
    "group": "temperature",
    "default_levels_variable": "eltek_battery_temp_default_variables",
}

# .
#   .--phase---------------------------------------------------------------.
#   |                           _                                          |
#   |                     _ __ | |__   __ _ ___  ___                       |
#   |                    | '_ \| '_ \ / _` / __|/ _ \                      |
#   |                    | |_) | | | | (_| \__ \  __/                      |
#   |                    | .__/|_| |_|\__,_|___/\___|                      |
#   |                    |_|                                               |
#   '----------------------------------------------------------------------'

# suggested by customer
factory_settings["eltek_battery_phase_default_variables"] = {
    "voltage": (52, 48),
    "current": (50, 76),
}

check_info["eltek_battery.supply"] = {
    "inventory_function": lambda parsed: discover()(parsed["supply"]),
    "check_function": lambda item, params, parsed: check_elphase(item, params, parsed["supply"]),
    "service_description": "Battery %s",
    "has_perfdata": True,
    "group": "el_inphase",
    "default_levels_variable": "eltek_battery_phase_default_variables",
}
