package com.sh.djpk.econsole.rest.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.xml.transform.OutputKeys;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.sh.djpk.econsole.rest.service.MonitoringSvc;
import com.sh.djpk.share.model.RestResponse;
import com.sh.djpk.share.util.JsonUtil;

@RestController
@RequestMapping("/monitoring")
public class MonitoringCtl {

	public static final Logger LOGGER = LoggerFactory
			.getLogger(MonitoringCtl.class);
	
	@Autowired
	MonitoringSvc monitoringSvc;
	
	@RequestMapping(value = "/nasional", method = RequestMethod.POST)
	public RestResponse getMonitoringNasional(
			@RequestParam(value = "tahun", required = true) int tahun) throws JsonParseException, JsonMappingException, IOException {
		LOGGER.info("getMonitoringNasional");
		RestResponse response = new RestResponse();
		try {
//			new RestResponse(RestResponse.OK_REST_STATUS,
//					"OK", null);
			response.setContents(monitoringSvc.getNationalMonitoring(tahun));
			LOGGER.trace("response={}", JsonUtil.getJson(response));
		}catch(Exception e) {
			response.setStatus(RestResponse.ERROR_REST_STATUS);
			response.setMessage("ERROR");
			response.setContents(new HashMap<>());
			LOGGER.info(e.toString());
			e.printStackTrace();
		}
		return response;
	}
	
	@RequestMapping(value = "/province", method = RequestMethod.POST)
	public RestResponse getMonitoringProvinsi(
			@RequestParam(value = "tahun", required = true) int tahun,
			@RequestParam(value = "prov_id", required = false) String prov_id) throws JsonParseException, JsonMappingException, IOException {
		LOGGER.info("get monitoring Provinsi");
		RestResponse response = new RestResponse();
		if(prov_id == null) {
			prov_id = "00";
		}
		try {
			response.setContents(monitoringSvc.getHeaderProvinceMonitoring(tahun, prov_id));
			LOGGER.trace("response={}", JsonUtil.getJson(response));
		}catch(Exception e) {
			response.setStatus(RestResponse.ERROR_REST_STATUS);
			response.setMessage("ERROR");
			response.setContents(new HashMap<>());
			LOGGER.info(e.toString());
			e.printStackTrace();
		}
		return response;
	}
	
	@RequestMapping(value = "/perProvince", method = RequestMethod.POST)
	public RestResponse getMonitoringPerProvinsi(
			@RequestParam(value = "tahun", required = true) int tahun,
			@RequestParam(value = "prov_id", required = true) String prov_id) throws JsonParseException, JsonMappingException, IOException {
		LOGGER.info("get monitoring per Provinsi");
		RestResponse response = new RestResponse();
		if(prov_id == null) {
			prov_id = "00";
		}
		try {
			response.setContents(monitoringSvc.getDetailProvinceMonitoring(tahun, prov_id));
			LOGGER.trace("response={}", JsonUtil.getJson(response));
		}catch(Exception e) {
			response.setStatus(RestResponse.ERROR_REST_STATUS);
			response.setMessage("ERROR");
			response.setContents(new HashMap<>());
			LOGGER.info(e.toString());
			e.printStackTrace();
		}
		return response;
	}
	
	@RequestMapping(value = "/pemda", method = RequestMethod.POST)
	public RestResponse getPemdaMonitoring(
			@RequestParam(value = "tahun", required = true) int tahun,
			@RequestParam(value = "pemda_id", required = true) String pemda_id) throws JsonParseException, JsonMappingException, IOException {
		LOGGER.info("get monitoring pemda");
		RestResponse response = new RestResponse();
		if(pemda_id == null) {
			pemda_id = "00";
		}
		try {
			response.setContents(monitoringSvc.getPemdaMonitoring(tahun, pemda_id));
			LOGGER.trace("response={}", JsonUtil.getJson(response));
		}catch(Exception e) {
			response.setStatus(RestResponse.ERROR_REST_STATUS);
			response.setMessage("ERROR");
			response.setContents(new HashMap<>());
			LOGGER.info(e.toString());
			e.printStackTrace();
		}
		return response;
	}
	
}
