package com.ctrip.xpipe.service.foundation;

import com.ctrip.framework.foundation.Foundation;
import com.ctrip.xpipe.api.foundation.FoundationService;
import com.ctrip.xpipe.utils.VisibleForTesting;

import java.util.Map;

/**
 * @author wenchao.meng
 *
 * Jun 13, 2016
 */
public class CtripFoundationService implements FoundationService{
	
	protected static final String DATA_CENTER_KEY = "XPIPE_DATA_CENTER";

	protected static final String GROUP_ID_KEY = "XPIPE_GROUP_ID";

	private static FoundationConfig config = new FoundationConfig();

	@Override
	public String getDataCenter() {
		Map<String, String> groupDcMap = config.getGroupDcMap();
		String groupId = System.getProperty(GROUP_ID_KEY, Foundation.group().getId());
		if (null != groupDcMap && groupDcMap.containsKey(groupId)) return groupDcMap.get(groupId);
		else return System.getProperty(DATA_CENTER_KEY, Foundation.server().getDataCenter());
	}

	@Override
	public String getAppId() {
		return Foundation.app().getAppId();
	}

	@Override
	public String getLocalIp() {
		return Foundation.net().getHostAddress();
	}

	public int getHttpPort() {
		return Foundation.web().getHttpPort();
	}

	@Override
	public String getGroupId() {
		return Foundation.group().getId();
	}

	public String getEnvironment() {
		return Foundation.server().getEnv().getName();
	}

	@Override
	public int getOrder() {
		return HIGHEST_PRECEDENCE;
	}

	@VisibleForTesting
	protected static void setConfig(FoundationConfig config) {
		CtripFoundationService.config = config;
	}

}

@@ -583,16 +583,44 @@
 ter() %7B%0A
+%09%09String dataCenter = null;%0A
 %09%09Map%3CSt
@@ -812,22 +812,29 @@
 oupId)) 
-return
+ dataCenter =
  groupDc
@@ -858,22 +858,29 @@
 %0A%09%09else 
-return
+ dataCenter =
  System.
@@ -946,16 +946,99 @@
 ter());%0A
+%09%09if(dataCenter == null)%7B%0A%09%09%09dataCenter = %22NODC_DEFINED%22;%0A%09%09%7D%0A%09%09return dataCenter;%0A
 %09%7D%0A%0A%09@Ov

make ctripsercice hickwalltest pass