package VistaControlador;

import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.Year;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.Month;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import com.github.lgooddatepicker.components.DatePicker;
import com.github.lgooddatepicker.optionalusertools.DateChangeListener;
import com.github.lgooddatepicker.zinternaltools.DateChangeEvent;
import com.github.lgooddatepicker.zinternaltools.DemoPanel;

import Logica.Gestion;
import Logica.Transaccion;
public class ControladorPanelLateral implements ActionListener, DateChangeListener{
	private VistaPanelLateral vista;
	private java.time.Month mes;
	private int anio;
	private Menu menu;
	
	/**
	 * Controlador del panel lateral que gestionara los datos
	 * 
	 * @param vista	Vista que usa el controlador
	 * @param menu	Menu que contiene la vista que usa este controlador
	 */
	public ControladorPanelLateral(VistaPanelLateral vista, Menu menu) {
		this.vista = vista;
		this.menu = menu;
		this.aniadirElementos();
	}
	
	public void actionPerformed(ActionEvent e) {
		switch(e.getActionCommand()) {
			case "Mostrar estadisticas":{
				
				XYSeriesCollection dataset = new XYSeriesCollection();
				for(Gestion gestion:this.menu.meses.getGestionesActuales()) {
					XYSeries serie = new XYSeries(gestion.getNombre());
					for(Transaccion transaccion:gestion.getElementos()) {
						serie.add(transaccion.getDia().getDayOfMonth(),transaccion.getDinero());
					}
					dataset.addSeries(serie);
				}
				
				JFreeChart chart = ChartFactory.createXYLineChart(this.menu.meses.getNombre(), "Dias", "Gastos", dataset);
				ChartFrame frame = new ChartFrame("Estadisricas", chart);
				frame.setVisible(true);
				frame.setSize(700,500);
				break;
			}
			
			case "Aniadir una nueva gestion":{
				String nombre = JOptionPane.showInputDialog("Introduce un nuevo gasto o ingreso");
				if(nombre == null) return;
				if(nombre.equals("")) {
					JOptionPane.showMessageDialog(null, "Debe introducir algo", "error", JOptionPane.WARNING_MESSAGE);
					return;
				}
				String sumaOResta = JOptionPane.showInputDialog("Son gastos?");
				if(sumaOResta == null) return;
				if(sumaOResta.equals("")) {
					JOptionPane.showMessageDialog(null, "Debe introducir algo", "error", JOptionPane.WARNING_MESSAGE);
					return;
				}
				VistaAniadirVisualizar vistaA;
				if(sumaOResta.equalsIgnoreCase("si")) {
					this.menu.aniadirGestion(nombre, false);
					
				}else if(sumaOResta.equalsIgnoreCase("no")) {
					this.menu.aniadirGestion(nombre, true);
				}else {
					JOptionPane.showMessageDialog(null, "Debe introducir si o no", "error", JOptionPane.WARNING_MESSAGE);
					return;
				}
				break;
			}
			
			case "Eliminar Transacciones":{
				this.menu.pestanias.get(this.menu.pestania.getSelectedIndex()).eliminarDeseleccionados();
			}
		}
		
	}
	
	/**
	 * Aniade los listeners a la lista de objetos
	 */
	private void aniadirElementos() {
		this.vista.mostrarEstadisticas.addActionListener(this);
		this.vista.mostrarEstadisticas.setActionCommand("Mostrar estadisticas");
		this.vista.aniadirGestion.addActionListener(this);
		this.vista.aniadirGestion.setActionCommand("Aniadir una nueva gestion");
		if(VistaPanelLateral.elegirMes.getDateChangeListeners().size() == 0) {
			VistaPanelLateral.elegirMes.addDateChangeListener(this);
			this.mes=VistaPanelLateral.elegirMes.getDate().getMonth();
			this.anio=VistaPanelLateral.elegirMes.getDate().getYear();
		}
		this.vista.eliminarTransaccion.addActionListener(this);
		this.vista.eliminarTransaccion.setActionCommand("Eliminar Transacciones");
	}

	public void dateChanged(DateChangeEvent arg0) {
		if(this.mes!=VistaPanelLateral.elegirMes.getDate().getMonth() || 
				this.anio!=VistaPanelLateral.elegirMes.getDate().getYear()) {
			this.menu.cargarGestiones(VistaPanelLateral.elegirMes.getDate().getYear(), VistaPanelLateral.elegirMes.getDate().getMonth());
			this.mes=VistaPanelLateral.elegirMes.getDate().getMonth();
			this.anio=VistaPanelLateral.elegirMes.getDate().getYear();
		}
		
	}

}