package com.eactive.eai.rms.onl.manage.serviceinfo.addqueue;

import com.eactive.eai.data.entity.onl.server.EAIServer;
import com.eactive.eai.rms.common.base.OnlBaseAnnotationController;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.data.entity.onl.chnlInfoIntegration.ChnlInfoIntegrationSearch;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * <pre>
 *     1. 기능 : 에러코드 관리 화면의 컨트롤러
 * </pre>
 *
 * @author : 
 * @version : $Revision: 1.24 $
 * @since : rms 1.0
 */

@Controller
public class AddQueueController extends OnlBaseAnnotationController  {

   @Autowired
   @Qualifier("addQueueManService")
   private AddQueueManService service;

   @Autowired
   private ComboService comboService;

   @GetMapping(value = "/onl/admin/service/addQueueMan.view")
   public String view() {
      return "/onl/admin/service/addQueueMan";
   }

   @GetMapping(value = "/onl/admin/service/addQueueMan.view", params = "cmd=DETAIL")
   public String viewDetail() {
      return "/onl/admin/service/addQueueManDetail";
   }


   @PostMapping(value = "/onl/admin/service/addQueueMan.json", params = "cmd=LIST")
   public ResponseEntity<GridResponse<AddQueueUI>> selectList(ChnlInfoIntegrationSearch chnlInfoIntegrationSearch,
                                                Pageable pageable) throws Exception {
      Page<AddQueueUI> page = service.selectList(chnlInfoIntegrationSearch, pageable);

      return ResponseEntity.ok(new GridResponse<>(page));
   }

   @PostMapping(value= "/onl/admin/service/addQueueMan.json", params = "cmd=DETAIL")
   private ResponseEntity<List<AddQueueUI>> selectDetail(String chnnl, String serviceType) throws Exception {

      List<AddQueueUI> map = service.selectDetail(chnnl, serviceType);

      return ResponseEntity.ok(map);
   }

   @RequestMapping(value= "/onl/admin/service/addQueueMan.json",params = "cmd=INSERT")
   private ResponseEntity<String> insert(HttpServletRequest request, HttpServletResponse response, @RequestParam HashMap<String,Object> paramMap, String gridData ) throws Exception {

      Gson gson = new Gson();
      @SuppressWarnings("unchecked")
      HashMap<String, Object>[] list = gson.fromJson(gridData, HashMap[].class);

      service.insert(paramMap,list);

      return ResponseEntity.ok("insert success");

   }

   @RequestMapping(value= "/onl/admin/service/addQueueMan.json",params = "cmd=UPDATE")
   private ResponseEntity<String> update(HttpServletRequest request, HttpServletResponse response, @RequestParam HashMap<String,Object> map, String gridData  ) throws Exception {
      Gson gson = new Gson();
      @SuppressWarnings("unchecked")
      HashMap<String, Object>[] list = gson.fromJson(gridData, HashMap[].class);

      service.update(map,list);

      return ResponseEntity.ok("update success");

   }

   @RequestMapping(value= "/onl/admin/service/addQueueMan.json",params = "cmd=DELETE")
   private ResponseEntity<String> delete(HttpServletRequest request,
                                HttpServletResponse response, @RequestParam HashMap paramMap) throws Exception {

      service.delete(paramMap);

      return ResponseEntity.ok("delete success");
   }

   @RequestMapping(value= "/onl/admin/service/addQueueMan.json",params = "cmd=LIST_DETAIL_COMBO")
   public ModelAndView initCombo( HttpServletRequest request,
                           HttpServletResponse response) throws Exception {

      List listChnnl         = service.selectChnnlList();
      List<EAIServer> listNode = service.selectNodeList();
      List listQueue = service.selectUserQueueList();
      List<ComboVo> listGubun = comboService.getMonitoringCodeSortedByCodeName("FLOW_CODE");

      Map resultMap = new HashMap();
      resultMap.put("listChnnl"         , listChnnl);
      resultMap.put("listNode"          , listNode);
      resultMap.put("listQueue"         , listQueue);
      resultMap.put("listGubun"         , listGubun);

      ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);
      return modelAndView;
   }

}