package com.eactive.eai.rms.onl.manage.authserver.scope;

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

import com.eactive.eai.agent.authserver.ReloadApiScopeCommand;
import com.eactive.eai.agent.command.CommonCommand;
import com.eactive.eai.rms.common.base.OnlBaseAnnotationController;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.data.entity.onl.eaimsg.ApiScopeSearch;
import com.eactive.eai.rms.onl.common.exception.BizException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

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 org.springframework.web.bind.annotation.RequestParam;

import com.eactive.eai.agent.authserver.ReloadApiScopeCommand;
import com.eactive.eai.agent.command.CommonCommand;
import com.eactive.eai.rms.common.base.OnlBaseAnnotationController;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.data.entity.onl.eaimsg.ApiScopeSearch;

@Controller
public class ApiScopeController extends OnlBaseAnnotationController {

	private ApiScopeManService apiScopeManService;

	@Autowired
	public  ApiScopeController(ApiScopeManService apiScopeManService) {
		this.apiScopeManService = apiScopeManService;
	}
	
	@GetMapping(value = "/onl/admin/authserver/apiScopeMan.view")
	public void viewApiScopeList() {
		// view
	}

	@PostMapping(value = "/onl/admin/authserver/apiScopeMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<ApiScopeUI>> selectApiScopeList( Pageable pageable, ApiScopeSearch apiScopeSearch ) {
		Page<ApiScopeUI> page = apiScopeManService.selectApiScopeRelationsList(pageable, apiScopeSearch);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/admin/authserver/apiScopeMan.json", params = "cmd=INSERT")
	public ResponseEntity<Void> insertApiScope(ApiScopeUI apiScopeUI) {
		apiScopeManService.insertApiScopeRelation(apiScopeUI);

		CommonCommand.builder()
								.name(CommonCommand.RELOAD_API_SCOPE_COMMAND)
								.args(new String[] {ReloadApiScopeCommand.COMMAND_TYPE_API_SCOPE, 
															apiScopeUI.getBzwkSvcKeyName(), apiScopeUI.getScopeId()})
								.build()
								.broadcast(agentUtilService);
		
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/admin/authserver/apiScopeMan.json", params = "cmd=INSERT_APILIST")
	public ResponseEntity<Void> insertApiScopeList(@RequestParam("apiList") String apiListJson, @RequestParam("scopeId") String scopeId) {
		
		ObjectMapper mapper = new ObjectMapper();
		List<ApiScopeUI> apiScopeList = null;
		
		try {
	        apiScopeList = mapper.readValue(apiListJson, new TypeReference<List<ApiScopeUI>>(){});
	        
	    } catch (Exception e) {
	        throw new BizException("데이터 변환 중 오류가 발생했습니다.");
	    }
		
		apiScopeManService.insertApiScopeRelationList(apiScopeList, scopeId);
		
		return ResponseEntity.ok().build();
	}
	
	
	@PostMapping(value = "/onl/admin/authserver/apiScopeMan.json", params = "cmd=DELETE")
	public ResponseEntity<Void> deleteApiScope(ApiScopeUI apiScopeUI) {
		apiScopeManService.deleteApiScopeRelation(apiScopeUI);
		
		CommonCommand.builder()
								.name(CommonCommand.RELOAD_API_SCOPE_COMMAND)
								.args(new String[] {ReloadApiScopeCommand.COMMAND_TYPE_API_SCOPE, 
										apiScopeUI.getBzwkSvcKeyName(), apiScopeUI.getScopeId()})
								.build()
								.broadcast(agentUtilService);
		
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/admin/authserver/apiScopeMan.json", params = "cmd=LIST_COMBO")
	public ResponseEntity<Map<String, Object>> selectCombo() {
		
		List<Map<String, Object>> listApi = apiScopeManService.selectApiListCombo();
		List<Map<String, Object>> listScope = apiScopeManService.selectScopeListCombo();

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("apiRows", listApi);
		resultMap.put("scopeRows", listScope);

		return ResponseEntity.ok(resultMap);
	}
}