package sikd.sikd5.ctrl.simpatik;

import static com.github.adminfaces.starter.util.Utils.addDetailMessage;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
/**
 * @author  : agus
 * @tanggal : Jan 11, 2019
 */
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Specializes;
import javax.inject.Named;
import javax.naming.Context;
import sikd.sikd5.ctrl.template.TahunPemdaFilter;
import sikd.sikd5.sikd5bean.query.InfoPengiriman;
import sikd.sikd5.sikd5bean.query.MonitorSimpatikPerBulan;
import sikd.sikd5.sikd5bean.query.Periode;
import sikd.sikd5.sikd5bean.tabel.UploadFile;
import sikd.sikd5.sikd5bean.util.SIKDUtil;
import sikd.sikd5.sikd5bean.util.ServerUtil;
import sikd.sikd5.sikd5bean.util.SikdSessionUtil;
import sikd.sikd5.sikd5ejb.session.LraSessionBeanRemote;
import sikd.sikd5.sikd5ejb.session.PerkiraanBelanjaSessionBeanRemote;
import sikd.sikd5.sikd5ejb.session.PosisiKasSessionBeanRemote;
import sikd.sikd5.sikd5ejb.session.UploadFileSessionBeanRemote;

@Named
@SessionScoped
public class MonitorPerPemdaCtrl extends TahunPemdaFilter implements Serializable {
	private static final long serialVersionUID = 1L;
	List<MonitorSimpatikPerBulan> statusKirims = new ArrayList<MonitorSimpatikPerBulan>();
	UploadFileSessionBeanRemote uploadFileSes;
	LraSessionBeanRemote lraSes;
	PerkiraanBelanjaSessionBeanRemote pbSes;
	PosisiKasSessionBeanRemote pkSes;

	@PostConstruct
	public void init() throws Exception {
		super.init();
		statusKirims.clear();
		for (Periode p : SIKDUtil.getBulans()) {
			statusKirims.add(new MonitorSimpatikPerBulan(p.getBulanShort(), p.getBulanString()));
		}
		Context ctxOffice = ServerUtil.getOfficeContextV3();
		uploadFileSes = (UploadFileSessionBeanRemote) ctxOffice.lookup(SikdSessionUtil.UPLOAD_FILE_SESSION);
		lraSes = (LraSessionBeanRemote) ctxOffice.lookup(SikdSessionUtil.LRA_SESSION);
		pbSes = (PerkiraanBelanjaSessionBeanRemote) ctxOffice.lookup(SikdSessionUtil.PERKIRAAN_BELANJA_SESSION);
		pkSes = (PosisiKasSessionBeanRemote) ctxOffice.lookup(SikdSessionUtil.POSISI_KAS_SESSION);
	}

	public void getStatusKirim() {
		try {
			statusKirims.clear();
			for (Periode p : SIKDUtil.getBulans()) {
				statusKirims.add(new MonitorSimpatikPerBulan(p.getBulanShort(), p.getBulanString()));
			}
			List<UploadFile> uploadFiles = uploadFileSes.getAllUploadFiles(getSelectedPemda().getKodeSatker(), getSelectedYear().getTahun());
			List<InfoPengiriman> lraInfos = lraSes.getInfoPengirimanXmlPerBulan(getSelectedPemda().getKodeSatker(), getSelectedYear().getTahun());
			List<InfoPengiriman> pbInfos = pbSes.getInfoPengirimanXmlPerBulan(getSelectedPemda().getKodeSatker(), getSelectedYear().getTahun());
			System.out.println(pbInfos.size());
			List<InfoPengiriman> pkInfos = pkSes.getInfoPengirimanXmlPerBulan(getSelectedPemda().getKodeSatker(), getSelectedYear().getTahun());
			System.out.println(getSelectedPemda().getKodeSatker());
			for (MonitorSimpatikPerBulan mp : statusKirims) {
				String per = mp.getBulanShort() > 9 ? "" + mp.getBulanShort() : "0" + mp.getBulanShort();
				// File PDF
				for (UploadFile uf : uploadFiles) {
					// PDF perkiraan Belanja
					if (uf.getKodeikd().equals("PBB" + per)) {
						mp.setPbbp(uf.getTglupload());
					}
					// PDF Posisi Kas
					if (uf.getKodeikd().equals("PKB" + per)) {
						mp.setPkbp(uf.getTglupload());
					}
					// PDF SUbregistry
					if (uf.getKodeikd().equals("SUB" + per)) {
						mp.setSubp(uf.getTglupload());
					}
				}
				// XML Perkiraan Belanja
				for (InfoPengiriman pbInfo : pbInfos) {
					if (pbInfo.getPeriode() == mp.getBulanShort()) {
						mp.setPbbx(pbInfo.getTglPengiriman());
						break;
					}
				}
				// XML Posisi Kas
				for (InfoPengiriman pkInfo : pkInfos) {
					if (pkInfo.getPeriode() == mp.getBulanShort()) {
						mp.setPkbx(pkInfo.getTglPengiriman());
						break;
					}
				}
				// XML LRA
				for (InfoPengiriman lraInfo : lraInfos) {
					if (lraInfo.getPeriode() == mp.getBulanShort()) {
						mp.setLrax(lraInfo.getTglPengiriman());
						break;
					}
				}

			}
		} catch (Exception e) {
			addDetailMessage(e.getMessage());
			e.printStackTrace();
		}
	}

	public List<MonitorSimpatikPerBulan> getStatusKirims() {
		return statusKirims;
	}

	public void onPemdaChange() {
		super.onPemdaChange();
		getStatusKirim();
	}

	public void onRefresh() {
//		onPemdaChange();
		getStatusKirim();
	}
	

}
