/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved.
 * <a>http://www.crunii.com</a>
 */
package com.crunii.micro.ds.single.listener;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.AbstractListener;
import com.crunii.micro.common.constants.DSConstants;
import com.crunii.micro.ds.single.druid.MainDSMysqlConfiguration;
import com.crunii.micro.ds.single.druid.MainDSPostgresqlConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.io.InputStreamResource;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Properties;

/**
 * <pre>
 * 	   复制类的时候注意修改 @AutoConfigureAfter(LogDSConfiguration.class)
 * 	   修改为对应的数据源配置的Configuration类！！！！！！！！！！！！！
 * </pre>
 *
 * @author 田平 create 2023-01-19 11:26:56
 */
@ConditionalOnBean(name = DSConstants.DB_MAIN + "DS")
@AutoConfigureAfter({
		MainDSMysqlConfiguration.class,
		MainDSPostgresqlConfiguration.class
})
@Slf4j
public class MainSinglePwdChangeConfiguration implements InitializingBean {

	@NacosInjected
	private ConfigService configService;

	@Resource(name = DSConstants.DB_MAIN + "DS")
	private DataSource dataSource;

	@Value("${dbtype.main}")
	private String dbTypeMain;

	@Override
	public void afterPropertiesSet() throws Exception {
		log.info("add password update listener:{}", DSConstants.DB_MAIN + "DS");
		String dataId = DSConstants.getDruidDsDataId(DSConstants.DB_MAIN, dbTypeMain);
		configService.addListener(dataId, Constants.DEFAULT_GROUP, new AbstractListener() {
			@Override
			public void receiveConfigInfo(String config) {
				DruidDataSource dataSource = (DruidDataSource) MainSinglePwdChangeConfiguration.this.dataSource;
				YamlPropertiesFactoryBean yamlProFb = new YamlPropertiesFactoryBean();
				yamlProFb.setResources(new InputStreamResource(IOUtils.toInputStream(config)));
				Properties properties = yamlProFb.getObject();
				String password = (String) properties.get("druid." + DSConstants.DB_MAIN + "." + dbTypeMain + ".password");
				if (!dataSource.getPassword().equals(password)) {
					log.info("{}DS password changed oldPwd:{},newPwd:{} ", DSConstants.DB_MAIN, dataSource.getPassword(), password);
					dataSource.setPassword(password);
				} else {
					log.info("{}DS password not change oldPwd:{},newPwd:{} ", DSConstants.DB_MAIN, dataSource.getPassword(), password);
				}
			}
		});
	}
}
