package sikd.sikd5.ctrl.simpatik;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import sikd.sikd5.model.combo.MonthYearCombo;
import sikd.sikd5.model.combo.PemdaCombo;
import sikd.sikd5.sikd5bean.query.MonitorSimpatikPemdaKoneksi;
import sikd.sikd5.sikd5bean.tabel.TahunAnggaran;
import sikd.sikd5.sikd5ejb.session.LraSessionBeanRemote;
import sikd.sikd5.sikd5ejb.session.SettingsSessionBeanRemote;

/**
 * @author  : Soraturaes
 */

@ManagedBean(name = "monitorPemdaKoneksiMBean")
@ViewScoped
public class MonitorPemdaKoneksiMBean implements Serializable {
        MonthYearCombo monthYearCombo;
	List<MonitorSimpatikPemdaKoneksi> statusKirims = new ArrayList();
        @EJB
        LraSessionBeanRemote lraSession;
        @EJB
        SettingsSessionBeanRemote settingSession;

	@PostConstruct
	public void init() {
            try {
                monthYearCombo = new MonthYearCombo("idPanel", true, "idComboPeriode", null, false, null, "panelrinci", "idComboYear", null, new ArrayList());
                monthYearCombo.setAlign("center");
                monthYearCombo.setLabelStyle("padding: 10px;");
                monthYearCombo.setComboYearStyle("width: 210px;");
                monthYearCombo.setYears(settingSession.getTahuns());
            if (monthYearCombo.getYears() != null && !monthYearCombo.getYears().isEmpty()) {
                monthYearCombo.setSelectedYear(monthYearCombo.getYears().get(0));
                GregorianCalendar gc = (GregorianCalendar)Calendar.getInstance();
                gc.setTime(new Date());
                int yy = gc.get(Calendar.YEAR)+1;
                if(monthYearCombo.getYears().get(0).getTahun() < yy ){
                    int sisa = yy - monthYearCombo.getYears().get(0).getTahun();
                    int ll = monthYearCombo.getYears().get(0).getTahun();
                    int jp = monthYearCombo.getYears().get(0).getJumlahPemda();
                    for (int i = 0; i < sisa; i++) {
                        monthYearCombo.getYears().add(0,new TahunAnggaran((short)++ll, (short)jp));
                    }
                    monthYearCombo.setSelectedYear(monthYearCombo.getYears().get(1));
                }
                
            }
                statusKirims.clear();
            } catch (Exception ex) {
                Logger.getLogger(MonitorPemdaKoneksiMBean.class.getName()).log(Level.SEVERE, null, ex);
            }
	}

	public void getStatusKirim() {
		try {
                    String f1  = "color: green; font-size: 16px;";
                    String f2  = "color: red; font-size: 16px;";
			statusKirims.clear();
			List<MonitorSimpatikPemdaKoneksi> lraInfos = lraSession.getMonitorPemdaKoneksi(monthYearCombo.getSelectedYear().getTahun());
			for (MonitorSimpatikPemdaKoneksi mp : lraInfos) {
                            if( mp.getApbd()!=null ) { 
                                mp.setImgApbd("V"); 
                                mp.setStyleApbd(f1); 
                            }
                            else {
                                mp.setImgApbd("X");
                                mp.setStyleApbd(f2);
                            } 
                            if( mp.getLra()!=null ) { 
                                mp.setImgLra("V"); 
                                mp.setStyleLra(f1);
                            }
                            else {
                                mp.setImgLra("X");
                                mp.setStyleLra(f2);
                            } 
                            if( mp.getDth()!=null ) { 
                                mp.setImgDth("V"); 
                                mp.setStyleDth(f1);
                            }
                            else {
                                mp.setImgDth("X");
                                mp.setStyleDth(f2);
                            } 
                            statusKirims.add(mp);
			}
		} catch (Exception e) {
//			addDetailMessage(e.getMessage());
			e.printStackTrace();
		}
	}

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

        
        public void onYearChange() {
//            System.out.println("tahun");
//            if(monthYearCombo.getSelectedYear()!=null)
//                System.out.println("thn "+monthYearCombo.getSelectedYear().getTahunAsString());
        }

	public void onRefresh() {
		getStatusKirim();
	}

    public MonthYearCombo getMonthYearCombo() {
        return monthYearCombo;
    }

    public void setMonthYearCombo(MonthYearCombo monthYearCombo) {
        this.monthYearCombo = monthYearCombo;
    }
    public String getUpdateRefresh(){
        return "panelrinci";
    }

}
