package com.lanyuanxiaoyao.service.common.utils;

import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.common.Constants;

/**
 * 命名相关工具
 *
 * @author ZhangJiacheng
 * @date 2022-06-24
 */
public class NameHelper {
    public static String pulsarSubscriptionName(Long flinkJobId, String alias) {
        return Constants.PULSAR_SUBSCRIPTION_NAME_PREFIX + "_" + flinkJobId + "_" + alias + "_20230425";
    }

    // Sync job name

    public static final String SYNC_JOB_NAME_REGEX = "^Sync_(\\d+?)_(.+)$";

    public static boolean isSyncJob(String name) {
        return StrUtil.isNotBlank(name) && name.matches(SYNC_JOB_NAME_REGEX);
    }

    public static String syncJobName(Long flinkJobId, String flinkJobName) {
        return "Sync_" + flinkJobId + "_" + flinkJobName;
    }

    public static final String COMPACTION_JOB_NAME_REGEX = "^Compaction_(\\d+?)_(.+?)$";

    public static boolean isCompactionJob(String name) {
        return StrUtil.isNotBlank(name) && name.matches(COMPACTION_JOB_NAME_REGEX);
    }

    public static String compactionJobName(Long flinkJobId, String alias) {
        return "Compaction_" + flinkJobId + "_" + alias;
    }

    // flink job name

    public static String syncFlinkName(Long flinkJobId, String flinkJobName) {
        return flinkJobName + " (ID: " + flinkJobId + ")";
    }

    public static String syncFlinkName(Long flinkJobId, String flinkJobName, String alias) {
        return flinkJobName + " " + alias + " (ID: " + flinkJobId + ")";
    }

    public static String compactionFlinkName(Long flinkJobId, String schema, String alias) {
        return schema + " " + alias + " (ID: " + flinkJobId + ")";
    }

    // sync state name

    public static String syncStateName(Long flinkJobId, String alias) {
        return flinkJobId + "-" + alias;
    }

    // zk lock name
    public static final String ZK_ROOT_PATH = "/hudi";
    public static final String ZK_LOCK_PATH = ZK_ROOT_PATH + "/lock";
    public static final String ZK_LAUNCHER_LOCK_PATH = ZK_LOCK_PATH + "/launcher";
    public static final String ZK_RUNNING_LOCK_PATH = ZK_LOCK_PATH + "/running";

    public static final String ZK_SYNC_SUFFIX_PATH = "/sync";
    public static final String ZK_SYNC_LAUNCHER_LOCK_PATH = ZK_LAUNCHER_LOCK_PATH + ZK_SYNC_SUFFIX_PATH;
    public static final String ZK_SYNC_RUNNING_LOCK_PATH = ZK_RUNNING_LOCK_PATH + ZK_SYNC_SUFFIX_PATH;

    public static String syncLockName(Long flinkJobId, String alias) {
        if (StrUtil.isNotBlank(alias)) {
            return "sync_lock_" + flinkJobId + "_" + alias;
        }
        return "sync_lock_" + flinkJobId;
    }

    public static String syncLauncherLockPath(Long flinkJobId) {
        return ZK_SYNC_LAUNCHER_LOCK_PATH + "/" + syncLockName(flinkJobId, null);
    }

    public static String syncRunningLockPath(Long flinkJobId) {
        return syncRunningLockPath(flinkJobId, null);
    }

    public static String syncRunningLockPath(Long flinkJobId, String alias) {
        return ZK_SYNC_RUNNING_LOCK_PATH + "/" + syncLockName(flinkJobId, alias);
    }

    public static final String ZK_COMPACTION_SUFFIX_PATH = "/compaction";
    public static final String ZK_COMPACTION_LAUNCHER_LOCK_PATH = ZK_LAUNCHER_LOCK_PATH + ZK_COMPACTION_SUFFIX_PATH;
    public static final String ZK_COMPACTION_RUNNING_LOCK_PATH = ZK_RUNNING_LOCK_PATH + ZK_COMPACTION_SUFFIX_PATH;

    public static String compactionLockName(Long flinkJobId, String alias) {
        return "compaction_lock_" + flinkJobId + "_" + alias;
    }

    public static String compactionLauncherLockPath(Long flinkJobId, String alias) {
        return ZK_COMPACTION_LAUNCHER_LOCK_PATH + "/" + compactionLockName(flinkJobId, alias);
    }

    public static String compactionRunningLockPath(Long flinkJobId, String alias) {
        return ZK_COMPACTION_RUNNING_LOCK_PATH + "/" + compactionLockName(flinkJobId, alias);
    }
}
