package VistaControlador;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import Logica.Gestion;
import Logica.Meses;

public class Menu extends JFrame{
	protected JPanel panelCentral;
	protected JTabbedPane pestania;
	protected VistaAniadirVisualizar ingresos;
	protected VistaAniadirVisualizar gastos;
	protected VistaPanelLateral panel;
	Meses meses;
	ArrayList<VistaAniadirVisualizar> pestanias;
	ArrayList<ControladorAniadirVisualizar> controladores;
	protected Gestion datosGastos;
	protected Gestion datosIngresos;
	public Menu() {
		this.pestania = new JTabbedPane();
		this.cargarGestiones(LocalDate.now().getYear(), LocalDate.now().getMonth());
		this.cargarPestanias();
		this.setLayout(new GridBagLayout());
		GridBagConstraints constrain = new GridBagConstraints();
		this.panel = new VistaPanelLateral(constrain, this.meses);
		this.panelCentral = new JPanel();
		
		constrain.fill = GridBagConstraints.VERTICAL;
		constrain.gridx = 0;
		constrain.gridy = 0;
		constrain.weightx = 2;
		this.panelCentral.add(pestania,constrain);
		getContentPane().add(pestania);
		setTitle("Titulo");
		setSize(new Dimension(420,320));
		setDefaultCloseOperation(3);
		setLocationRelativeTo(null);
		ControladorPanelLateral controlador3 = new ControladorPanelLateral(this.panel, this);
		this.pestania.addChangeListener((ChangeListener)->{
			if(this.pestania.getTabCount()<0) {
				this.panel.actualizarDatos(meses.getGestionesActuales().get(this.pestania.getSelectedIndex()));
			}
		});
		this.add(this.panel);
	}
	
	private void iniciarMes(int anio, Month mes) {
		this.pestania.removeAll();
		meses.aniadirGestion("Ingresos", anio, mes, true);
		meses.aniadirGestion("Gastos", anio, mes, false);
		this.pestanias=new ArrayList<VistaAniadirVisualizar>();
		this.pestanias.add(new VistaAniadirVisualizar(this,meses.getGestionesActuales().get(0)));
		this.pestanias.add(new VistaAniadirVisualizar(this,meses.getGestionesActuales().get(1)));
		this.controladores=new ArrayList<ControladorAniadirVisualizar>();
		this.controladores.add(new ControladorAniadirVisualizar(this.pestanias.get(0)));
		this.controladores.add(new ControladorAniadirVisualizar(this.pestanias.get(1)));
		this.cargarPestanias();
	}
	
	void cargarGestiones(int anio, Month mes) {
		if(this.meses==null) {
			this.meses=new Meses();
		}
		this.meses.elegirMes(anio, mes);
		if(this.meses.getGestionesActuales().size() == 0) {
			this.iniciarMes(anio, mes);
		}else {
			this.cargarMes();
		}
	}
	
	private void cargarMes() {
		this.pestania.removeAll();
		this.pestanias.clear();
		this.controladores.clear();
		for(Gestion gestion:this.meses.getGestionesActuales()) {
			VistaAniadirVisualizar vista = new VistaAniadirVisualizar(this, gestion);
			ControladorAniadirVisualizar controlador = new ControladorAniadirVisualizar(vista);
			vista.iniciarGestion();
			this.pestanias.add(vista);
			this.controladores.add(controlador);
		}
		this.cargarPestanias();
	}
	
	void cargarPestanias() {
		this.pestania.removeAll();
		for(VistaAniadirVisualizar vista:this.pestanias) {
			this.pestania.addTab(vista.getName(),vista);
		}
	}
	
	void aniadirGestion(String nombre, boolean sumaOResta) {
		Gestion gestion=this.meses.aniadirGestion(nombre, VistaPanelLateral.getDate().getYear(), VistaPanelLateral.getDate().getMonth(), sumaOResta);
		VistaAniadirVisualizar vista = new VistaAniadirVisualizar(this, gestion);
		this.pestanias.add(vista);
		this.controladores.add(new ControladorAniadirVisualizar(vista));
		this.pestania.addTab(vista.getName(),vista);
	}
	
}