
package org.tio.sitexxx.service.service;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.sitexxx.service.init.PropInit;
import org.tio.sitexxx.service.model.main.Conf;
import org.tio.sitexxx.service.model.main.ConfDev;
import org.tio.sitexxx.service.vo.Const;
import org.tio.sitexxx.service.vo.OssConfig;
import org.tio.utils.jfinal.P;
import org.tio.utils.lock.LockUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 配置信息加载
 * @author tanyaowu
 *
 */
public class ConfService {
	private static Logger				log			= LoggerFactory.getLogger(ConfService.class);
	public static final ConfService		me			= new ConfService();
	final static Conf					confDao		= new Conf().dao();
	final static ConfDev				confDevDao	= new ConfDev().dao();
	/**
	 * key: name, value: value
	 */
	private static Map<String, String>	cacheData	= null;

	/**
	 * 
	 */
	public static void clearCache() {
		try {
			LockUtils.runWriteOrWaitRead(ConfService.class.getName(), ConfService.class, () -> {
				cacheData = null;
			});
		} catch (Exception e) {
			log.error("", e);
		}
	}

	/**
	 * 清空所有配置的缓存
	 * 
	 * @author tanyaowu
	 */
	public static void clearAll() {
		ConfService.clearCache();
		P.clear();
		PropInit.forceInit();
	}

	private static void loadData() {
		//		clearCache();

		Map<String, String> tempCache = new HashMap<>();
		List<Conf> list = null;
		try {
			list = confDao.find("select * from conf");
			if (list == null) {
				list = new ArrayList<>();
			}
			List<ConfDev> list2 = confDevDao.find("select * from conf_dev");

			if (list2 != null) {
				for (ConfDev item : list2) {
					Conf conf = new Conf();
					conf.setName(item.getName());
					conf.setValue(item.getValue());
					conf.setRemark(item.getRemark());
					list.add(conf);
				}
			}

			if (list.size() == 0) {
				return;
			}

			for (Conf conf : list) {
				tempCache.put(conf.getName(), conf.getValue());
			}

		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			cacheData = tempCache;
		}
	}

	/**
	 * 根据参数名字获取参数值
	 * @param name
	 * @param defaultValue 默认值
	 * @return
	 */
	public static String getString(String name, String defaultValue) {
		if (cacheData == null) {
			try {
				LockUtils.runWriteOrWaitRead(ConfService.class.getName(), ConfService.class, () -> {
				    if (cacheData == null) {
						loadData();
					}
				});
			} catch (Exception e) {
				log.error("", e);
			}
		}
		if (cacheData == null) {
			return "";
		}
		String value = cacheData.get(name);
		if (!StrUtil.isBlank(value)) {
			return value;
		} else {
			return defaultValue;
		}
	}

	public static OssConfig getOssConfig(){
		String accessKey = getString(Const.OssConfig.OSS_ACCESS_KEY, null);
		String secretKey = getString(Const.OssConfig.OSS_SECRET_KEY, null);
		String endpoint = getString(Const.OssConfig.OSS_ENDPOINT, null);
		String bucketName = getString(Const.OssConfig.OSS_BUCKET_NAME, null);
		if(StrUtil.isEmpty(accessKey) || StrUtil.isEmpty(secretKey) || StrUtil.isEmpty(endpoint) || StrUtil.isEmpty(bucketName)){
			log.error("OSS config is not valid");
			return null;
		}
		OssConfig ossConfig = new OssConfig();
		ossConfig.setAccessKeyId(accessKey);
		ossConfig.setAccessKeySecret(secretKey);
		ossConfig.setBucketName(bucketName);
		ossConfig.setEndpoint(endpoint);
		ossConfig.setDomain(endpoint+bucketName);
		return ossConfig;
	}

	/**
	 * 
	 * @param name
	 * @param defaultValue
	 * @return
	 */
	public static Integer getInt(String name, Integer defaultValue) {
		String value = getString(name, null);
		if (value == null) {
			return defaultValue;
		}
		return Integer.parseInt(value);
	}

	/**
	 * @param name
	 * @param defaultValue
	 * @return
	 * @author xufei
	 * 2020年12月3日 上午10:45:21
	 */
	public static Short getShort(String name, Short defaultValue) {
		String value = getString(name, null);
		if (value == null) {
			return defaultValue;
		}
		return Short.parseShort(value);
	}

	/**
	 * @param name
	 * @param defaultValue
	 * @return
	 */
	public static Double getDouble(String name, Double defaultValue) {
		String value = getString(name, null);
		if (value == null) {
			return defaultValue;
		}
		return Double.parseDouble(value);
	}

	/**
	 * 
	 * @param name
	 * @param defaultValue
	 * @param trueValue
	 * @return
	 */
	public static Boolean getBoolean(String name, String defaultValue, String trueValue) {
		String value = getString(name, defaultValue);
		return value.equalsIgnoreCase(trueValue);
	}

	/**
	 * 值为true、TRUE的返回true
	 * @param name
	 * @param defaultValue
	 * @return
	 */
	public static Boolean getBoolean(String name, String defaultValue) {
		String value = getString(name, defaultValue);
		return value.equalsIgnoreCase("true");
	}

	/**
	 * @param name
	 * @param defaultValue
	 * @return
	 * @author tanyaowu
	 */
	public static Long getLong(String name, Long defaultValue) {
		String value = getString(name, null);
		if (value == null) {
			return defaultValue;
		}
		return Long.parseLong(value);
	}
}
