import {RouteType} from "../enums/RouteType"
import StringUtils from "../utils/StringUtils";

/**
 * 微服务客户端配置
 * @author WuFengLin
 */
export class GatewayConfig {

    /**
     * 网关服务地址
     */
    public hostAddress: string = 'https://mms-service.keytop.cn/';

    /**
     * 微服务应用ID
     */
    public appId: string = "";

    /**
     * appId在Header中的变量名称
     */
    public headerVariableName: string = "appId";


    /**
     * 微服务 Key
     */
    public appKey: string = "";

    /**
     * 环境代码
     */
    public envCode: string = "";

    /**
     * 路由类型，默认 GATE_WAY
     */
    public routeType: RouteType = RouteType.GATE_WAY;

    private checkOk = false;

    static make() {
        let result = new GatewayConfig();
        let userId = process.env['KT_ME_USER_ID'] || '';
        let userKey = process.env['KT_ME_USER_KEY'] || '';
        let envCode = process.env['KT_ME_ENV'] || '';
        let envKey = process.env['KT_ME_ENV_KEY'] || '';

        if (StringUtils.isNotEmpty(userId)) {
            result.appId = userId;
            result.appKey = userKey;
            result.headerVariableName = 'userId';
            result.envCode = "DEV";
        } else if (StringUtils.isNotEmpty(envCode)) {
            result.appId = envCode;
            result.envCode = envCode;
            result.appKey = envKey;
            result.headerVariableName = 'envCode';
        }
        return result;
    }


    check() {
        if (this.checkOk) {
            return;
        }
        if (StringUtils.isEmpty(this.hostAddress)) {
            throw new Error("MsClient initialization failed：'hostAddress' Parameter not configured!")
        }
        if (StringUtils.isEmpty(this.appId)) {
            throw new Error("MsClient initialization failed：'appId' Parameter not configured!")
        }
        if (StringUtils.isEmpty(this.appKey)) {
            throw new Error("MsClient initialization failed：'appKey' Parameter not configured!")
        }
    }
}