/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sikd.ikonsol.ctrl;

import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.primefaces.event.ItemSelectEvent;
import org.primefaces.model.chart.Axis;
import org.primefaces.model.chart.AxisType;
import org.primefaces.model.chart.BarChartModel;
import org.primefaces.model.chart.ChartSeries;
import sikd.ikonsol.ejb.session.ChartEjbHome;
import sikd.ikonsol.model.Chart;

/**
 *
 * @author Imam Bhoim
 */
@ViewScoped
@Named(value = "chartViewBean")
public class ChartViewBeanCtrl implements Serializable {

    private BarChartModel barModel;
    private Chart ch;

    private List<Chart> list;

    @EJB
    ChartEjbHome chartEjbHome;

    @PostConstruct
    public void init() {

        createBarModels();

    }

    public void itemSelect(ItemSelectEvent event) {
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Item selected",
                "Item Index: " + event.getItemIndex() + ", Series Index:" + event.getSeriesIndex());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public BarChartModel getBarModel() {
        return barModel;
    }

    private BarChartModel initBarModel() {
        BarChartModel model = new BarChartModel();

        ch = new Chart();

        list = chartEjbHome.findAll();
        ChartSeries lra = new ChartSeries();
        ChartSeries lo = new ChartSeries();
        ChartSeries neracalap = new ChartSeries();
        ChartSeries neracatrial = new ChartSeries();
        for (Chart obj : list) {
            if (obj.getNama().equalsIgnoreCase("LRA")) {
                lra.setLabel("LRA");
                System.out.println("LRA " + obj.getTahun() + " " + obj.getJumlah());
                lra.set(obj.getTahun(), obj.getJumlah());
                
            } else if (obj.getNama().equalsIgnoreCase("LO")) {
                lo.setLabel("LO");
                System.out.println("LO " + obj.getTahun() + " " + obj.getJumlah());
                lo.set(obj.getTahun(), obj.getJumlah());
               
            } else if (obj.getNama().equalsIgnoreCase("Neraca Laporan")) {
                neracalap.setLabel("Neraca Laporan");
                System.out.println("Neraca Laporan " + obj.getTahun() + " " + obj.getJumlah());
                neracalap.set(obj.getTahun(), obj.getJumlah());
              
            } else if (obj.getNama().equalsIgnoreCase("Neraca Trial Balance")) {
                neracatrial.setLabel("Neraca Trial Balance");
                neracatrial.set(obj.getTahun(), obj.getJumlah());
 
            }
        }

//        girls.setLabel("LO");
//        girls.set("2004", 52);
//        girls.set("2005", 60);
//        girls.set("2006", 110);
//        girls.set("2007", 135);
//        girls.set("2008", 120);
//
//        girls2.setLabel("Neraca");
//        girls2.set("2004", 52);
//        girls2.set("2005", 60);
//        girls2.set("2006", 110);
//        girls2.set("2007", 135);
//        girls2.set("2008", 120);
        model.addSeries(lra);
        model.addSeries(lo);
        model.addSeries(neracalap);
        model.addSeries(neracatrial);
//        model.addSeries(girls);
//        model.addSeries(girls2);

        return model;
    }

    private void createBarModels() {
        createBarModel();
    }

    private void createBarModel() {
        barModel = initBarModel();

        barModel.setTitle("Overview Konsolidasi LKPD");
        barModel.setLegendPosition("ne");

        Axis xAxis = barModel.getAxis(AxisType.X);
        xAxis.setLabel("Jenis Laporan");

        Axis yAxis = barModel.getAxis(AxisType.Y);
        yAxis.setLabel("Jumlah Data");
        //yAxis.setMin(0);
        //yAxis.setMax();
    }
}
