package com.eactive.eai.rms.onl.loader.controller;

import com.eactive.eai.rms.bap.loader.service.SearchBAPService;
import com.eactive.eai.rms.bat.loader.service.BatSearchService;
import com.eactive.eai.rms.common.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;
import com.eactive.eai.rms.common.util.CamelCaseUtil;
import com.eactive.eai.rms.onl.loader.service.DeployResourceDownloadBapService;
import com.eactive.eai.rms.onl.loader.service.DeployResourceDownloadBatService;
import com.eactive.eai.rms.onl.loader.service.DeployResourceDownloadOnlineService;
import com.eactive.eai.rms.onl.loader.service.DeployResourceDownloadServiceFactory;
import com.eactive.eai.rms.onl.loader.service.SearchService;
import com.google.gson.Gson;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
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 java.util.HashMap;
import java.util.Map;

@Controller
public class DeployResourceSearchController {

	private final Logger logger = Logger.getLogger(getClass());

	@Autowired
	@Qualifier("searchService")
	private SearchService onlineSearchService;

	@Autowired
	@Qualifier("batSearchService")
	private BatSearchService batSearchService;

	@Autowired
	@Qualifier("searchBAPService")
	private SearchBAPService bapSearchService;

	@Autowired
	private DeployResourceDownloadServiceFactory serviceFactory;

	@RequestMapping(value = "/common/acl/deploy/deployResourceSearchMan.view")
	private String viewList(HttpServletRequest request, @RequestParam(value = "cmd", required = false) String cmd)
			throws Exception {
		if (cmd == null || "LIST".equals(cmd.toUpperCase())) {
			cmd = "";
		} else {
			cmd = CamelCaseUtil.converCamelCase(cmd);
		}
		String path = request.getServletPath();
		return path.split("[.]")[0] + cmd;
	}

	@RequestMapping(value = "/common/acl/deploy/deployResourceSearchMan.json", params = "cmd=LIST")
	public ModelAndView search(HttpServletRequest request, @RequestParam HashMap<String, Object> paramMap,
			String serviceType) throws Exception {

		if (!DataSourceTypeManager.getDataSourceTypeNames().contains(serviceType.toUpperCase()))
			throw new Exception("Invalid ServiceType:" + serviceType);

		HashMap map = null;
		if (DataSourceTypeManager.getDataSourceType(serviceType).isOnline()) {
			map = onlineSearchService.selectList(request, 1, 999, paramMap);
		} else if (DataSourceTypeManager.getDataSourceType(serviceType).getName().equals(DataSourceTypeManager.BAT)) {
			map = batSearchService.selectList(request, 0, 0, paramMap);
		} else if (DataSourceTypeManager.getDataSourceType(serviceType).getName().equals(DataSourceTypeManager.BAP)) {
			map = bapSearchService.selectBAPList(request, 0, 0, paramMap);
		}

		Map resultMap = new HashMap();
		resultMap.put("rows", map.get("list"));

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

	@PostMapping(value = "/common/acl/deploy/deployResourceSearchMan.json", params = "cmd=DOWNLOAD_RESOURCES_ZIP")
	public ResponseEntity<Object> downloadZip(String serviceType, String gridData) {

		DataSourceContextHolder.setDataSourceType(DataSourceTypeManager.getDataSourceType(serviceType));

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

			byte[] resourcesZip = serviceFactory.create(serviceType).getResourcesZip(list);

			return ResponseEntity.ok()
					.header("Content-Disposition", "attachment; filename=" + serviceType + "deployResources.zip")
					.body(resourcesZip);

		} catch (Exception e) {
			logger.error(e);
			return ResponseEntity.internalServerError().body(e.getMessage());
		}

	}

}