package com.blueland.bpm.manage.util;

import com.blueland.bpm.common.util.ApplicationContextHolder;
import com.blueland.bpm.common.util.SysConfig;
import com.blueland.bpm.log.service.TlogUserOperateManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


public class MyContants {
	public static final String userId = "bpm_login_cookie_userid";
	public static final String APP_ID_KEY="app_id";
	public static final String APPLICATION="application";
    public static final String MAX_COOKIE=(String) SysConfig.getValue("MAX_COOKIE");
	
	public static final String MAX_SESSION=(String) SysConfig.getValue("MAX_SESSION");
	public static final String LOGIN_SESSION_USER ="bpm_login_session_user";
	public static final String TOP_ORG_ID= (String)SysConfig.getValue("TOP_ORG_ID");
	public static final String profile= (String)SysConfig.getValue("profile");
	
	public static String BPM_PROCESS_MODELER_PATH = (String) SysConfig
			.getValue("bpm_process_modeler_path") ;
	
	public static final String EBPM_UNITEWORK_MANAGE_PATH = (String) SysConfig
			.getValue("EBPM_UNITEWORK_MANAGE_PATH") ;
	public static final String PERSONAL_OFFICE_PATH = (String) SysConfig
			.getValue("personal_office_path") ;
	public static final String IS_OPEN_TENANT=(String)SysConfig.getValue("IS_OPEN_TENANT");
	public static final String USER_ROLES="user_roles";
	public static final Map DV_STATUS=new HashMap();
	private static final Logger log = LoggerFactory.getLogger(MyContants.class);
	
	public static final String KSKF_PAHT = (String) SysConfig
			.getValue("kskf_path") ;
	public static final String BPM_FILE_PATH = (String) SysConfig
			.getValue("bpm_file_path") ;
	public static final String BPM_FRONT_PATH = (String) SysConfig
			.getValue("bpm_front_path") ;
	
	@Autowired
	private static  TlogUserOperateManager userOperateService ;
	
	public static void setUserOperateService(
			TlogUserOperateManager userOperateService) {
		MyContants.userOperateService = userOperateService;
	}

	public static void resetBpmProcessModelerPath(String path){
		BPM_PROCESS_MODELER_PATH = path;
	}

	static{
		DV_STATUS.put("OFF","离线");
		DV_STATUS.put("ON","在线");
	}
	
	public static boolean isTestProfile() {
		if(profile!=null&&profile.equals("test")) {
			return true;
		}
		return false;
	}
	public static String getContextJunction(HttpServletRequest request) {
		String http = "http:";
		String https = "https:";
		String ctx = "";
		//log.info("iv--"+request.getHeader("iv-user"));
		if (null == SysConfig.getValue("CTX")
				||request.getHeader("iv-user") == null) {
			return ctx;
		}
		/*log.info("iv--"+request.getHeader("iv-user"));
		// referer为先前网页的地址，当前请求网页紧随其后,即来路。可视化中用此属性来区分http/https
		String referer = request.getHeader("referer");
		// Via为通知中间网关或代理服务器地址，通信协议。如可视化或直接浏览器访问没有referer,用此属性来区分http/https
		String via = request.getHeader("Via");
		log.info("referer--"+referer);
		log.info("via--"+via);
		*/
		ctx = (String) SysConfig.getValue("CTX");
		/*if (StringUtils.isNotEmpty(referer)) {
			if (referer.indexOf(https) != -1) {// https访问
				ctx = ctx.indexOf(http) != -1 ? ctx.replace(http, https) : ctx;
			} else if (referer.indexOf(http) != -1) {// http访问
				ctx = ctx.indexOf(https) != -1 ? ctx.replace(https, http) : ctx;
			}
		} else if (StringUtils.isNotEmpty(via)) {
			if (request.getHeader("Via").indexOf(":443") != -1) {// https访问
				ctx = ctx.indexOf(http) != -1 ? ctx.replace(http, https) : ctx;
			} else if (request.getHeader("Via").indexOf(":80") != -1) {// http访问
				ctx = ctx.indexOf(https) != -1 ? ctx.replace(https, http) : ctx;
			}
		}
		log.info("ctx-"+ctx);*/
		return ctx;
	}
	
	
	
	
	/**
	 * 
	 * @param request 客户端发送的请求
	 * @param param   需要获取的参数列表
	 * @param isSuccess true为成功  false为失败
	 * @param message 操作的结果字符串，可以自定义，可以为空
	 * @param login_session_user 在session中获取用户信息的key
	 * @param 在session中获取当前租户信息的key
	 */
	public static void saveUserOperate(HttpServletRequest request,
			String paramStr,boolean isSuccess,String message,
			String login_session_user,String applicationStr){
		try {
			userOperateService=(TlogUserOperateManager)ApplicationContextHolder.getBean("tlogUserOperateManager");
			userOperateService.saveUserOperate(request, paramStr, 
					isSuccess, message,login_session_user,applicationStr);
		} catch (Exception e) {
			e.getMessage();
		}
	
	}
}