package com.engine.htsc.meeting.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.meeting.service.MeetingService;
import com.engine.htsc.meeting.service.impl.MeetingServiceImpl;
import com.engine.htsc.trip.util.AjaxResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MeetingProposalAction {

  private MeetingService getService(User user) {
    return ServiceUtil.getService(MeetingServiceImpl.class, user);
  }

  private final Log log = LogFactory.getLog(MeetingProposalAction.class);

  private MeetingService getService() {
    return ServiceUtil.getService(MeetingServiceImpl.class);
  }

  @GET
  @Path("/getParticipants")
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<List<Map<String, String>>> getParticipants(@Context HttpServletRequest request, @Context HttpServletResponse response){
    try {
      List<Map<String, String>> participants = getService().getParticipants();
      return AjaxResult.ok(participants);
    } catch (Exception e) {
      log.error(e);
      return AjaxResult.error(e.getMessage());
    }
  }

  @GET
  @Path("/getForm")
  @Produces({MediaType.TEXT_PLAIN})
  public String getFormDemo(@Context HttpServletRequest request, @Context HttpServletResponse response){
    Map<String, Object> apidatas = new HashMap<>();
    try {
      //获取当前用户
      User user = HrmUserVarify.getUser(request, response);
      apidatas.putAll(getService(user).getFormDemo(ParamUtil.request2Map(request)));
      apidatas.put("api_status", true);
    } catch (Exception e) {
      e.printStackTrace();
      apidatas.put("api_status", false);
      apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
    }
    return JSONObject.toJSONString(apidatas);
  }

  @POST
  @Path("/saveForm")
  @Produces({MediaType.TEXT_PLAIN})
  public String saveFormDemo(@Context HttpServletRequest request, @Context HttpServletResponse response){
    Map<String, Object> apidatas = new HashMap<>();
    try {
      //获取当前用户
      User user = HrmUserVarify.getUser(request, response);
      apidatas.putAll(getService(user).saveFormDemo(ParamUtil.request2Map(request)));
      apidatas.put("api_status", true);
    } catch (Exception e) {
      e.printStackTrace();
      apidatas.put("api_status", false);
      apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
    }
    return JSONObject.toJSONString(apidatas);
  }

  @POST
  @Path("/archiving")
  @Produces({MediaType.TEXT_PLAIN})
  public String archiving(@Context HttpServletRequest request, @Context HttpServletResponse response){
    Map<String, Object> apidatas = new HashMap<>();
    try {
      //获取当前用户
      User user = HrmUserVarify.getUser(request, response);
      apidatas.putAll(getService(user).archiving(ParamUtil.request2Map(request)));
      apidatas.put("api_status", true);
    } catch (Exception e) {
      e.printStackTrace();
      apidatas.put("api_status", false);
      apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
    }
    return JSONObject.toJSONString(apidatas);
  }

  @POST
  @Path("/getImageFileData")
  @Produces({MediaType.APPLICATION_JSON})
  public AjaxResult<Map<String, Object>> getImageFileData(@Context HttpServletRequest request){
    try {
      Map<String, Object> request2Map = ParamUtil.request2Map(request);
      String imagefileids = (String) request2Map.get("imagefileids");
      Map<String, Object> imageFileInfoByFieldIds = getService().getImageFileInfoByFieldIds(imagefileids);
      return AjaxResult.ok(imageFileInfoByFieldIds);
    } catch (Exception e) {
      e.printStackTrace();
      return AjaxResult.error(e.getMessage());
    }
  }
}
