#!/user/bin/env python3
"""HTTP components parameter form for Traefik"""

from cmk.rulesets.v1 import Title, Help
from cmk.rulesets.v1.form_specs import (
    DictElement,
    Dictionary,
    SimpleLevels,
    DefaultValue,
    Integer,
    LevelDirection,
    Float,
)

from cmk.rulesets.v1.rule_specs import CheckParameters, Topic, HostCondition


# function name should begin with an underscore to limit it's visibility
def _parameter_form():
    return Dictionary(
        elements={
            "levels_traefik_min_tcp_routers": DictElement(
                parameter_form=SimpleLevels(
                    title=Title("Levels for minimum number of TCP routers"),
                    help_text=Help(
                        "Define the levels for the minimum number of TCP routers"
                    ),
                    form_spec_template=Integer(unit_symbol=""),
                    level_direction=LevelDirection.LOWER,
                    prefill_fixed_levels=DefaultValue(value=(0, 0)),
                ),
                required=True,
            ),
            "levels_traefik_max_tcp_routers": DictElement(
                parameter_form=SimpleLevels(
                    title=Title("Levels for maximum number of TCP routers"),
                    help_text=Help(
                        "Define the levels for the maximum number of TCP routers"
                    ),
                    form_spec_template=Integer(unit_symbol=""),
                    level_direction=LevelDirection.UPPER,
                    prefill_fixed_levels=DefaultValue(value=(25, 50)),
                ),
                required=True,
            ),
            "levels_traefik_min_tcp_services": DictElement(
                parameter_form=SimpleLevels(
                    title=Title("Levels for minimum number of TCP services"),
                    help_text=Help(
                        "Define the levels for the minimum number of TCP services"
                    ),
                    form_spec_template=Integer(unit_symbol=""),
                    level_direction=LevelDirection.LOWER,
                    prefill_fixed_levels=DefaultValue(value=(0, 0)),
                ),
                required=True,
            ),
            "levels_traefik_max_tcp_services": DictElement(
                parameter_form=SimpleLevels(
                    title=Title("Levels for maximum number of TCP services"),
                    help_text=Help(
                        "Define the levels for the maximum number of TCP services"
                    ),
                    form_spec_template=Integer(unit_symbol=""),
                    level_direction=LevelDirection.UPPER,
                    prefill_fixed_levels=DefaultValue(value=(25, 50)),
                ),
                required=True,
            ),
            "levels_traefik_min_tcp_middlewares": DictElement(
                parameter_form=SimpleLevels(
                    title=Title("Levels for minimum number of TCP middlewares"),
                    help_text=Help(
                        "Define the levels for the minimum number of TCP middlewares"
                    ),
                    form_spec_template=Integer(unit_symbol=""),
                    level_direction=LevelDirection.LOWER,
                    prefill_fixed_levels=DefaultValue(value=(0, 0)),
                ),
                required=True,
            ),
            "levels_traefik_max_tcp_middlewares": DictElement(
                parameter_form=SimpleLevels(
                    title=Title("Levels for maximum number of TCP middlewares"),
                    help_text=Help(
                        "Define the levels for the maximum number of TCP middlewares"
                    ),
                    form_spec_template=Integer(unit_symbol=""),
                    level_direction=LevelDirection.UPPER,
                    prefill_fixed_levels=DefaultValue(value=(25, 50)),
                ),
                required=True,
            ),
            "levels_traefik_tcp_components_not_ok": DictElement(
                parameter_form=SimpleLevels(
                    title=Title("Levels for percentage of not OK TCP components"),
                    help_text=Help(
                        "Define the levels for the maximum number of TCP components in not OK state"
                    ),
                    form_spec_template=Float(unit_symbol="%"),
                    level_direction=LevelDirection.UPPER,
                    prefill_fixed_levels=DefaultValue(value=(0.5, 1.0)),
                ),
                required=True,
            ),
        }
    )


# name must begin with "rule_spec_", should refer to the used check plugin
# must be an instance of "CheckParameters"
rule_spec_traefik_tcp_components = CheckParameters(
    # "name" should be the same as the check plugin
    name="traefik_tcp_components",
    # the title is shown in the GUI
    title=Title("Traefik TCP components parameters"),
    # this ruleset can be found under Setup|Service monitoring rules|Applications...
    topic=Topic.APPLICATIONS,
    # define the name of the function which creates the GUI elements
    parameter_form=_parameter_form,
    condition=HostCondition(),
)