package com.sh.djpk.econsole.ui.controller.monitoring;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sh.djpk.econsole.ui.controller.HeaderTemplate;
import com.sh.djpk.share.model.RestResponse.HttpMethod;
import com.sh.djpk.share.util.ClientsUtil;
@ManagedBean
@SessionScoped
public class PemdaMonitoring implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Map<String,Object> dummyLoad = new HashMap<>();
	public static final Logger LOGGER = LoggerFactory.getLogger(PemdaMonitoring.class);
	int tahun;
	
	List<Map<String, Object>> listHeadMonitor;
	
	@PostConstruct
    public void init() {
		listHeadMonitor = ClientsUtil.callWsListResponse(
				"/monitoring/pemda", dummyLoad, HttpMethod.POST,
				"tahun=" + getSessionTahunAnggaran(), "pemda_id=" + "00");
		sessionPrint();
	}
	
	@SuppressWarnings("unchecked")
	private void sessionPrint() {
			Map<String, Object> userSession = (Map<String, Object>) getSession().getAttribute("user_login");
			System.out.println("Print Session");
			System.out.println(userSession);
			
	}
	
	HttpSession getSession(){
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext()
				.getRequest();
		HttpSession session = request.getSession(true);
		return session;
	}
	
	public int getSessionTahunAnggaran() {
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();
		HttpSession session = request.getSession(true);
		HeaderTemplate hdrT = (HeaderTemplate) session
				.getAttribute("headerTemplate");
		int i = hdrT.getTahunAnggaran();
		return i;
	}

	public List<Map<String, Object>> getListHeadMonitor() {
		return listHeadMonitor;
	}

	public void setListHeadMonitor(List<Map<String, Object>> listHeadMonitor) {
		this.listHeadMonitor = listHeadMonitor;
	}

	public void reload() throws IOException {
		init();
	    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
	    ec.redirect(((HttpServletRequest) ec.getRequest()).getRequestURI());
	}
	
	public int getTahun() throws Exception {
		reload();
		return getSessionTahunAnggaran();
	}

	public void setTahun(int tahun) {
		this.tahun = tahun;
	}
}
