package com.winsea.svc.common.config;

import com.yh.saas.common.support.util.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;

import java.util.HashMap;
import java.util.Map;

/**
 * 所有服务的共通配置
 *
 * @author SunTianjun
 */
public class CommonConfig implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        String active = environment.getProperty("spring.profiles.active");
        if (StringUtils.isEmpty(active)) {
            return;
        }

        Map<String, Object> defaultConfig = new HashMap<>();
        defaultConfig.putAll(getJacksonConfig());
        defaultConfig.putAll(getUnnecessaryConfig());
        defaultConfig.putAll(getDefaultWhitelistConfig());
        defaultConfig.putAll(getDefaultPushConfig());
        defaultConfig.putAll(getDefaultOssConfig(active));
        defaultConfig.putAll(getDefaultMybatisPlusConfig());

        environment.getPropertySources().addLast(new MapPropertySource("defaultProperties", defaultConfig));
    }

    /**
     * 获取Jackson配置
     */
    private Map<String, Object> getJacksonConfig() {
        Map<String, Object> jacksonConfig = new HashMap<>();
        jacksonConfig.put("spring.jackson.date-format", "yyyy-MM-dd HH:mm:ss");
        jacksonConfig.put("spring.jackson.time-zone", "GMT+8");
        return jacksonConfig;
    }

    /**
     * 获取不是每个服务都必要的配置
     */
    private Map<String, Object> getUnnecessaryConfig() {
        Map<String, Object> unnecessaryConfig = new HashMap<>();
        unnecessaryConfig.put("front-origin", "");
        unnecessaryConfig.put("file-root-path", "");
        unnecessaryConfig.put("system.temp-file-path", "");
        unnecessaryConfig.put("po.password", "");
        unnecessaryConfig.put("po.syspath", "");
        return unnecessaryConfig;
    }

    /**
     * 获取默认白名单配置
     */
    private Map<String, Object> getDefaultWhitelistConfig() {
        Map<String, Object> whitelistConfig = new HashMap<>();
        whitelistConfig.put("auth.anon-urls[0]", "/auth/api/login");
        whitelistConfig.put("auth.anon-urls[1]", "/auth/api/loginEnhanced");
        whitelistConfig.put("auth.anon-urls[2]", "/auth/api/loginQuickly");
        whitelistConfig.put("auth.anon-urls[3]", "/auth/api/checkPassword");
        whitelistConfig.put("auth.anon-urls[4]", "/auth/api/retrievePassword");
        whitelistConfig.put("auth.anon-urls[5]", "/veriCode/api/send");
        whitelistConfig.put("auth.anon-urls[6]", "/veriCode/api/check");
        whitelistConfig.put("auth.anon-urls[7]", "/company/query/companyInfoByDomainName");
        whitelistConfig.put("auth.anon-urls[8]", "/webservice/**/*");
        whitelistConfig.put("auth.anon-urls[9]", "/workflowSync/api/startFlow");
        whitelistConfig.put("auth.anon-urls[10]", "/office/*");
        whitelistConfig.put("auth.anon-urls[11]", "/auth/api/phoneLogin");
        whitelistConfig.put("auth.anon-urls[12]", "/test/test/test");
        return whitelistConfig;
    }

    /**
     * 获取默认推送配置
     */
    private Map<String, Object> getDefaultPushConfig() {
        Map<String, Object> pushConfig = new HashMap<>();
        pushConfig.put("jpush.appkey", "0f39aadb2d14d6a34c8a0d14");
        pushConfig.put("jpush.live.time", "10000");
        pushConfig.put("jpush.masterSecret", "2d5c37c625445ee37080ecbb");
        return pushConfig;
    }

    /**
     * 获取默认OSS配置
     */
    private Map<String, Object> getDefaultOssConfig(String active) {
        Map<String, Object> ossConfig = new HashMap<>();
        ossConfig.put("oss.access.key.id", "LTAI4G9c14PgKvM23WZ9zrpc");
        ossConfig.put("oss.access.key.secret", "FpClTp4OVrRRtHEfi3lBOWUoLxKieW");
        ossConfig.put("oss.endpoint.default", "oss-cn-beijing.aliyuncs.com");
        ossConfig.put("oss.endpoint.internal", "https://taohaoliang.oss-cn-beijing.aliyuncs.com/");
        ossConfig.put("oss.bucket.name", "prod".equals(active) ? "eliangeyun" : "eliangeyun-test");
        return ossConfig;
    }

    /**
     * 获取默认MybatisPlus配置
     */
    private Map<String, Object> getDefaultMybatisPlusConfig() {
        Map<String, Object> mybatisPlusConfig = new HashMap<>();
        mybatisPlusConfig.put("mybatis-plus.mapper-locations", "classpath*:/mapper/*Mapper.xml");
        mybatisPlusConfig.put("mybatis-plus.global-config.logic-delete-value", "1");
        mybatisPlusConfig.put("mybatis-plus.global-config.logic-not-delete-value", "0");
        mybatisPlusConfig.put("mybatis-plus.configuration.map-underscore-to-camel-case", "true");
        return mybatisPlusConfig;
    }
}