package com.jsowell.adapay.config;

import com.huifu.adapay.Adapay;
import com.huifu.adapay.model.MerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(value = 3)
@Component
public class AdapayConfig implements CommandLineRunner {

	private Logger logger = LoggerFactory.getLogger(AdapayConfig.class);

	@Value("${adapay.apiKey}")
	private String API_KEY;

	@Value("${adapay.mockApiKey}")
	private String MOCK_API_KEY;

	@Value("${adapay.rsaPrivateKey}")
	private String RSA_PRIVATE_KEY;

	@Value("${adapay.debugFlag}")
	private boolean DEBUG_FLAG;

	@Value("${adapay.prodMode}")
	private boolean PROD_MODE;

	@Override
	public void run(String... args) throws Exception {
		/**
		 * debug 模式，开启后有详细的日志
		 */
		Adapay.debug = DEBUG_FLAG;

		/**
		 * prodMode 模式，默认为生产模式，false可以使用mock模式
		 */
		Adapay.prodMode = PROD_MODE;

		/**
		 * 初始化商户配置，服务器启动前，必须通过该方式初始化商户配置完成
		 * apiKey为prod模式的API KEY
		 * mockApiKey为mock模式的API KEY
		 * rsaPrivateKey为商户发起请求时，用于请求参数加签所需要的RSA私钥
		 */
		String apiKey = API_KEY;
		String mockApiKey = MOCK_API_KEY;
		String rsaPrivateKey = RSA_PRIVATE_KEY;
		MerConfig merConfig = new MerConfig();
		merConfig.setApiKey(apiKey);
		merConfig.setApiMockKey(mockApiKey);
		merConfig.setRSAPrivateKey(rsaPrivateKey);
		Adapay.initWithMerConfig(merConfig);
		logger.info("汇付配置初始化成功，debug:{}, prodMode:{}, apiKey:{}, mockApiKey:{}, rsaPrivateKey:{}"
				, DEBUG_FLAG, PROD_MODE, API_KEY, MOCK_API_KEY, RSA_PRIVATE_KEY);
	}
}
