/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.dolphinscheduler.common.constants;

import java.time.Duration;
import java.util.regex.Pattern;

import lombok.experimental.UtilityClass;

@UtilityClass
public final class Constants {

    public static final String COMMON_PROPERTIES_PATH = "/common.properties";

    public static final String REMOTE_LOGGING_YAML_PATH = "/remote-logging.yaml";
    public static final String AWS_YAML_PATH = "/aws.yaml";
    public static final String RESOURCE_CENTER_YAML_PATH = "/resource-center.yaml";

    public static final String FORMAT_S_S = "%s/%s";
    public static final String FORMAT_S_S_COLON = "%s:%s";
    public static final String FOLDER_SEPARATOR = "/";

    public static final String EMPTY_STRING = "";

    /**
     * resource.hdfs.fs.defaultFS
     */
    public static final String FS_DEFAULT_FS = "resource.hdfs.fs.defaultFS";

    public static final String DATA_BASEDIR_PATH = "data.basedir.path";

    /**
     * resource.view.suffixs
     */
    public static final String RESOURCE_VIEW_SUFFIXES = "resource.view.suffixs";

    public static final String RESOURCE_VIEW_SUFFIXES_DEFAULT_VALUE =
            "txt,log,sh,bat,conf,cfg,py,java,sql,xml,hql,properties,json,yml,yaml,ini,js";

    public static final String DEVELOPMENT_STATE = "development.state";

    /**
     * shell.kill.wait.timeout: this property defines the wait timeout in seconds before using SIGKILL.
     */
    public static final String SHELL_KILL_WAIT_TIMEOUT = "shell.kill.wait.timeout";

    public static final String SUDO_ENABLE = "sudo.enable";

    public static final String RESOURCE_STORAGE_TYPE = "resource.storage.type";

    public static final String AWS_S3_BUCKET_NAME = "aws.s3.bucket.name";

    public static final String COMMA = ",";

    public static final String COLON = ":";

    public static final String SPACE = " ";

    public static final String SINGLE_SLASH = "/";

    public static final String DOUBLE_SLASH = "//";

    public static final String AT_SIGN = "@";

    public static final String SLASH = "/";

    public static final String SEMICOLON = ";";

    public static final String IMPORT_SUFFIX = "_import_";

    public static final String COPY_SUFFIX = "_copy_";
    public static final int HTTP_CONNECT_TIMEOUT = 60 * 1000;

    /**
     * http connect request time out
     */
    public static final int HTTP_CONNECTION_REQUEST_TIMEOUT = 60 * 1000;

    /**
     * httpclient socket time out
     */
    public static final int SOCKET_TIMEOUT = 60 * 1000;

    /**
     * http header
     */
    public static final String HTTP_HEADER_UNKNOWN = "unKnown";

    public static final String HTTP_X_FORWARDED_FOR = "X-Forwarded-For";

    public static final String HTTP_X_REAL_IP = "X-Real-IP";

    /**
     * user name regex
     */
    public static final Pattern REGEX_USER_NAME = Pattern.compile("^[a-zA-Z0-9._-]{3,39}$");

    public static final int READ_PERMISSION = 2;

    public static final int WRITE_PERMISSION = 2 * 2;

    public static final int EXECUTE_PERMISSION = 1;

    public static final int DEFAULT_ADMIN_PERMISSION = 7;

    public static final int DEFAULT_HASH_MAP_SIZE = 16;

    public static final int ALL_PERMISSIONS = READ_PERMISSION | WRITE_PERMISSION | EXECUTE_PERMISSION;

    /**
     * forbid running task
     */
    public static final String FLOWNODE_RUN_FLAG_FORBIDDEN = "FORBIDDEN";

    /**
     * normal running task
     */
    public static final String FLOWNODE_RUN_FLAG_NORMAL = "NORMAL";

    public static final String DEFAULT = "default";
    public static final String PASSWORD = "password";
    public static final String XXXXXX = "******";
    public static final String NULL = "NULL";
    public static final String THREAD_NAME_MASTER_SERVER = "Master-Server";
    public static final String THREAD_NAME_WORKER_SERVER = "Worker-Server";
    public static final String THREAD_NAME_ALERT_SERVER = "Alert-Server";

    /**
     * complement date default cron string
     */
    public static final String DEFAULT_CRON_STRING = "0 0 0 * * ? *";

    /**
     * sleep 1000ms
     */
    public static final long SLEEP_TIME_MILLIS = 1_000L;

    public static final Duration SERVER_CLOSE_WAIT_TIME = Duration.ofSeconds(3);

    public static final int EXIT_CODE_SUCCESS = 0;

    public static final int EXIT_CODE_FAILURE = -1;

    /**
     * workflow or task definition failure
     */
    public static final int DEFINITION_FAILURE = -1;

    /**
     * workflow or task definition first version
     */
    public static final int VERSION_FIRST = 1;

    public static final String UNDERLINE = "_";

    public static final String GLOBAL_PARAMS = "globalParams";
    public static final String LOCAL_PARAMS = "localParams";
    public static final String SUBWORKFLOW_INSTANCE_ID = "subWorkflowInstanceId";
    public static final String WORKFLOW_INSTANCE_STATE = "workflowInstanceState";
    public static final String PARENT_WORKFLOW_INSTANCE = "parentWorkflowInstance";
    public static final String TASK_LIST = "taskList";
    public static final String QUEUE = "queue";
    public static final String QUEUE_NAME = "queueName";

    /**
     * dependent task
     */
    public static final long DEPENDENT_ALL_TASK_CODE = -1;
    public static final long DEPENDENT_WORKFLOW_CODE = 0;

    public static final int PREVIEW_SCHEDULE_EXECUTE_COUNT = 5;

    public static final String KERBEROS = "kerberos";

    public static final String JAVA_SECURITY_KRB5_CONF = "java.security.krb5.conf";

    public static final String JAVA_SECURITY_KRB5_CONF_PATH = "java.security.krb5.conf.path";

    public static final String HADOOP_SECURITY_AUTHENTICATION = "hadoop.security.authentication";

    /**
     * hadoop.security.authentication
     */
    public static final String HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE =
            "hadoop.security.authentication.startup.state";

    /**
     * loginUserFromKeytab user
     */
    public static final String LOGIN_USER_KEY_TAB_USERNAME = "login.user.keytab.username";

    /**
     * loginUserFromKeytab path
     */
    public static final String LOGIN_USER_KEY_TAB_PATH = "login.user.keytab.path";

    public static final String SSO_LOGIN_USER_STATE = "sso.login.user.state";

    public static final String STATUS = "status";

    public static final String MSG = "msg";

    public static final String PAGE_SIZE = "pageSize";

    /**
     * current page no
     */
    public static final String PAGE_NUMBER = "pageNo";

    /**
     *
     */
    public static final String DATA_LIST = "data";

    public static final String TOTAL_LIST = "totalList";

    public static final String CURRENT_PAGE = "currentPage";

    public static final String TOTAL_PAGE = "totalPage";

    public static final String TOTAL = "total";

    public static final long DEFAULT_PROJECT_CODE = 0;

    public static final long DEPENDENT_ALL_TASK = 0;

    public static final String SESSION_USER = "session.user";

    public static final String SESSION_ID = "sessionId";

    /**
     * locale
     */
    public static final String LOCALE_LANGUAGE = "language";

    public static final int SESSION_TIME_OUT = 7200;

    public static final int AUTHORIZE_WRITABLE_PERM = 7;

    public static final String START_TIME = "start time";
    public static final String END_TIME = "end time";
    public static final String START_END_DATE = "startDate,endDate";

    public static final String SYSTEM_LINE_SEPARATOR = System.getProperty("line.separator");

    /**
     * exec shell scripts
     */
    public static final String SH = "sh";

    /**
     * use for k8s
     */
    public static final String NAMESPACE = "namespace";
    public static final String CLUSTER = "cluster";
    public static final Long K8S_LOCAL_TEST_CLUSTER_CODE = 0L;

    public static final String SCHEDULE_TIMEZONE = "schedule_timezone";

    /**
     * tenant
     */
    public static final int TENANT_FULL_NAME_MAX_LENGTH = 30;

    /**
     * password max and min LENGTH
     */
    public static final int USER_PASSWORD_MAX_LENGTH = 20;

    public static final int USER_PASSWORD_MIN_LENGTH = 2;

    public static final String FUNCTION_START_WITH = "$";

    public static final Integer DEFAULT_QUEUE_ID = 1;

    /**
     * Security authentication types (supported types: PASSWORD,LDAP)
     */
    public static final String SECURITY_CONFIG_TYPE = "securityConfigType";

    public static final String SECURITY_CONFIG_TYPE_PASSWORD = "PASSWORD";

    public static final String SECURITY_CONFIG_TYPE_LDAP = "LDAP";

    /**
     * Task Types
     */
    public static final String TYPE_UNIVERSAL = "Universal";
    public static final String TYPE_DATA_INTEGRATION = "DataIntegration";
    public static final String TYPE_CLOUD = "Cloud";
    public static final String TYPE_LOGIC = "Logic";
    public static final String TYPE_OTHER = "Other";
    public static final String TYPE_MACHINE_LEARNING = "MachineLearning";

    /**
     * spi constants
     */
    /** alert plugin param field string **/
    public static final String STRING_PLUGIN_PARAM_FIELD = "field";
    /** alert plugin param name string **/
    public static final String STRING_PLUGIN_PARAM_NAME = "name";
    /** alert plugin param props string **/
    public static final String STRING_PLUGIN_PARAM_PROPS = "props";
    /** alert plugin param type string **/
    public static final String STRING_PLUGIN_PARAM_TYPE = "type";
    /** alert plugin param title string **/
    public static final String STRING_PLUGIN_PARAM_TITLE = "title";
    /** alert plugin param value string **/
    public static final String STRING_PLUGIN_PARAM_VALUE = "value";
    /** alert plugin param validate string **/
    public static final String STRING_PLUGIN_PARAM_VALIDATE = "validate";
    /** alert plugin param options string **/
    public static final String STRING_PLUGIN_PARAM_OPTIONS = "options";
    /**plugin param emit string **/
    public static final String STRING_PLUGIN_PARAM_EMIT = "emit";

    /** string true */
    public static final String STRING_TRUE = "true";
    /** string false */
    public static final String STRING_FALSE = "false";
    /** string yes */
    public static final String STRING_YES = "YES";
    /** string no */
    public static final String STRING_NO = "NO";

    public static final String REMOTE_LOGGING_ENABLE = "remote.logging.enable";

    public static final String REMOTE_LOGGING_TARGET = "remote.logging.target";

    public static final String REMOTE_LOGGING_BASE_DIR = "remote.logging.base.dir";

    /**
     * remote logging for OSS
     */
    public static final String REMOTE_LOGGING_OSS_ACCESS_KEY_ID = "remote.logging.oss.access.key.id";

    public static final String REMOTE_LOGGING_OSS_ACCESS_KEY_SECRET = "remote.logging.oss.access.key.secret";

    public static final String REMOTE_LOGGING_OSS_BUCKET_NAME = "remote.logging.oss.bucket.name";

    public static final String REMOTE_LOGGING_OSS_ENDPOINT = "remote.logging.oss.endpoint";

    /**
     * remote logging for GCS
     */
    public static final String REMOTE_LOGGING_GCS_CREDENTIAL = "remote.logging.google.cloud.storage.credential";

    public static final String REMOTE_LOGGING_GCS_BUCKET_NAME = "remote.logging.google.cloud.storage.bucket.name";

    /**
     * remote logging for ABS
     */
    public static final String REMOTE_LOGGING_ABS_ACCOUNT_NAME = "remote.logging.abs.account.name";
    public static final String REMOTE_LOGGING_ABS_ACCOUNT_KEY = "remote.logging.abs.account.key";
    public static final String REMOTE_LOGGING_ABS_CONTAINER_NAME = "remote.logging.abs.container.name";

    /**
     * remote logging for COS
     */
    public static final String REMOTE_LOGGING_COS_ACCESS_KEY_ID = "remote.logging.cos.access.key.id";

    public static final String REMOTE_LOGGING_COS_ACCESS_KEY_SECRET = "remote.logging.cos.access.key.secret";

    public static final String REMOTE_LOGGING_COS_BUCKET_NAME = "remote.logging.cos.bucket.name";

    public static final String REMOTE_LOGGING_COS_REGION = "remote.logging.cos.region";

    public static final String DATABASES_QUERY = "show databases";
    public static final String DATABASES_QUERY_PG = "SELECT datname FROM pg_database";

    /**
     * K8S sensitive param
     */
    public static final String K8S_CONFIG_REGEX = "(?<=((?i)configYaml(\" : \"))).*?(?=(\",\\n))";

    public static final String RELEASE_STATE = "releaseState";
    public static final String EXECUTE_TYPE = "executeType";

}
