# CLI options
import re

CLI_OPT_TRACE = "--trace"

# mandatory parameters

ENV_GCN_MQTT_SERVER_HOST = "GCN_MQTT_SERVER_HOST"
ENV_GCN_MQTT_SERVER_PORT = "GCN_MQTT_SERVER_PORT"
ENV_GCN_MQTT_USER_NAME = "GCN_MQTT_USER_NAME"
ENV_GCN_MQTT_USER_PASSWORD = "GCN_MQTT_USER_PASSWORD"

# optional parameters

ENV_GCN_MQTT_TRANSPORT = "GCN_MQTT_TRANSPORT"
DEFAULT_GCN_MQTT_TRANSPORT = "tcp"

ENV_GCN_MQTT_TLS_MIN_VERSION = "GCN_MQTT_TLS_MIN_VERSION"
DEFAULT_GCN_MQTT_TLS_MIN_VERSION = None  # use python default for version

ENV_GCN_MQTT_TLS_MAX_VERSION = "GCN_MQTT_TLS_MAX_VERSION"
DEFAULT_GCN_MQTT_TLS_MAX_VERSION = None  # use python default for version

ENV_GCN_MQTT_TLS_CIPHERS = "GCN_MQTT_TLS_CIPHERS"
DEFAULT_GCN_MQTT_TLS_CIPHERS = None  # use python default for version

ENV_GCN_MQTT_KEEPALIVE_SECOND = "GCN_MQTT_KEEPALIVE_SECOND"
DEFAULT_GCN_MQTT_KEEPALIVE_SECOND = 60

ENV_GCN_MQTT_CONNECT_TIMEOUT_SECOND = "GCN_MQTT_CONNECT_TIMEOUT_SECOND"
DEFAULT_GCN_MQTT_CONNECT_TIMEOUT_SECOND = 3

ENV_GCN_MQTT_STILL_CONNECTING_ALERT = "GCN_MQTT_STILL_CONNECTING_ALERT"
DEFAULT_GCN_MQTT_STILL_CONNECTING_ALERT = 5 * 60

ENV_GCN_MQTT_RECONNECT = "GCN_MQTT_RECONNECT"
DEFAULT_GCN_MQTT_RECONNECT = False

ENV_GCN_MQTT_LOG_LEVEL = "GCN_MQTT_LOG_LEVEL"
DEFAULT_GCN_MQTT_LOG_LEVEL = "warning"

ENV_GCN_MQTT_CLIENT_ID_RANDOM_BYTES = "GCN_MQTT_CLIENT_ID_RANDOM_BYTES"
DEFAULT_GCN_MQTT_CLIENT_ID_RANDOM_BYTES = 4

ENV_GCN_MQTT_SOCKET_SEND_BUFFER_SIZE = "GCN_MQTT_SOCKET_SEND_BUFFER_SIZE"
DEFAULT_GCN_MQTT_SOCKET_SEND_BUFFER_SIZE = 2**11

# nothing fast is required, used for the queuing mqtt keepalives and message tasks cleanups
ENV_GCN_IDLE_LOOP_SLEEP_SEC = "GCN_IDLE_LOOP_SLEEP_SEC"
DEFAULT_GCN_IDLE_LOOP_SLEEP_SEC = 1

ENV_GCN_HEARTBEAT_MAX_SKEW_SEC = "GCN_HEARTBEAT_MAX_SKEW_SEC"
DEFAULT_GCN_HEARTBEAT_MAX_SKEW_SEC = 10 * 60

ENV_GCN_HEARTBEAT_WATCHDOG_SEC = "GCN_HEARTBEAT_WATCHDOG_SEC"
DEFAULT_GCN_HEARTBEAT_WATCHDOG_SEC = 60

ENV_GCN_CLIENT_GPIO_CHANGE_DEBOUNCE_SEC = "GCN_CLIENT_GPIO_CHANGE_DEBOUNCE_SEC"
DEFAULT_GCN_CLIENT_GPIO_CHANGE_DEBOUNCE_SEC = 0

# notifications

ENV_GCN_MANAGER_STARTING_RECIPIENTS = "GCN_MANAGER_STARTING_RECIPIENTS"
ENV_GCN_MANAGER_STILL_CONNECTING_RECIPIENTS = "GCN_MANAGER_STILL_CONNECTING_RECIPIENTS"
ENV_GCN_MANAGER_CONNECTED_RECIPIENTS = "GCN_MANAGER_CONNECTED_RECIPIENTS"
ENV_GCN_MANAGER_DISCONNECTED_RECIPIENTS = "GCN_MANAGER_DISCONNECTED_RECIPIENTS"
ENV_GCN_MANAGER_EXITING_RECIPIENTS = "GCN_MANAGER_EXITING_RECIPIENTS"

ENV_GCN_CLIENT_SKEWED_HEARTBEAT_RECIPIENTS = "GCN_CLIENT_HEARTBEAT_SKEWED_RECIPIENTS"
ENV_GCN_CLIENT_MISSED_HEARTBEAT_RECIPIENTS = "GCN_CLIENT_MISSED_HEARTBEAT_RECIPIENTS"
ENV_GCN_CLIENT_DROPPED_ITEMS_RECIPIENTS = "GCN_CLIENT_DROPPED_ITEMS_RECIPIENTS"
ENV_GCN_CLIENT_STATUS_CHANGE_ONLINE_RECIPIENTS = (
    "GCN_CLIENT_STATUS_CHANGE_ONLINE_RECIPIENTS"
)
ENV_GCN_CLIENT_STATUS_CHANGE_OFFLINE_RECIPIENTS = (
    "GCN_CLIENT_STATUS_CHANGE_OFFLINE_RECIPIENTS"
)
ENV_GCN_CLIENT_GPIO_CHANGE_UP_RECIPIENTS = "GCN_CLIENT_GPIO_CHANGE_UP_RECIPIENTS"
ENV_GCN_CLIENT_GPIO_CHANGE_DOWN_RECIPIENTS = "GCN_CLIENT_GPIO_CHANGE_DOWN_RECIPIENTS"

# providers

ENV_GCN_ENABLE_NOTIFY_SMS = "GCN_ENABLE_NOTIFY_SMS"
ENV_GCN_ENABLE_NOTIFY_EMAIL = "GCN_ENABLE_NOTIFY_EMAIL"
ENV_GCN_ENABLE_NOTIFY_TWITTER = "GCN_ENABLE_NOTIFY_TWITTER"

ENV_GCN_EMAIL_FROM = "GCN_EMAIL_FROM"
ENV_GCN_EMAIL_SMTP_HOST = "GCN_EMAIL_SMTP_HOST"
ENV_GCN_EMAIL_SMTP_PORT = "GCN_EMAIL_SMTP_PORT"
ENV_GCN_EMAIL_SMTP_USERNAME = "GCN_EMAIL_SMTP_USERNAME"
ENV_GCN_EMAIL_SMTP_PASSWORD = "GCN_EMAIL_SMTP_PASSWORD"
ENV_GCN_EMAIL_SMTP_STARTTLS = "GCN_EMAIL_SMTP_STARTTLS"
ENV_GCN_EMAIL_SMTP_DEBUG = "GCN_EMAIL_SMTP_DEBUG"
ENV_GCN_EMAIL_SMTP_DISABLE_TLS13 = "GCN_EMAIL_SMTP_DISABLE_TLS13"
ENV_GCN_EMAIL_SMTP_SEND_AS_REPORT = "GCN_EMAIL_SMTP_SEND_AS_REPORT"
ENV_GCN_EMAIL_SMTP_TIMEOUT_SEC = "GCN_EMAIL_SMTP_TIMEOUT_SEC"
DEFAULT_GCN_EMAIL_SMTP_TIMEOUT_SEC = 3

ENV_GCN_ALLOW_SMS_COUNTRIES = "GCN_ALLOW_SMS_COUNTRIES"
ENV_GCN_OVH_SMS_SENDER_NAME = "GCN_OVH_SMS_SENDER_NAME"
ENV_GCN_OVH_SMS_SERVICE_NAME = "GCN_OVH_SMS_SERVICE_NAME"
ENV_GCN_OVH_SMS_USER_NAME = "GCN_OVH_SMS_USER_NAME"
ENV_OVH_API_ENDPOINT_NAME = "OVH_ENDPOINT"
ENV_OVH_API_APPLICATION_KEY = "OVH_APPLICATION_KEY"
ENV_OVH_API_APPLICATION_SECRET = "OVH_APPLICATION_SECRET"
ENV_OVH_API_CONSUMER_KEY = "OVH_CONSUMER_KEY"
ENV_GCN_OVH_API_TIMEOUT_SEC = "GCN_OVH_API_TIMEOUT_SEC"
DEFAULT_GCN_OVH_API_TIMEOUT_SEC = 3

OVH_API_MAX_TIME_SKEW = 3
# https://github.com/ovh/python-ovh/blob/master/ovh/client.py#L67
KNOWN_OVH_API_ENDPOINT_NAMES = (
    "ovh-eu",
    "ovh-us",
    "ovh-ca",
    "kimsufi-eu",
    "kimsufi-ca",
    "soyoustart-eu",
    "soyoustart-ca",
)

ENV_GCN_TWITTER_APP_CONSUMER_KEY = "GCN_TWITTER_APP_CONSUMER_KEY"
ENV_GCN_TWITTER_APP_CONSUMER_SECRET = "GCN_TWITTER_APP_CONSUMER_SECRET"
ENV_GCN_TWITTER_USER_ACCESS_TOKEN = "GCN_TWITTER_USER_ACCESS_TOKEN"
ENV_GCN_TWITTER_USER_ACCESS_TOKEN_SECRET = "GCN_TWITTER_USER_ACCESS_TOKEN_SECRET"

# hardcoded values from Arduino program

MQTT_APP_CLIENT = "gcn"
MQTT_APP_CLIENT_ID = re.compile(r"^(?:[a-zA-Z0-9]{2}:){5}[a-zA-Z0-9]{2}$")
MQTT_APP_CLIENT_OUT = "out"
MQTT_APP_CLIENT_IN = "in"
MQTT_APP_CLIENT_STATUS = "status"
MQTT_APP_CLIENT_GPIO = "gpio"
MQTT_APP_CLIENT_MONITORED_GPIO = "monitored_gpio"
MQTT_APP_CLIENT_HEARTBEAT = "heartbeat"
MQTT_APP_CLIENT_BUFFER_TOTAL_DROPPED_ITEM = "buffer_total_dropped_item"
MQTT_APP_CLIENT_SUBSCRIPTION = f"{MQTT_APP_CLIENT}/+/{MQTT_APP_CLIENT_OUT}"

# hardcoded values from Brain program

MQTT_APP_MANAGER = "gcn_manager"
MQTT_APP_MANAGER_ID = re.compile(r"^gcn_manager_[a-zA-Z0-9]+$")
MQTT_APP_MANAGER_STATUS = "status"
MQTT_APP_MANAGER_STATUS_ONLINE = "online"
MQTT_APP_MANAGER_STATUS_OFFLINE = "offline"
MQTT_APP_MANAGER_STATUS_TOPIC = f"{MQTT_APP_MANAGER}/{MQTT_APP_MANAGER_STATUS}"
MQTT_APP_MANAGER_STATUS_SUBSCRIPTION = f"{MQTT_APP_MANAGER_STATUS_TOPIC}/+"
