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.ManagedProperty;
import javax.faces.bean.ViewScoped;
import org.primefaces.model.chart.Axis;
import org.primefaces.model.chart.AxisType;
import org.primefaces.model.chart.BarChartModel;
import org.primefaces.model.chart.LegendPlacement;
import org.primefaces.model.chart.LineChartSeries;
import sikd.sikd5.filter.DateMarginFilter;
import sikd.sikd5.model.MonitorDataTerkirimObj;
import sikd.sikd5.model.combo.PemdaCombo;
import sikd.sikd5.security.LoginMB;
import sikd.sikd5.sikd5bean.query.MonitorSimpatikDataKirimPemdaChart;
import sikd.sikd5.sikd5bean.tabel.Pemda;
import sikd.sikd5.sikd5bean.tabel.TahunAnggaran;
import sikd.sikd5.sikd5bean.tabel.mgr.TUserAccount;
import sikd.sikd5.sikd5ejb.session.LraSessionBeanRemote;
import sikd.sikd5.sikd5ejb.session.SettingsSessionBeanRemote;
import sikd.sikd5.sikd5ejb.session.login.LoginSessionBeanRemote;

/**
 * @author : Soraturaes
 */
@ManagedBean(name = "monitorDataKirimMBean")
@ViewScoped
public class MonitorDataKirimMBean implements Serializable {
    protected DateMarginFilter dateMarginFilter;
    PemdaCombo pemdaCombo;
    private BarChartModel chartModel;
    @EJB
    LraSessionBeanRemote lraSession;
    @EJB
    SettingsSessionBeanRemote settingSession;
    
    @ManagedProperty(value="#{loginMB}")
    LoginMB loginMB;
    
    List<MonitorDataTerkirimObj> monitorDatas;
    short maxTahun;

    @PostConstruct
    public void init() {
        try {
            List<TahunAnggaran> tahuns = settingSession.getTahuns();
            if(tahuns!=null && !tahuns.isEmpty()){
                maxTahun = tahuns.get(0).getTahun();
            }
            pemdaCombo = new PemdaCombo(true, false, "", "Pemerintah Daerah");
            pemdaCombo.setAlign("center");
            dateMarginFilter = new DateMarginFilter();
            Date dd = new Date();            
            GregorianCalendar gc = (GregorianCalendar)Calendar.getInstance();
            gc.setTime(dd);
            short yy = (short) gc.get(Calendar.YEAR);
            
            pemdaCombo.setPemdas(loginMB.getPemdaByTahuns(yy));
            Pemda p = new Pemda(0, "", "", "", "Nasional", "", (short) 0);
            if (pemdaCombo.getPemdas() != null && !pemdaCombo.getPemdas().isEmpty()) {
                pemdaCombo.getPemdas().add(0, p);
                pemdaCombo.setSelectedPemda(pemdaCombo.getPemdas().get(0));
            } else {
                pemdaCombo.setPemdas(loginMB.getPemdaByTahuns(maxTahun));
                if( pemdaCombo.getPemdas() != null )
                    pemdaCombo.getPemdas().add(0, p);
                else
                    pemdaCombo.addPemda( p);
                pemdaCombo.setSelectedPemda(p);
            }

            TUserAccount uac = loginMB.getTuserAccount();
            if (uac != null && uac.getPemda() != null) {
                pemdaCombo.setSelectedPemda(uac.getPemda());
                pemdaCombo.setView(false);
            }
            
            monitorDatas= new ArrayList();
            chartModel = new BarChartModel();
//            initChart();
        } catch (Exception ex) {
            Logger.getLogger(MonitorDataKirimMBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    protected void initChart() {
        try {
            monitorDatas.clear();            
            monitorDatas.add(new MonitorDataTerkirimObj("LRA"));
            monitorDatas.add(new MonitorDataTerkirimObj("APBD"));
            monitorDatas.add(new MonitorDataTerkirimObj("DTH"));
            String[] labels = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
            LineChartSeries lineApbd = initLineSerries(labels);
            LineChartSeries lineLra = initLineSerries(labels);
            LineChartSeries lineDth = initLineSerries(labels);

            chartModel = new BarChartModel();
            chartModel.setShowPointLabels(true);
            chartModel.setShowDatatip(true);
            chartModel.setAnimate(true);
            chartModel.setLegendPosition("w");
            chartModel.setLegendPlacement(LegendPlacement.INSIDE);
            chartModel.setExtender("removeLegend");

            List<MonitorSimpatikDataKirimPemdaChart> sd = new ArrayList();
            Date tanggal = dateMarginFilter.getDateStart();
            if(pemdaCombo.getSelectedPemda() == null)
                sd = lraSession.getMonitorDataKirimChartNasional(tanggal);
            else{
                if( pemdaCombo.getSelectedPemda().getId()<= 0 || pemdaCombo.getSelectedPemda().getKodeSatker() == null || pemdaCombo.getSelectedPemda().getKodeSatker().trim().equals(""))
                    sd = lraSession.getMonitorDataKirimChartNasional(tanggal);
                else
                    sd = lraSession.getMonitorDataKirimChartPemda(tanggal, pemdaCombo.getSelectedPemda().getKodeSatker());
            }
            long max = 0;
            if (sd != null && !sd.isEmpty()) {
                lineApbd = new LineChartSeries();
                lineLra = new LineChartSeries();
                lineDth = new LineChartSeries();
                for (int i = 0; i < sd.size(); i++) {
                    lineApbd.set(""+sd.get(i).getNo(), sd.get(i).getJumlahApbd());
                    if (max < sd.get(i).getJumlahApbd()) max = sd.get(i).getJumlahApbd();
                    lineLra.set(""+sd.get(i).getNo(), sd.get(i).getJumlahLra());
                    if (max < sd.get(i).getJumlahLra()) max = sd.get(i).getJumlahLra();
                    lineDth.set(""+sd.get(i).getNo(), sd.get(i).getJumlahDth());
                    if (max < sd.get(i).getJumlahDth()) max = sd.get(i).getJumlahDth();
                    settingMonitorDatas(i, sd.get(i));
                }
            }

            String strm = "" + max;
            if (max > 0) {
                int pa = strm.trim().length() - 1;
                long kal = 1;
                for (int i = 0; i < pa; i++) {
                    kal = kal * 10;
                }
                if (max % kal != 0) {
                    long ll = Long.valueOf(strm.trim().charAt(0) + "");
                    max = (ll + 1) * kal;
                }
            }
            else{
                max = max+100;
            }
//max=max+(max/10);

            lineApbd.setLabel("APBD");
            lineLra.setLabel("LRA");
            lineDth.setLabel("DTH");
            
            chartModel.addSeries(lineLra);
            chartModel.addSeries(lineApbd);
            chartModel.addSeries(lineDth);

            Axis yAxis = chartModel.getAxis(AxisType.Y);
            yAxis.setMin(0);
            yAxis.setMax(max);
            yAxis.setTickCount(11);

            Axis xAxis = chartModel.getAxis(AxisType.X);
            xAxis.setMin(0);
            xAxis.setMax(10);
            xAxis.setTickCount(1);
//            xAxis.setTickInterval("10");
//            xAxis.setTickAngle(50);
        } catch (Exception ex) {
            Logger.getLogger(MonitorDataKirimMBean.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
    }
    
    protected void settingMonitorDatas(int i, MonitorSimpatikDataKirimPemdaChart obj){
        if( i== 0 ){
            monitorDatas.get(1).setH(obj.getJumlahLra());
            monitorDatas.get(0).setH(obj.getJumlahApbd());
            monitorDatas.get(2).setH(obj.getJumlahDth());
        }
        else if( i== 1 ){
            monitorDatas.get(1).setH1(obj.getJumlahLra());
            monitorDatas.get(0).setH1(obj.getJumlahApbd());
            monitorDatas.get(2).setH1(obj.getJumlahDth());
        }
        else if( i== 2 ){
            monitorDatas.get(1).setH2(obj.getJumlahLra());
            monitorDatas.get(0).setH2(obj.getJumlahApbd());
            monitorDatas.get(2).setH2(obj.getJumlahDth());
        }
        else if( i== 3 ){
            monitorDatas.get(1).setH3(obj.getJumlahLra());
            monitorDatas.get(0).setH3(obj.getJumlahApbd());
            monitorDatas.get(2).setH3(obj.getJumlahDth());
        }
        else if( i== 4 ){
            monitorDatas.get(1).setH4(obj.getJumlahLra());
            monitorDatas.get(0).setH4(obj.getJumlahApbd());
            monitorDatas.get(2).setH4(obj.getJumlahDth());
        }
        else if( i== 5 ){
            monitorDatas.get(1).setH5(obj.getJumlahLra());
            monitorDatas.get(0).setH5(obj.getJumlahApbd());
            monitorDatas.get(2).setH5(obj.getJumlahDth());
        }
        else if( i== 6 ){
            monitorDatas.get(1).setH6(obj.getJumlahLra());
            monitorDatas.get(0).setH6(obj.getJumlahApbd());
            monitorDatas.get(2).setH6(obj.getJumlahDth());
        }
        else if( i== 7 ){
            monitorDatas.get(1).setH7(obj.getJumlahLra());
            monitorDatas.get(0).setH7(obj.getJumlahApbd());
            monitorDatas.get(2).setH7(obj.getJumlahDth());
        }
        else if( i== 8 ){
            monitorDatas.get(1).setH8(obj.getJumlahLra());
            monitorDatas.get(0).setH8(obj.getJumlahApbd());
            monitorDatas.get(2).setH8(obj.getJumlahDth());
        }
        else if( i== 9 ){
            monitorDatas.get(1).setH9(obj.getJumlahLra());
            monitorDatas.get(0).setH9(obj.getJumlahApbd());
            monitorDatas.get(2).setH9(obj.getJumlahDth());
        }
    }
    
    public LineChartSeries initLineSerries(String[] labels) {
        LineChartSeries ls = new LineChartSeries();
        for (int i = 0; i < labels.length; i++) {
            ls.set(labels[i], i*10);
        }
        return ls;
    }

    public BarChartModel getChartModel() {
        return chartModel;
    }

    public List<MonitorDataTerkirimObj> getMonitorDatas() {
        return monitorDatas;
    }

    public LoginMB getLoginMB() {
        return loginMB;
    }

    public void setLoginMB(LoginMB loginMB) {
        this.loginMB = loginMB;
    }

    public DateMarginFilter getDateMarginFilter() {
        return dateMarginFilter;
    }

    public void setDateMarginFilter(DateMarginFilter dateMarginFilter) {
        this.dateMarginFilter = dateMarginFilter;
    }

    public PemdaCombo getPemdaCombo() {
        return pemdaCombo;
    }

    public void setPemdaCombo(PemdaCombo pemdaCombo) {
        this.pemdaCombo = pemdaCombo;
    }

    public short getMaxTahun() {
        return maxTahun;
    }

    public void setMaxTahun(short maxTahun) {
        this.maxTahun = maxTahun;
    }
    public String getUpdateRefresh(){
        return "chart table0";
    }
    
    public void onRefresh() {
        initChart();
    }
    public void onPemdaChange(){
        
    }

}
