package io.renren.common.export;

import cn.afterturn.easypoi.handler.inter.IExcelDictHandler;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Component
public class GlobalExcelDictHandler implements IExcelDictHandler {
	
	private static final Logger LOG = LoggerFactory.getLogger(GlobalExcelDictHandler.class);

	@Override
	public String toName(String dict, Object obj, String name, Object value) {//导出
		return getDictCache(dict).get(value);
	}

	@Override
	public String toValue(String dict, Object obj, String name, Object value) {//导入
		return getDictCache(dict).inverse().get(name);
	}
	
	public BiMap<String,String> getDictCache(String catalog){
		BiMap<String,String> weekNameMap = HashBiMap.create();
		if(catalog.equals("advertiserRole")) {
			weekNameMap.put("ROLE_CHILD_ADVERTISER","广告主");
			weekNameMap.put("ROLE_ADVERTISER","普通广告主（直客）");
			weekNameMap.put("ROLE_CHILD_ADVERTISER","普通广告主（代理商子客户）");
			weekNameMap.put("ROLE_CHILD_AGENT","二级代理商");
			weekNameMap.put("ROLE_AGENT","一级代理商");
		}
		if(catalog.equals("advertiserStatus")) {
			weekNameMap.put("STATUS_DISABLE","已禁用");
			weekNameMap.put("STATUS_PENDING_CONFIRM","申请待审核");
			weekNameMap.put("STATUS_PENDING_VERIFIED","待验证");
			weekNameMap.put("STATUS_CONFIRM_FAIL","审核不通过");
			weekNameMap.put("STATUS_ENABLE","已审核");
			weekNameMap.put("STATUS_CONFIRM_FAIL_END","CRM审核不通过");
			weekNameMap.put("STATUS_PENDING_CONFIRM_MODIFY","修改待审核");
			weekNameMap.put("STATUS_CONFIRM_MODIFY_FAIL","修改审核不通过");
			weekNameMap.put("STATUS_LIMIT","限制");
			weekNameMap.put("STATUS_WAIT_FOR_BPM_AUDIT","等待CRM审核");
			weekNameMap.put("STATUS_WAIT_FOR_PUBLIC_AUTH","待对公验证");
			weekNameMap.put("STATUS_SELF_SERVICE_UNAUDITED","自助开户待验证资质");
		}
		if (catalog.equals("transactionType")){
			weekNameMap.put("RECHARGE","充值");
			weekNameMap.put("TRANSFER","转账");
		}
		return weekNameMap;
	}
	
	
	//定义一个本地缓存临时缓存字典数据
	ListeningExecutorService refreshPools = MoreExecutors
			.listeningDecorator(Executors.newFixedThreadPool(2));
	LoadingCache<String, Map<String, String>> cache = CacheBuilder.newBuilder()
			.refreshAfterWrite(30, TimeUnit.MINUTES).expireAfterAccess(1, TimeUnit.HOURS).maximumSize(50)
			.build(new CacheLoader<String, Map<String, String>>() {
				@Override
				// 当本地缓存命没有中时，调用load方法获取结果并将结果缓存
				public Map<String, String> load(String key) {
					LOG.info("开始从远程读取缓存到本地");
					Map<String, String> data = getDictCache(key);
					return data;
				}
				// 刷新
				@Override
				public ListenableFuture<Map<String, String>> reload(String key, Map<String, String> oldValue)
						throws Exception {
					LOG.info("刷新远程读取缓存到本地");
					return refreshPools.submit(new Callable<Map<String, String>>() {
						@Override
						public Map<String, String> call() throws Exception {
							LOG.info("开始从远程读取缓存到本地");
							Map<String, String> data = getDictCache(key);
							return data;
						}
					});
				}
			});
}