public class AutobusFired implements Task {
	private Autobus autobus;
	private Evento evento;

	public AutobusFired(Autobus autobus, Evento evento) {
		this.autobus = autobus;
		this.evento = evento;
	}

	@Override
	public void esegui() {
		switch (this.autobus.getStato()) {
		case INAUTORIMESSA:
			if (this.evento.getClass() == Entrata.class) {
				Autista autista = (Autista) this.evento.getMittente();
				if (autista.getAutorizzato().contains(this.autobus.getPosizione().getAutorimessa())) {
					this.autobus.setPosizione(null);
					this.autobus.precedente = autista;
					this.autobus.stato = Autobus.Stato.INSERVIZIO;
				}
			}
			else if (this.evento.getClass() == Scadenza.class) {
				this.autobus.stato = Autobus.Stato.INAUTORIMESSA;
			}
			break;

		case INSERVIZIO:
			if (this.evento.getClass() == Entrata.class) {
				Autista autista = (Autista) this.evento.getMittente();
				Entrata entrata = (Entrata) evento;
				Autorimessa rimessa = entrata.getAutorimessa();
				if (autista.equals(this.autobus.precedente)) {
					this.autobus.setPosizione(new LinkPosizione(this.autobus, rimessa));
					Environment.aggiungiEvento(new Entrata(this.autobus, this.autobus.precedente, rimessa));
					Environment.aggiungiEvento(new Entrata(this.autobus, rimessa, rimessa));
					this.autobus.stato = Autobus.Stato.INAUTORIMESSA;
				}
			}
			else if (this.evento.getClass() == Rottura.class) {
				this.autobus.precedente.setRotture(this.autobus.precedente.getRotture() + 1);
				this.autobus.stato = Autobus.Stato.ROTTURA;
			}
			break;

		case ROTTURA:
			if (this.evento.getClass() == Trasporto.class) {
				Trasporto trasporto = (Trasporto) this.evento;
				this.autobus.setPosizione(new LinkPosizione(this.autobus, trasporto.getOfficina()));
				this.autobus.stato = Autobus.Stato.INMANUTENZIONE;
			}
			break;

		case INMANUTENZIONE:
			if (this.evento.getClass() == Fine.class) {
				this.autobus.stato = Autobus.Stato.INAUTORIMESSA;
			}
			break;
		}
	}
}
