// 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 React from "react";
import PublicIcon from "@mui/icons-material/Public";
import SdStorageIcon from "@mui/icons-material/SdStorage";
import CompressIcon from "@mui/icons-material/Compress";
import CodeIcon from "@mui/icons-material/Code";
import LocalHospitalIcon from "@mui/icons-material/LocalHospital";
import FindReplaceIcon from "@mui/icons-material/FindReplace";
import VpnKeyIcon from "@mui/icons-material/VpnKey";
import LockOpenIcon from "@mui/icons-material/LockOpen";
import LoginIcon from "@mui/icons-material/Login";
import PendingActionsIcon from "@mui/icons-material/PendingActions";
import CallToActionIcon from "@mui/icons-material/CallToAction";
import { IElement, IElementValue } from "./types";

export const configurationElements: IElement[] = [
  {
    icon: <PublicIcon />,
    configuration_id: "region",
    configuration_label: "服务器位置",
  },
  {
    icon: <SdStorageIcon />,
    configuration_id: "cache",
    configuration_label: "缓存",
  },
  {
    icon: <CompressIcon />,
    configuration_id: "compression",
    configuration_label: "压缩",
  },
  {
    icon: <CodeIcon />,
    configuration_id: "api",
    configuration_label: "API",
  },
  {
    icon: <LocalHospitalIcon />,
    configuration_id: "heal",
    configuration_label: "修复",
  },
  {
    icon: <FindReplaceIcon />,
    configuration_id: "scanner",
    configuration_label: "扫描",
  },
  {
    icon: <VpnKeyIcon />,
    configuration_id: "etcd",
    configuration_label: "Etcd",
  },
  {
    icon: <LockOpenIcon />,
    configuration_id: "identity_openid",
    configuration_label: "身份Openid",
  },
  {
    icon: <LoginIcon />,
    configuration_id: "identity_ldap",
    configuration_label: "身份LDAP",
  },
  {
    icon: <CallToActionIcon />,
    configuration_id: "logger_webhook",
    configuration_label: "日志记录器Webhook",
  },
  {
    icon: <PendingActionsIcon />,
    configuration_id: "audit_webhook",
    configuration_label: "审查Webhook",
  },
];

export const fieldsConfigurations: any = {
  region: [
    {
      name: "name",
      required: true,
      label: "服务器位置",
      tooltip: '服务器位置的名称，例如“us-west-rack2”',
      type: "string",
      placeholder: "举例 us-west-rack-2",
    },
    {
      name: "comment",
      required: false,
      label: "备注",
      tooltip: "您可以在此设置中添加备注",
      type: "comment",
      placeholder: "输入备注",
    },
  ],
  cache: [
    {
      name: "drives",
      required: true,
      label: "挂载点",
      tooltip:
        '挂载点，例如“/optane1”或“/optane2”，每个字段可以写入一个装入点',
      type: "csv",
      placeholder: "输入挂载点",
    },
    {
      name: "expiry",
      required: false,
      label: "到期时间",
      tooltip: '缓存到期时间（以天为单位），例如“90”',
      type: "number",
      placeholder: "输入到期",
    },
    {
      name: "quota",
      required: false,
      label: "定额",
      tooltip: '限制缓存磁盘的使用百分比，例如“90”',
      type: "number",
      placeholder: "输入 in %",
    },
    {
      name: "exclude",
      required: false,
      label: "通配符",
      tooltip:
        '通配符通配符模式，例如“bucket/*.tmp”或“*.exe”，您可以为每个字段写入一个',
      type: "csv",
      placeholder: "输入通配符通配符规则",
    },
    {
      name: "after",
      required: false,
      label: "之后",
      tooltip: "缓存对象之前的最小访问次数",
      type: "number",
      placeholder: "输入最小访问次数",
    },
    {
      name: "watermark_low",
      required: false,
      label: "最小值",
      tooltip: "最小值",
      type: "number",
      placeholder: "输入最小值",
    },
    {
      name: "watermark_high",
      required: false,
      label: "最大值",
      tooltip: "最大值",
      type: "number",
      placeholder: "输入最大值",
    },
    {
      name: "comment",
      required: false,
      label: "备注",
      tooltip: "您可以在此设置中添加备注",
      type: "comment",
      multiline: true,
      placeholder: "输入备注",
    },
  ],
  compression: [
    {
      name: "extensions",
      required: false,
      label: "扩展名",
      tooltip:
        '要压缩的扩展名，例如“.txt”、“.log”或“.csv”，您可以为每个字段编写一个扩展名',
      type: "csv",
      placeholder: "输入扩展名",
      withBorder: true,
    },
    {
      name: "mime_types",
      required: false,
      label: "mime类型",
      tooltip:
        'Mime类型，例如“text/*”、“application/json”或“application/xml”，您可以为每个字段编写一个',
      type: "csv",
      placeholder: "输入主要类型",
      withBorder: true,
    },
  ],
  api: [
    {
      name: "requests_max",
      required: false,
      label: "请求最大值",
      tooltip: "最大并发请求数，例如“1600”",
      type: "number",
      placeholder: "输入请求最大值",
    },
    {
      name: "cors_allow_origin",
      required: false,
      label: "Cors允许来源",
      tooltip: "CORS请求允许的来源列表",
      type: "csv",
      placeholder: "输入允许来源 举例： https://example.com",
    },
    {
      name: "replication_workers",
      required: false,
      label: "复制任务数",
      tooltip: "复制任务数人员的数量，默认为100",
      type: "number",
      placeholder: "输入复制任务数",
    },
    {
      name: "replication_failed_workers",
      required: false,
      label: "复制失败任务数",
      tooltip:
        "最近失败的复制副本的复制任务数进程数，默认为4",
      type: "number",
      placeholder: "输入复制失败任务数",
    },
  ],
  heal: [
    {
      name: "bitrotscan",
      required: false,
      label: "Bitrot 扫描",
      tooltip:
        "在扫描过程中检查对象时，对磁盘执行bitrot扫描",
      type: "开|关",
    },
    {
      name: "max_sleep",
      required: false,
      label: "最大睡眠",
      tooltip:
        "对象之间的最长睡眠时间，以减缓修复操作。例2",
      type: "duration",
      placeholder: "输入最大睡眠",
    },
    {
      name: "max_io",
      required: false,
      label: "最大IO",
      tooltip:
        "对象之间允许的最大IO请求，以减缓修复操作。例3",
      type: "number",
      placeholder: "输入最大IO",
    },
  ],
  scanner: [
    {
      name: "delay",
      required: false,
      label: "延迟倍数",
      tooltip: "扫描延迟倍增器，默认为“10.0”",
      type: "number",
      placeholder: "输入延迟倍数",
    },
    {
      name: "max_wait",
      required: false,
      label: "最大等待时间",
      tooltip: "操作之间的最大等待时间，默认为“15秒”",
      type: "duration",
      placeholder: "输入最大等待时间",
    },
    {
      name: "cycle",
      required: false,
      label: "循环",
      tooltip: "扫描周期之间的持续时间，默认为“1m”",
      type: "duration",
      placeholder: "输入循环",
    },
  ],
  etcd: [
    {
      name: "endpoints",
      required: true,
      label: "节点",
      tooltip:
        'etcd节点列表，例如“http://localhost:2379“，每个字段可以写一个',
      type: "csv",
      placeholder: "输入挂载点",
    },
    {
      name: "path_prefix",
      required: false,
      label: "路径前缀",
      tooltip: '用于隔离租户的命名空间前缀，例如“customer1/”',
      type: "string",
      placeholder: "输入路径前缀",
    },
    {
      name: "coredns_path",
      required: false,
      label: "Coredns路径",
      tooltip: '共享bucket DNS记录，默认为“/skydns”',
      type: "string",
      placeholder: "输入Coredns路径",
    },
    {
      name: "client_cert",
      required: false,
      label: "客户端证书",
      tooltip: "用于mTLS身份验证的客户端证书",
      type: "string",
      placeholder: "输入客户端证书",
    },
    {
      name: "client_cert_key",
      required: false,
      label: "客户端证书密钥",
      tooltip: "用于mTLS身份验证的客户端证书密钥",
      type: "string",
      placeholder: "输入客户端证书密钥",
    },
    {
      name: "comment",
      required: false,
      label: "备注",
      tooltip: "您可以在此设置中添加备注",
      type: "comment",
      multiline: true,
      placeholder: "输入备注",
    },
  ],
  identity_openid: [
    {
      name: "config_url",
      required: false,
      label: "配置URL",
      tooltip: "身份提供程序配置的配置URL",
      type: "string",
      placeholder:
        "https://identity-provider-url/.well-known/openid-configuration",
    },
    {
      name: "client_id",
      required: false,
      label: "客户端ID",
      type: "string",
      placeholder: "输入客户端ID",
    },
    {
      name: "client_secret",
      required: false,
      label: "密钥ID",
      type: "string",
      placeholder: "输入密钥ID",
    },
    {
      name: "claim_name",
      required: false,
      label: "断言名称",
      type: "string",
      placeholder: "输入断言名称",
    },
    {
      name: "claim_prefix",
      required: false,
      label: "断言前缀",
      tooltip: "前缀",
      type: "string",
      placeholder: "输入断言前缀",
    },
    {
      name: "claim_userinfo",
      required: false,
      label: "断言用户信息",
      type: "开|关",
    },
    {
      name: "redirect_uri",
      required: false,
      label: "重定向URL",
      type: "string",
      placeholder: "https://console-endpoint-url/oauth_callback",
    },
    {
      name: "scopes",
      required: false,
      label: "范围",
      type: "string",
      placeholder: "openid、个人资料、电子邮件",
    },
  ],
  identity_ldap: [
    {
      name: "server_addr",
      required: true,
      label: "服务器地址",
      tooltip: 'AD/LDAP服务器地址，例如“myldapserver.com:636”',
      type: "string",
      placeholder: "输入服务器地址",
    },
    {
      name: "username_format",
      required: true,
      label: "用户名格式",
      tooltip:
        '用户名绑定DNs列表，例如“uid=%s”、“cn=accounts”、“dc=myldapserver”或“dc=com”，每个字段可以写一个',
      type: "csv",
      placeholder: "输入用户名格式",
    },
    {
      name: "username_search_filter",
      required: true,
      label: "用户名搜索过滤器",
      tooltip:
        '用户搜索筛选器，例如“（cn=%s）”或“（sAMAccountName=%s）”或“（uid=%s）”',
      type: "string",
      placeholder: "输入用户名搜索过滤器",
    },
    {
      name: "group_search_filter",
      required: true,
      label: "组搜索过滤器",
      tooltip:
        '搜索组筛选器，例如“（&（objectclass=groupOfNames）（memberUid=%s））”',
      type: "string",
      placeholder: "输入组搜索过滤器",
    },
    {
      name: "username_search_base_dn",
      required: false,
      label: "用户名搜索基础 DN",
      tooltip: "用户名搜索DNs列表，每个字段可以写一个",
      type: "csv",
      placeholder: "输入用户名搜索基础 DN",
    },
    {
      name: "group_name_attribute",
      required: false,
      label: "组名属性",
      tooltip: '搜索组名属性，例如“cn”',
      type: "string",
      placeholder: "输入组名属性",
    },
    {
      name: "sts_expiry",
      required: false,
      label: "STS 到期时间",
      tooltip:
        '临时凭证的有效期为s、m、h、d。默认值为“1h”',
      type: "string",
      placeholder: "输入STS 到期时间",
    },
    {
      name: "tls_skip_verify",
      required: false,
      label: "TLS 跳过验证",
      tooltip:
        '未经验证的信任服务器TLS，默认为“关闭”（验证）',
      type: "开|关",
    },
    {
      name: "server_insecure",
      required: false,
      label: "服务器不安全",
      tooltip:
        '允许纯文本连接到AD/LDAP服务器，默认为“关闭”',
      type: "开|关",
    },
    {
      name: "comment",
      required: false,
      label: "备注",
      tooltip: "向该设置添加备注",
      type: "comment",
      placeholder: "输入备注",
    },
  ],
  logger_webhook: [
    {
      name: "endpoint",
      required: true,
      label: "节点",
      type: "string",
      placeholder: "输入节点",
    },
    {
      name: "auth_token",
      required: true,
      label: "身份验证令牌",
      type: "string",
      placeholder: "输入身份验证令牌",
    },
  ],
  audit_webhook: [
    {
      name: "endpoint",
      required: true,
      label: "节点",
      type: "string",
      placeholder: "输入节点",
    },
    {
      name: "auth_token",
      required: true,
      label: "身份验证令牌",
      type: "string",
      placeholder: "输入身份验证令牌",
    },
  ],
};

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

  return nonEmptyFields;
};

export const selectSAs = (
  e: React.ChangeEvent<HTMLInputElement>,
  setSelectedSAs: Function,
  selectedSAs: string[]
) => {
  const targetD = e.target;
  const value = targetD.value;
  const checked = targetD.checked;

  let elements: string[] = [...selectedSAs]; // We clone the selectedSAs array
  if (checked) {
    // If the user has checked this field we need to push this to selectedSAs
    elements.push(value);
  } else {
    // User has unchecked this field, we need to remove it from the list
    elements = elements.filter((element) => element !== value);
  }
  setSelectedSAs(elements);
  return elements;
};
