package Logica;

import java.time.Month;
import java.util.ArrayList;

public class Meses {
private ArrayList<Mes> meses;
	private int mesActual=0;
	
	public Meses() {
		this.meses = new ArrayList<Mes>();
	}
	
	public Gestion aniadirGestion(String nombre, int anio, Month mes, boolean isPositivo) {
		Gestion ret;
		try {
			for(Mes mesSelect:this.meses) {
				if(mesSelect.getMes().equals(mes) && mesSelect.getAnio() == anio){
					ret = new Gestion(nombre, isPositivo);
					mesSelect.getGestiones().add(ret);
					return ret;
				}
			}
		}catch (NullPointerException e) {
			ArrayList<Gestion> gestiones=new ArrayList<Gestion>();
			ret = new Gestion(nombre, isPositivo);
			gestiones.add(ret);
			this.meses.add(new Mes(gestiones,anio,mes));
			return ret;
		}
		ArrayList<Gestion> gestiones=new ArrayList<Gestion>();
		ret = new Gestion(nombre, isPositivo);
		gestiones.add(ret);
		this.meses.add(new Mes(gestiones,anio,mes));
		return ret;
	}
	
	public void aniadirTransaccion(Transaccion transaccion, String nombre, boolean isPositivo) {
		for(Mes mes:this.meses) {
			if(transaccion.getDia().getMonth().equals(mes.getMes()) &&
					transaccion.getDia().getYear() == mes.getAnio()){
				mes.aniadirTransaccion(transaccion, nombre, isPositivo);
				return;
			}
		}
		this.meses.add(new Mes(new ArrayList<Gestion>(),transaccion.getDia().getYear(),
				transaccion.getDia().getMonth()));
		this.meses.get(this.meses.size()-1).aniadirTransaccion(transaccion, nombre, isPositivo);
	}
	
	public void elegirMes(int anio, Month mes) {
		for(int i = 0; i < this.meses.size(); i++) {
			if(this.meses.get(i).getAnio()==anio && this.meses.get(i).getMes().equals(mes)) {
				this.mesActual = i;
				return;
			}
		}
		this.meses.add(new Mes(new ArrayList<Gestion>(), anio, mes));
		this.mesActual = this.meses.size()-1;
		System.out.println(this.meses.size()-1);
	}
	
	public ArrayList<Gestion> getGestionesActuales(){
		return this.meses.get(this.mesActual).getGestiones();
	}
	
}

class Mes{
	private int anio;
	private Month mes;
	int total;
	ArrayList<Gestion> gestiones;
	Mes(ArrayList<Gestion> gestiones, int anio, Month mes){
		this.gestiones = gestiones;
		this.anio=anio;
		this.mes=mes;
	}
	
	void aniadirTransaccion(Transaccion transaccion, String nombre, boolean isPositivo) {
		for(Gestion gestion:this.gestiones) {
			if(gestion.getNombre().equals(nombre)) {
				gestion.aniadirGasto(transaccion);
				return;
			}
		}
		gestiones.add(new Gestion(nombre, isPositivo));
		gestiones.get(this.gestiones.size()-1).aniadirGasto(transaccion);
		return;
	}
	
	Month getMes() {
		return this.mes;
	}
	
	int getAnio() {
		return this.anio;
	}
	
	ArrayList<Gestion> getGestiones(){
		return this.gestiones;
	}
	
}