package com.eactive.eai.rms.onl.manage.comm.server;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
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.rms.common.spring.BaseController;
import com.eactive.eai.rms.common.vo.GridResponse;

@Controller
public class AdapterInstManController extends BaseController {

	private AdapterInstManService adapterInstManService;

	@Autowired
	public AdapterInstManController(AdapterInstManService adapterInstManService) {
		this.adapterInstManService = adapterInstManService;
	}

	@GetMapping(value = "/onl/admin/common/adapterInstMan.view")
	public String viewList() {
		return "/onl/admin/common/adapterInstMan";
	}

	@PostMapping(value = "/onl/admin/common/adapterInstMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<AdapterInstUI>> selectList(String searchAdptGrpName, String searchPortNum) {
		List<AdapterInstUI> adapterInstUIs = new ArrayList<>();
		if (searchAdptGrpName != null && searchPortNum != null) {
			adapterInstUIs = adapterInstManService.selectList(searchAdptGrpName, searchPortNum);
		}

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

	@PostMapping(value = "/onl/admin/common/adapterInstMan.json", params = "cmd=LIST_SUB")
	public ResponseEntity<GridResponse<Map<String, Object>>> selectSubList(HttpServletRequest request,
			HttpServletResponse response, String searchAdptGrpName) {
		List<Map<String, Object>> map = adapterInstManService.selectSubList(searchAdptGrpName);
		return ResponseEntity.ok(new GridResponse<>(map));
	}

	@PostMapping(value = "/onl/admin/common/adapterInstMan.json", params = "cmd=TRANSACTION_STARTSTOP")
	public ResponseEntity<Map<String, Object>> transactionStartStop(String eaiInstanceName, String control,
			String adapterGroupName, String adapterName, String level) throws Exception {

		Map<String, Object> resultMap = adapterInstManService
				.transactionStartStop(eaiInstanceName, control, adapterGroupName, adapterName, level);
		return ResponseEntity.ok(resultMap);
	}

}