#!/usr/bin/env python3
# pylint: disable=line-too-long, missing-module-docstring

from cmk.rulesets.v1 import Help, Label, Title
from cmk.rulesets.v1.form_specs import (
    BooleanChoice,
    DictElement,
    Dictionary,
    migrate_to_password,
    Password,
    String,
    Integer,
    validators,
    InputHint,
)
from cmk.rulesets.v1.rule_specs import SpecialAgent, Topic


def _form_spec_special_agent_nextcloud() -> Dictionary:
    return Dictionary(
        title=Title("Nextcloud Server Information"),
        help_text=Help("Checking Nextcloud servers via API"),
        elements={
            "hostname": DictElement(
                required=True,
                parameter_form=String(
                    title=Title("Hostname"),
                    help_text=Help(
                        "Hostname of Nextcloud server (bare FQDN or IP), mandatory, eg. nextcloud.yourdomain.tld"
                    ),
                    custom_validate=(validators.LengthInRange(min_value=1),),
                    prefill=InputHint("nextcloud.yourdomain.tld"),
                ),
            ),
            "username": DictElement(
                required=True,
                parameter_form=String(
                    title=Title("Username"),
                    help_text=Help("Username with administrative rights, mandatory"),
                    custom_validate=(validators.LengthInRange(min_value=1),),
                    prefill=InputHint("admin"),
                ),
            ),
            "password": DictElement(
                required=True,
                parameter_form=Password(
                    title=Title("App Password"),
                    help_text=Help(
                        "Specify app password, mandatory, use Personal Settings|Security|Devices and Sessions within the NC UI to create one for the given user"
                    ),
                    custom_validate=(validators.LengthInRange(min_value=1),),
                    migrate=migrate_to_password,
                ),
            ),
            "port": DictElement(
                required=False,
                parameter_form=Integer(
                    title=Title("Port"),
                    help_text=Help(
                        "Specify port if not listening to HTTP(S), optional"
                    ),
                    prefill=InputHint(8080),
                    custom_validate=(validators.NetworkPort(),),
                ),
            ),
            "folder": DictElement(
                required=False,
                parameter_form=String(
                    title=Title("Folder"),
                    help_text=Help(
                        "Specify subfolder if your Nextcloud instance is not installed in the web root, no trailing/leading slashes, optional"
                    ),
                    prefill=InputHint("nextcloud"),
                ),
            ),
            "no_https": DictElement(
                required=False,
                parameter_form=BooleanChoice(
                    title=Title("Protocol handling"),
                    label=Label("Disable HTTPS"),
                    help_text=Help(
                        "Activate to disable encryption (not recommended), optional"
                    ),
                ),
            ),
            "no_cert_check": DictElement(
                required=False,
                parameter_form=BooleanChoice(
                    title=Title("Certificate handling"),
                    label=Label("Disable certificate validation"),
                    help_text=Help(
                        "Activate to disable certificate validation (not recommended), optional"
                    ),
                ),
            ),
        },
    )


rule_spec_nextcloud = SpecialAgent(
    name="nextcloud",
    title=Title("Nextcloud connection parameters"),
    topic=Topic.APPLICATIONS,
    parameter_form=_form_spec_special_agent_nextcloud,
)