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

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 com.eactive.eai.agent.command.CommonCommand;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.common.spring.BaseController;
import com.eactive.eai.rms.common.util.CommonUtil;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.data.entity.onl.eaimsg.EAIMessageUISearch;
import com.eactive.eai.rms.onl.manage.serviceinfo.eaimsg.ui.EAIMessageUI;

@Controller
public class TranChangeSimController extends BaseController {

	private EaiMsgManService eaiMsgManService;

	private ComboService comboService;

	@Autowired
	public TranChangeSimController(EaiMsgManService eaiMsgManService, ComboService comboService) {
		this.eaiMsgManService = eaiMsgManService;
		this.comboService = comboService;
	}
	
	@GetMapping(value = "/onl/admin/common/tranChangeSim.view")
	public void viewList() {
		// view
	}

	@PostMapping(value = "/onl/admin/common/tranChangeSim.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<EAIMessageUI>> selectList(EAIMessageUISearch eaiMessageUISearch, Pageable pageable, String sortname,  String sortorder) {
		eaiMessageUISearch.setTranChangeSim("true");
		Page<EAIMessageUI> page = eaiMsgManService.selectList(eaiMessageUISearch, pageable, sortname, sortorder);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/admin/common/tranChangeSim.json", params = "cmd=CHANGE_TRANTYPE")
	public ResponseEntity<Map<String, Object>> updateTranType(String tranType, String eaiSvcName) {
		int resultCnt = 0;
		String[] eaiSvcNameArr = eaiSvcName.split(",");

		for (String svcName : eaiSvcNameArr) {
			resultCnt++;
			eaiMsgManService.updateTranType(svcName, tranType);
		}

		// Reload
		CommonCommand.builder()
								.name(CommonCommand.RELOAD_EAI_MESSAGE_COMMAND)
								.args(eaiSvcName)
								.build().broadcast(agentUtilService);
		
		String message = "총" + resultCnt + "건의 서비스를 수정하였습니다";
		
		return ResponseEntity.ok().body(Collections.singletonMap("message", message));
	}

	@PostMapping(value = "/onl/admin/common/tranChangeSim.json", params = "cmd=LIST_INIT")
	public ResponseEntity<Map<String, Object>> listInitCombo(String codeGroupArr) {
		// 취약점소스 수정 2020.08.27
		List<ComboVo> tranTypeList = StringUtils.isNotBlank(codeGroupArr) 
												? comboService.getFromCode(codeGroupArr) : new ArrayList<>();
		
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("list", tranTypeList);
		return ResponseEntity.ok(resultMap);
	}

	@PostMapping(value = "/onl/admin/common/tranChangeSim.json", params = "cmd=INITIALIZES")
	public ResponseEntity<Map<String, Object>> initializes() throws Exception {
		CommonCommand command = new CommonCommand(CommonCommand.RELOAD_EAI_MESSAGE_COMMAND, "ALL");
		Map<String, Object> returnMap = agentUtilService.broadcast(command);

		Map<String, Object> map = new HashMap<>();
		map.put("message", CommonUtil.getMapString(returnMap, true));
		return ResponseEntity.ok(map);
	}

}
