/*
 * Copyright (c) 2021 LG Electronics Inc.
 * SPDX-License-Identifier: AGPL-3.0-only 
 */

package oss.fosslight.controller;

import java.io.IOException;
import java.util.HashMap;
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.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import oss.fosslight.CoTopComponent;
import oss.fosslight.common.CoConstDef;
import oss.fosslight.common.CommonFunction;
import oss.fosslight.common.Url.SESSION;
import oss.fosslight.common.Url.VULNERABILITY;
import oss.fosslight.domain.OssMaster;
import oss.fosslight.domain.Vulnerability;
import oss.fosslight.service.VulnerabilityService;

@Controller
public class VulnerabilityController extends CoTopComponent {
	@Autowired VulnerabilityService vulnerabilityService;
	
	@GetMapping(value=VULNERABILITY.LIST, produces = "text/html; charset=utf-8")
	public String list(HttpServletRequest req, HttpServletResponse res, Model model){
		Object _param =  getSessionObject(CoConstDef.SESSION_KEY_PREFIX_DEFAULT_SEARCHVALUE + "VULNOSSNAME", true);
		
		if (_param != null) {
			String defaultSearchOssName = (String) _param;
			
			if (!isEmpty(defaultSearchOssName)) {
				if (defaultSearchOssName.indexOf("|") > -1) {
					String[] params = defaultSearchOssName.split("\\|");
					
					model.addAttribute("defaultOssName", params[0]);
					model.addAttribute("defaultOssVersion", params[1]);
				} else {
					model.addAttribute("defaultOssName", defaultSearchOssName);
				}
			}
			
			model.addAttribute("exactMatchFlag", !isEmpty(defaultSearchOssName) ? CoConstDef.FLAG_YES : CoConstDef.FLAG_NO);
		}
		
		return VULNERABILITY.LIST_JSP;
	}
	
	@GetMapping(value=VULNERABILITY.LIST_AJAX, produces = "text/html; charset=utf-8")
	public @ResponseBody ResponseEntity<Object> listajax(@ModelAttribute Vulnerability vulnerability,
			HttpServletRequest req, HttpServletResponse res) {
		int page = Integer.parseInt(req.getParameter("page"));
		int rows = Integer.parseInt(req.getParameter("rows"));
		
		vulnerability.setCurPage(page);
		vulnerability.setPageListSize(rows);
		
		HashMap<String, Object> map = vulnerabilityService.getVulnerabilityList(vulnerability, false);
		
		return makeJsonResponseHeader(map);
	}
	
	@GetMapping(value=VULNERABILITY.CHECK_CVE_ID, produces = "text/html; charset=utf-8")
	public @ResponseBody ResponseEntity<Object> checkCveId(@RequestParam(name = "cveId") String cveId) {
		
		return makeJsonResponseHeader(vulnerabilityService.getCveInfo(cveId));
	}
	
	@GetMapping(value=VULNERABILITY.AUTOCOMPLETE_AJAX, produces = "text/html; charset=utf-8")
	public @ResponseBody ResponseEntity<Object> AutoCompleteAjax(HttpServletRequest req, HttpServletResponse res){
		HashMap<String, Object> map = vulnerabilityService.getVulnerabilityAutoComplete();
		
		return makeJsonResponseHeader(map);
	}
	
	@GetMapping(value=VULNERABILITY.VERSION_AUTOCOMPLETE_AJAX, produces = "text/html; charset=utf-8")
	public @ResponseBody ResponseEntity<Object> VersionAutoCompleteAjax(HttpServletRequest req, HttpServletResponse res){
		String name = avoidNull(req.getParameter("product"));
		
		List<String> list = vulnerabilityService.getVulnerabilityVersionAutoComplete(name);
		
		return makeJsonResponseHeader(list);
	}
	
	
	@GetMapping(value=VULNERABILITY.VULN_POPUP, produces = "text/html; charset=utf-8")
	public String vulnpopup(HttpServletRequest req, HttpServletResponse res, @ModelAttribute OssMaster bean, Model model) throws IOException{
		if (!isLogin()) {
			return SESSION.LOGIN_JSP;
		}
		model.addAttribute("ossInfo", bean);

		return VULNERABILITY.VULN_POPUP_JSP;
	}
	
	@GetMapping(value=VULNERABILITY.VULN_LIST)
	public @ResponseBody ResponseEntity<Object> getVulnList(
			HttpServletRequest req, 
			HttpServletResponse res,
			@ModelAttribute OssMaster bean, 
			Model model){
		Map<String, Object> resultMap = new HashMap<>();
		
		if (!isEmpty(bean.getOssName())) {
			int page = Integer.parseInt(req.getParameter("page"));
			int rows = Integer.parseInt(req.getParameter("rows"));
			String sidx = req.getParameter("sidx");
			String sord = req.getParameter("sord");	
			
			Map<String, String> exceptionMap = new HashMap<>();
			exceptionMap.put("product", "product");
			exceptionMap.put("version", "version");
			exceptionMap.put("cvssScore", "cvssScore");
			exceptionMap.put("cveId", "cveId");
			exceptionMap.put("modiDate", "modiDate");
			
			String filters = "";
			
			if (!isEmpty(bean.getFilters())) {
				filters = bean.getFilters().replaceAll("\"op\":\"cn\"", "\"op\":\"eq\"");	// popup에서는 equals만 지원함.
			}
			
			String filterCondition = CommonFunction.getFilterToString(filters, null, exceptionMap);
			
			if (!isEmpty(filterCondition)) {
				bean.setFilterCondition(filterCondition);
			}
			
			bean.setSord(sord);
			bean.setSidx(sidx);
			bean.setCurPage(page);
			bean.setPageListSize(rows);
			
			if (avoidNull(bean.getVulnerabilityCheckFlag(), "").equals(CoConstDef.FLAG_NO)) {
				bean.setVulnerabilityCheckFlag(null);
			} else {
				bean.setVulnerabilityCheckFlag(CoConstDef.FLAG_YES);
			}
			
			resultMap = vulnerabilityService.getVulnListByOssName(bean);
		}
		
		return makeJsonResponseHeader(resultMap);
	}
}
