package com.lanyuanxiaoyao.service.sync.utils;

import com.lanyuanxiaoyao.service.common.Constants;
import com.lanyuanxiaoyao.service.common.entity.FlinkJob;
import com.lanyuanxiaoyao.service.common.entity.TableMeta;
import com.lanyuanxiaoyao.service.common.exception.MissingArgumentException;
import java.util.List;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.type.TypeReference;

/**
 * 入参解析相关内容
 *
 * @author ZhangJiacheng
 * @date 2022-03-10
 */
public class ArgumentsUtils {
    public static long getJobId(String[] args) throws MissingArgumentException {
        ParameterTool argsTool = ParameterTool.fromArgs(args);
        if (!argsTool.has(Constants.JOB_ID)) {
            throw new MissingArgumentException(Constants.JOB_ID);
        }
        return argsTool.getLong(Constants.JOB_ID);
    }

    public static String getTable(String[] args) throws MissingArgumentException {
        ParameterTool argsTool = ParameterTool.fromArgs(args);
        if (!argsTool.has(Constants.TABLE_NAME)) {
            throw new MissingArgumentException(Constants.TABLE_NAME);
        }
        return argsTool.get(Constants.TABLE_NAME);
    }

    public static Boolean getServiceMode(String[] args) throws Exception {
        ParameterTool argsTool = ParameterTool.fromArgs(args);
        if (!argsTool.has(Constants.SERVICE_MODE)) {
            return false;
        }
        return argsTool.getBoolean(Constants.SERVICE_MODE);
    }

    public static String getMessageId(String[] args) throws MissingArgumentException {
        ParameterTool argsTool = ParameterTool.fromArgs(args);
        if (!argsTool.has(Constants.MESSAGE_ID)) {
            throw new MissingArgumentException(Constants.MESSAGE_ID);
        }
        return argsTool.get(Constants.MESSAGE_ID);
    }

    public static TableMeta getTableMeta(String[] args) throws Exception {
        ParameterTool argsTool = ParameterTool.fromArgs(args);
        if (!argsTool.has(Constants.TABLE_META)) {
            throw new MissingArgumentException(Constants.TABLE_META);
        }
        return JacksonUtils.getMapper().readValue(argsTool.get(Constants.TABLE_META), TableMeta.class);
    }

    public static List<TableMeta> getTableMetaList(String[] args) throws Exception {
        ParameterTool argsTool = ParameterTool.fromArgs(args);
        if (!argsTool.has(Constants.TABLE_META_LIST)) {
            throw new MissingArgumentException(Constants.TABLE_META_LIST);
        }
        return JacksonUtils.getMapper().readValue(argsTool.get(Constants.TABLE_META_LIST), new TypeReference<List<TableMeta>>() {});
    }

    public static FlinkJob getFlinkJob(String[] args) throws MissingArgumentException, JsonProcessingException {
        ParameterTool argsTool = ParameterTool.fromArgs(args);
        if (!argsTool.has(Constants.FLINK_JOB)) {
            throw new MissingArgumentException(Constants.FLINK_JOB);
        }
        return JacksonUtils.getMapper().readValue(argsTool.get(Constants.FLINK_JOB), FlinkJob.class);
    }

    public static String getInstants(String[] args) throws JsonProcessingException {
        ParameterTool argsTool = ParameterTool.fromArgs(args);
        if (!argsTool.has(Constants.INSTANTS)) {
            return "";
        }
        return argsTool.get(Constants.INSTANTS);
    }

    public static Boolean getBetaMode(String[] args) {
        ParameterTool argsTool = ParameterTool.fromArgs(args);
        if (!argsTool.has(Constants.BETA)) {
            return false;
        }
        return argsTool.getBoolean(Constants.BETA);
    }

    public static String getCluster(String[] args) {
        ParameterTool argsTool = ParameterTool.fromArgs(args);
        if (!argsTool.has(Constants.CLUSTER)) {
            return "";
        }
        return argsTool.get(Constants.CLUSTER);
    }
}
