package VistaControlador;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimplePdfExporterConfiguration;
import net.sf.jasperreports.view.JasperViewer;

public class ControladorInforme implements ActionListener{
	VistaInformes vista;
	ModeloInformes modelo;
	
	public ControladorInforme(VistaInformes vista, ModeloInformes modelo) {
		this.vista = vista;
		this.modelo = modelo;
		for(String anios:this.modelo.anios()) {
			this.vista.opciones1_2.addItem(anios);
		}
		this.iniciarControlador();
	}
	
	private void iniciarControlador() {
		this.vista.exportarInforme1.addActionListener(this);
		this.vista.exportarInforme1.setActionCommand("exportar informe 1");
		this.vista.mostrarInforme1.addActionListener(this);
		this.vista.mostrarInforme1.setActionCommand("mostrar informe 1");
		this.vista.exportarInforme2.addActionListener(this);
		this.vista.exportarInforme2.setActionCommand("exportar informe 2");
		this.vista.mostrarInforme2.addActionListener(this);
		this.vista.mostrarInforme2.setActionCommand("mostrar informe 2");
	}

	@Override
	public void actionPerformed(ActionEvent ev) {
		switch(ev.getActionCommand()) {
			case "mostrar informe 1":{
				JasperPrint jasperPrint=null;
                try {
                        Map<String, Object> params = new HashMap<String, Object>();
                        params.put("Order", this.vista.opciones1_1.getSelectedItem());
                        params.put("Year", this.vista.opciones1_2.getSelectedItem());
                		jasperPrint=JasperFillManager.fillReport("Cherry.jasper",params,modelo.controlador);
                        JasperViewer jasperViewver=new JasperViewer(jasperPrint,false);
                        jasperViewver.setVisible(true);
                } catch (JRException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
				break;
			}
			
			case "exportar informe 1":{
				JasperPrint jasperPrint=null;
                try {
                        Map<String, Object> params = new HashMap<String, Object>();
                        params.put("Order", this.vista.opciones1_1.getSelectedItem());
                        params.put("Year", this.vista.opciones1_2.getSelectedItem());
                		jasperPrint=JasperFillManager.fillReport("Cherry.jasper",params,modelo.controlador);
                        JRPdfExporter exp=new JRPdfExporter();
                        exp.setExporterInput(new SimpleExporterInput(jasperPrint));
                        exp.setExporterOutput(new SimpleOutputStreamExporterOutput("Informe1.pdf"));
                        SimplePdfExporterConfiguration conf=new SimplePdfExporterConfiguration();
                        exp.setConfiguration(conf);
                        exp.exportReport();
                } catch (JRException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
				break;
			}
			case "mostrar informe 2":{
				JasperPrint jasperPrint=null;
                try {
                        Map<String, Object> params = new HashMap<String, Object>();
                        params.put("Order",this.vista.opciones2.getSelectedItem());
                		jasperPrint=JasperFillManager.fillReport("Graficos_gastos.jasper",params,modelo.controlador);
                        JasperViewer jasperViewver=new JasperViewer(jasperPrint,false);
                        jasperViewver.setVisible(true);
                } catch (JRException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
				break;
			}
			
			case "exportar informe 2":{
				JasperPrint jasperPrint=null;
                try {
                        Map<String, Object> params = new HashMap<String, Object>();
                        params.put("Order",this.vista.opciones2.getSelectedItem());
                		jasperPrint=JasperFillManager.fillReport("Graficos_gastos.jasper",params,modelo.controlador);
                        JRPdfExporter exp=new JRPdfExporter();
                        exp.setExporterInput(new SimpleExporterInput(jasperPrint));
                        exp.setExporterOutput(new SimpleOutputStreamExporterOutput("Informe2.pdf"));
                        SimplePdfExporterConfiguration conf=new SimplePdfExporterConfiguration();
                        exp.setConfiguration(conf);
                        exp.exportReport();
                } catch (JRException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
				break;
			}
		}
		
	}
}
