package com.api.htsc.browser;

import com.alibaba.fastjson.JSONObject;
import com.api.browser.service.Browser;
import com.api.browser.util.BrowserConfigComInfo;
import com.api.browser.util.BrowserConstant;
import com.api.browser.util.DeviceTypeAttr;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import org.apache.commons.lang3.StringUtils;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

/**
 * 华泰浏览框接口
 */
@Path("/htsc/browser")
public class HtscBrowserAction {
  
  /**
   * 设置浏览框的默认配置
   *
   * @param browser
   * @param params
   */
  public void setBrowserStatus(Browser browser, Map<String, Object> params) {
    String deviceType = Util.null2String(params.get("_ec_device"));
    String isMobile = Util.null2String(params.get("_ec_ismobile"));
    browser.setDeviceType(DeviceTypeAttr.PC);
    //pc mobile_ec mobile_dingding
    if (StringUtils.isNotBlank(deviceType)) {
      DeviceTypeAttr dt = DeviceTypeAttr.valueOf(deviceType.toUpperCase());
      browser.setDeviceType(dt);
    }
    
    browser.setMobile("true".equals(isMobile));
  }
  
  @POST
  @Path("/data/{type}")
  @Produces("text/plain;charset=utf-8")
  public String getBrowserData2(@PathParam("type") String type, @Context HttpServletRequest request,
      @Context HttpServletResponse response) {
    return getBrowserData(type, request, response);
  }
  
  @GET
  @Path("/data/{type}")
  @Produces("text/plain;charset=utf-8")
  public String getBrowserData(@PathParam("type") String type, @Context HttpServletRequest request,
      @Context HttpServletResponse response) {
    BrowserConfigComInfo browserConfigComInfo = new BrowserConfigComInfo();
    Map<String, Object> params = getRequestParams(request, response);
    String browserClassName = browserConfigComInfo.getClazz(type);
    Map<String, Object> apidatas = new HashMap<String, Object>();
    try {
      // 替换为华泰Browser
      String className = String.format("com.api.htsc.browser.service.Htsc%s",
          browserClassName.substring(browserClassName.lastIndexOf(".") + 1));
      Browser browser = (Browser) Class.forName(className).newInstance();
      browser.setBrowserType(type);
      setBrowserStatus(browser, params);
      //获取测试预览设置的用户
      if (params.get("operatoruser_" + params.get("currenttime")) != null) {
        browser.setUser(this.getPreviewUser(request));
      } else {
        browser.setUser(this.getUserByRequest(request, response));
      }
      
      String sqlWhere = Util.null2String(browser.spellSqlWhere(params));
      String tempSqlWhere = Util.null2String(params.get(BrowserConstant.SPELL_SQL_WHERE));
      if (!"".equals(sqlWhere) || !tempSqlWhere.equals(sqlWhere)) {
        params.put(BrowserConstant.SPELL_SQL_WHERE, tempSqlWhere + " " + sqlWhere);
      }
      apidatas.putAll(browser.getBrowserData(params));
      
    } catch (Exception e) {
      e.printStackTrace();
      apidatas.put("status", false);
      apidatas.put("error", e.getMessage());
      apidatas.put("api_status", false);
      apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
    }
    
    return JSONObject.toJSONString(apidatas);
  }
  
  
  private Map<String, Object> getRequestParams(HttpServletRequest request,
      HttpServletResponse response) {
    Map<String, Object> params = new HashMap<String, Object>();
    Enumeration<String> em = request.getParameterNames();
    while (em.hasMoreElements()) {
      String paramname = em.nextElement();
      params.put(paramname, request.getParameter(paramname));
    }
    return params;
  }
  
  private User getUserByRequest(HttpServletRequest request, HttpServletResponse response) {
    String f_weaver_belongto_userid = Util
        .null2String(request.getParameter("f_weaver_belongto_userid"));
    String f_weaver_belongto_usertype = Util
        .null2String(request.getParameter("f_weaver_belongto_usertype"));
    User user = HrmUserVarify
        .getUser(request, response, f_weaver_belongto_userid, f_weaver_belongto_usertype);
    return user;
  }
  
  /**
   * 获取测试预览操作用户
   *
   * @param request
   * @return
   */
  private User getPreviewUser(HttpServletRequest request) {
    String f_weaver_belongto_userid = Util
        .null2String(request.getParameter("f_weaver_belongto_userid"));
    String f_weaver_belongto_usertype = Util
        .null2String(request.getParameter("f_weaver_belongto_usertype"));
    User user = User.getUser(Integer.parseInt(f_weaver_belongto_userid),
        Integer.parseInt(f_weaver_belongto_usertype));
    return user;
  }
}
