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

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
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.bean.ViewScoped;
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
@ViewScoped
public class NasionalMonitoring implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Map<String,Object> dummyLoad = new HashMap<>();
	public static final Logger LOGGER = LoggerFactory.getLogger(NasionalMonitoring.class);
	
	private List<Map<String, Object>> listMonitor = new ArrayList<>();

	private int tahun;
	
	public NasionalMonitoring() {
	    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("listMonitor");
	}
	
	@PostConstruct
    public void init() {
		listMonitor = new ArrayList<>();
		listMonitor = ClientsUtil.callWsListResponse(
				"/monitoring/nasional", dummyLoad, HttpMethod.POST,
				"tahun=" + getSessionTahunAnggaran());
	}
	
	public void reload() throws IOException {
		init();
	    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
	    ec.redirect(((HttpServletRequest) ec.getRequest()).getRequestURI());
	}
	
	private void destroySession() {
		List<Map<String, Object>> listMonitors = new ArrayList<>(listMonitor);
	    //rmaModel = newRMAModel;
	    this.setListMonitor(listMonitors);

	    FacesContext context = FacesContext.getCurrentInstance();
	    HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
	    HttpSession httpSession = request.getSession(false);
	    httpSession.setAttribute("listMonitor", this);
	}
	
	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>> getListMonitor() {
		
		return listMonitor;
	}

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

	public int getTahun() throws Exception {
		reload();
		System.out.println("Panggil tahun =============");
		return getSessionTahunAnggaran();
	}

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