// This file is part of MinIO Console Server
// Copyright (c) 2021 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

import { NotificationEndpointItem } from "./types";

import { IElementValue } from "../Configurations/types";

export const notifyPostgres = "notify_postgres";
export const notifyMysql = "notify_mysql";
export const notifyKafka = "notify_kafka";
export const notifyAmqp = "notify_amqp";
export const notifyMqtt = "notify_mqtt";
export const notifyRedis = "notify_redis";
export const notifyNats = "notify_nats";
export const notifyElasticsearch = "notify_elasticsearch";
export const notifyWebhooks = "notify_webhook";
export const notifyNsq = "notify_nsq";

export const notificationTransform = (
  notificationElements: NotificationEndpointItem[]
) => {
  return notificationElements.map((element) => {
    return {
      service_name: `${element.service}:${element.account_id}`,
      status: element.status,
    };
  });
};

export const servicesList = [
  {
    actionTrigger: notifyPostgres,
    targetTitle: "Postgres SQL",
    logo: "/postgres-logo.svg",
  },
  {
    actionTrigger: notifyKafka,
    targetTitle: "Kafka",
    logo: "/kafka-logo.svg",
  },
  {
    actionTrigger: notifyAmqp,
    targetTitle: "AMQP",
    logo: "/amqp-logo.svg",
  },
  {
    actionTrigger: notifyMqtt,
    targetTitle: "MQTT",
    logo: "/mqtt-logo.svg",
  },
  {
    actionTrigger: notifyRedis,
    targetTitle: "Redis",
    logo: "/redis-logo.svg",
  },
  {
    actionTrigger: notifyNats,
    targetTitle: "NATS",
    logo: "/nats-logo.svg",
  },
  {
    actionTrigger: notifyMysql,
    targetTitle: "Mysql",
    logo: "/mysql-logo.svg",
  },
  {
    actionTrigger: notifyElasticsearch,
    targetTitle: "Elastic Search",
    logo: "/elasticsearch-logo.svg",
  },
  {
    actionTrigger: notifyWebhooks,
    targetTitle: "Webhook",
    logo: "/webhooks-logo.svg",
  },
  {
    actionTrigger: notifyNsq,
    targetTitle: "NSQ",
    logo: "/nsq-logo.svg",
  },
];

const commonFields = [
  {
    name: "queue-dir",
    label: "队列目录",
    required: true,

    tooltip: "未送达邮件的暂存目录，例如“/home/events”",
    type: "string",
    placeholder: "输入队列目录",
  },
  {
    name: "queue-limit",
    label: "排队限制",
    required: false,

    tooltip: "未送达邮件的最大限制，默认为“10000”",
    type: "number",
    placeholder: "输入排队限制",
  },
  {
    name: "comment",
    label: "解释",
    required: false,
    type: "comment",
    placeholder: "输入解释",
  },
];

export const removeEmptyFields = (formFields: IElementValue[]) => {
  const nonEmptyFields = formFields.filter((field) => field.value !== "");

  return nonEmptyFields;
};

export const notificationEndpointsFields: any = {
  [notifyKafka]: [
    {
      name: "brokers",
      label: "经纪人",
      required: true,

      tooltip: "以逗号分隔的卡夫卡经纪人地址列表",
      type: "string",
      placeholder: "输入经纪人",
    },
    {
      name: "topic",
      label: "话题",
      tooltip: "用于bucket通知的卡夫卡主题",
      type: "string",
      placeholder: "输入话题",
    },
    {
      name: "sasl_username",
      label: "SASL Username",
      tooltip: "SASL用户名",
      type: "string",
      placeholder: "输入SASL用户名",
    },
    {
      name: "sasl_password",
      label: "SASL密码",
      tooltip: "SASL密码",
      type: "string",
      placeholder: "输入SASL密码",
    },
    {
      name: "sasl_mechanism",
      label: "SASL机制",
      tooltip: "SASL身份验证机制，默认为“普通”",
      type: "string",
    },
    {
      name: "tls_client_auth",
      label: "TLS客户端验证",
      tooltip:
        "客户端身份验证确定Kafka服务器对TLS客户端身份验证的策略",
      type: "string",
      placeholder: "输入TLS客户端验证",
    },
    {
      name: "sasl",
      label: "SASL",
      tooltip: "设置为“开”以启用SASL身份验证",
      type: "on|off",
    },
    {
      name: "tls",
      label: "TLS",
      tooltip: "设置为“开”以启用TLS",
      type: "on|off",
    },
    {
      name: "tls_skip_verify",
      label: "TLS跳过验证",
      tooltip:
        '未经验证的信任服务器TLS，默认为“开”（验证）',
      type: "on|off",
    },
    {
      name: "client_tls_cert",
      label: "客户TLS证书",
      tooltip: "mTLS身份验证的客户端证书路径",
      type: "path",
      placeholder: "输入客户TLS证书",
    },
    {
      name: "client_tls_key",
      label: "客户端TLS密钥",
      tooltip: "mTLS身份验证的客户端密钥路径",
      type: "path",
      placeholder: "输入客户端TLS密钥",
    },
    {
      name: "version",
      label: "版本",
      tooltip: "指定卡夫卡群集的版本，例如“2.2.0”",
      type: "string",
      placeholder: "输入版本",
    },
    ...commonFields,
  ],
  [notifyAmqp]: [
    {
      name: "url",
      required: true,
      label: "URL",
      tooltip:
        "AMQP server endpoint e.g. `amqp://myuser:mypassword@localhost:5672`",
      type: "url",
    },
    {
      name: "exchange",
      label: "交换",
      tooltip: "AMQP交换的名称",
      type: "string",
      placeholder: "输入交换",
    },
    {
      name: "exchange_type",
      label: "交换类型",
      tooltip: "AMQP交换类型",
      type: "string",
      placeholder: "输入交换类型",
    },
    {
      name: "routing_key",
      label: "路由密钥",
      tooltip: "用于发布的路由密钥",
      type: "string",
      placeholder: "输入路由密钥",
    },
    {
      name: "mandatory",
      label: "强制性",
      tooltip:
        "当设置为“关闭”时，悄悄忽略未送达的邮件，默认设置为“打开”",
      type: "on|off",
    },
    {
      name: "durable",
      label: "持久",
      tooltip:
        "当设置为“开”时，代理上的持久队列将重新启动，默认设置为“关”",
      type: "on|off",
    },
    {
      name: "no_wait",
      label: "不等待",
      tooltip:
        "非阻塞消息传递设置为“开”时，默认为“关”",
      type: "on|off",
    },
    {
      name: "internal",
      label: "内部",
      tooltip:
        "设置为“开”，则exchange不会被发布者直接使用，但仅在绑定到其他exchange时使用",
      type: "on|off",
    },
    {
      name: "auto_deleted",
      label: "自动删除",
      tooltip:
        "当设置为“开启”时，在没有消费者时自动删除队列",
      type: "on|off",
    },
    {
      name: "delivery_mode",
      label: "配送模式",
      tooltip: "对于非持久队列，设置为“1”，对于持久队列，设置为“2”",
      type: "number",
      placeholder: "输入配送模式",
    },
    ...commonFields,
  ],
  [notifyRedis]: [
    {
      name: "address",
      required: true,
      label: "地址",
      tooltip: "Redis服务器的地址。例如：`localhost:6379`",
      type: "address",
      placeholder: "输入地址",
    },
    {
      name: "key",
      required: true,
      label: "Key",
      tooltip: "Redis键用于存储/更新事件，该键是自动创建的",
      type: "string",
      placeholder: "输入 Key",
    },
    {
      name: "password",
      label: "密码",
      tooltip: "Redis服务器密码",
      type: "string",
      placeholder: "输入密码",
    },
    ...commonFields,
  ],
  [notifyMqtt]: [
    {
      name: "broker",
      required: true,
      label: "经纪人",
      tooltip: "MQTT服务器节点，例如`tcp://localhost:1883`",
      type: "uri",
      placeholder: "输入经纪人",
    },
    {
      name: "topic",
      required: true,
      label: "话题",
      tooltip: "要发布的MQTT主题的名称",
      type: "string",
      placeholder: "输入话题",
    },
    {
      name: "username",
      label: "用户名",
      tooltip: "MQTT用户名",
      type: "string",
      placeholder: "输入用户名",
    },
    {
      name: "password",
      label: "密码",
      tooltip: "MQTT 密码",
      type: "string",
      placeholder: "输入MQTT 密码",
    },
    {
      name: "qos",
      label: "QOS",
      tooltip: "设置服务质量优先级，默认为“0”",
      type: "number",
      placeholder: "输入 QOS",
    },
    {
      name: "keep_alive_interval",
      label: "保持活动间隔",
      tooltip: "s、m、h、d中MQTT连接的保持活动间隔",
      type: "duration",
      placeholder: "输入保持活动间隔",
    },
    {
      name: "reconnect_interval",
      label: "重新连接间隔",
      tooltip: "s、m、h、d中MQTT连接的重新连接间隔",
      type: "duration",
      placeholder: "输入重新连接间隔",
    },
    ...commonFields,
  ],
  [notifyNats]: [
    {
      name: "address",
      required: true,
      label: "地址",
      tooltip: "NATS 地址 例子： '0.0.0.0:4222'",
      type: "address",
      placeholder: "输入地址",
    },
    {
      name: "subject",
      required: true,
      label: "主题",
      tooltip: "NATS 主题",
      type: "string",
      placeholder: "输入主题",
    },
    {
      name: "username",
      label: "用户名",
      tooltip: "NATS 用户名",
      type: "string",
      placeholder: "输入NATS 用户名",
    },
    {
      name: "password",
      label: "密码",
      tooltip: "NATS 密码",
      type: "string",
      placeholder: "输入NATS 密码",
    },
    {
      name: "token",
      label: "Token",
      tooltip: "NATS token",
      type: "string",
      placeholder: "输入NATS token",
    },
    {
      name: "tls",
      label: "TLS",
      tooltip: "设置 '开' 进入 TLS",
      type: "on|off",
    },
    {
      name: "tls_skip_verify",
      label: "TLS跳过验证",
      tooltip:
        '未经验证的信任服务器TLS，默认为“开”（验证）',
      type: "on|off",
    },
    {
      name: "ping_interval",
      label: "Ping间隔",
      tooltip: "客户端ping命令间隔为s、m、h、d。默认情况下禁用",
      type: "duration",
      placeholder: "输入Ping间隔",
    },
    {
      name: "streaming",
      label: "流动",
      tooltip: "设置为“开”，以使用流式NATS服务器",
      type: "on|off",
    },
    {
      name: "streaming_async",
      label: "流异步",
      tooltip: "设置为“开”，以启用异步发布",
      type: "on|off",
    },
    {
      name: "streaming_max_pub_acks_in_flight",
      label: "正在传输最大发布确认",
      tooltip: "无需等待确认即可发布的邮件数",
      type: "number",
      placeholder: "输入正在传输最大发布确认",
    },
    {
      name: "streaming_cluster_id",
      label: "流式集群ID",
      tooltip: "NATS流媒体群集的唯一ID",
      type: "string",
      placeholder: "输入流式集群ID",
    },
    {
      name: "cert_authority",
      label: "证书授权",
      tooltip: "目标NATS服务器的证书链的路径",
      type: "string",
      placeholder: "输入证书授权",
    },
    {
      name: "client_cert",
      label: "客户端证书",
      tooltip: "NATS mTLS身份验证的客户端证书",
      type: "string",
      placeholder: "输入客户端证书",
    },
    {
      name: "client_key",
      label: "客户端密钥",
      tooltip: "客户端密钥",
      type: "string",
      placeholder: "输入客户端密钥",
    },
    ...commonFields,
  ],
  [notifyElasticsearch]: [
    {
      name: "url",
      required: true,
      label: "URL",
      tooltip:
        "Elasticsearch服务器的地址，带有可选的身份验证信息",
      type: "url",
      placeholder: "输入 URL",
    },
    {
      name: "index",
      required: true,
      label: "首页",
      tooltip:
        "Elasticsearch索引用于存储/更新事件，索引是自动创建的",
      type: "string",
      placeholder: "输入首页",
    },
    {
      name: "format",
      required: true,
      label: "计划",
      tooltip:
        "“namespace”反映当前bucket/object列表，“access”反映对象操作日志，默认为“namespace”",
      type: "enum",
      placeholder: "输入计划",
    },
    ...commonFields,
  ],
  [notifyWebhooks]: [
    {
      name: "endpoint",
      required: true,
      label: "终点",
      tooltip:
        "webhook服务器节点",
      type: "url",
      placeholder: "输入终点",
    },
    {
      name: "auth_token",
      label: "身份验证令牌",
      tooltip: "JWT 身份验证令牌",
      type: "string",
      placeholder: "输入身份验证令牌",
    },
    ...commonFields,
  ],
  [notifyNsq]: [
    {
      name: "nsqd_address",
      required: true,
      label: "NSQD 地址",
      tooltip: "NSQ 地址，举例 '127.0.0.1:4150'",
      type: "address",
      placeholder: "输入NSQD 地址",
    },
    {
      name: "topic",
      required: true,
      label: "话题",
      tooltip: "NSQ 话题",
      type: "string",
      placeholder: "输入主题",
    },
    {
      name: "tls",
      label: "TLS",
      tooltip: "设置为“开”以启用TLS",
      type: "on|off",
    },
    {
      name: "tls_skip_verify",
      label: "TLS跳过验证",
      tooltip:
        '未经验证的信任服务器TLS，默认为“开”（验证）',
      type: "on|off",
    },
    ...commonFields,
  ],
};
