/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 *
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * PeerfactSim.KOM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package de.tud.kom.p2psim.impl.vehicular;

import java.util.List;
import java.util.Set;

import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.common.graph.INodeID;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.route.Route;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSComponent;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSDataCallback;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSInfoProperties;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSInformationProvider.SiSProviderHandle;
import de.tudarmstadt.maki.simonstrator.api.component.sis.exception.InformationNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.sis.type.SiSTypes;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.VehicleInformationComponent;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.SimulationSetupExtractor;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.VehicleController;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkEdge;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkRoute;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.exception.NoAdditionalRouteAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.exception.NoExitAvailableException;

public class DefaultVehicleInformationComponent implements VehicleInformationComponent {

	private Host host;

	private VehicleController controller;
	private SimulationSetupExtractor extractor;

	private String vehicleID;

	public DefaultVehicleInformationComponent(Host host, VehicleController controller, SimulationSetupExtractor extractor, boolean pRouteKnown) {
		this.host = host;

		this.controller = controller;
		this.extractor = extractor;

		try {
			SiSComponent siSComponent = getHost().getComponent(SiSComponent.class);
			siSComponent.provide().nodeState(SiSTypes.ROAD_EDGE, new SiSDataCallback<RoadNetworkEdge>() {

				@Override
				public RoadNetworkEdge getValue(INodeID pNodeID,
						SiSProviderHandle pProviderHandle)
								throws InformationNotAvailableException {
					if (pNodeID == getHost().getId()) {
						if (isValid()) {
							RoadNetworkRoute route = getCurrentRoute();
							if (route != null) {
								return route.getStart();
							}
						}
					}
					return null;
				}

				@Override
				public Set<INodeID> getObservedNodes() {
					return INodeID.getSingleIDSet(getHost().getId());
				}

				@Override
				public SiSInfoProperties getInfoProperties() {
					return new SiSInfoProperties();
				}

			});

			siSComponent.provide().nodeState(SiSTypes.HEADING, new SiSDataCallback<Double>() {

				@Override
				public Double getValue(INodeID pNodeID,
						SiSProviderHandle pProviderHandle)
								throws InformationNotAvailableException {
					if (pNodeID == getHost().getId()) {
						if (isValid()) {
							return controller.getVehicleHeading(vehicleID);
						}
					}
					return null;
				}

				@Override
				public Set<INodeID> getObservedNodes() {
					return INodeID.getSingleIDSet(getHost().getId());
				}

				@Override
				public SiSInfoProperties getInfoProperties() {
					return new SiSInfoProperties();
				}

			});

			if (pRouteKnown) {
				siSComponent.provide().nodeState(SiSTypes.ROUTE, new SiSDataCallback<RoadNetworkRoute>() {

					@Override
					public RoadNetworkRoute getValue(INodeID pNodeID,
							SiSProviderHandle pProviderHandle)
									throws InformationNotAvailableException {
						if (pNodeID == getHost().getId()) {
							if (isValid()) {
								return getCurrentRoute();
							}
						}
						return null;
					}

					@Override
					public Set<INodeID> getObservedNodes() {
						return INodeID.getSingleIDSet(getHost().getId());
					}

					@Override
					public SiSInfoProperties getInfoProperties() {
						return new SiSInfoProperties();
					}

				});
			}
		} catch (ComponentNotAvailableException e) {
			// Nothing to do!
		}
	}

	@Override
	public void setVehicleID(String pVehicleID) {
		vehicleID = pVehicleID;
	}

	@Override
	public void resetVehicleID() {
		vehicleID = null;
	}

	@Override
	public void initialize() {
		// TODO Auto-generated method stub

	}

	@Override
	public void shutdown() {
		// TODO Auto-generated method stub

	}

	@Override
	public Host getHost() {
		return host;
	}

	@Override
	public RoadNetworkRoute getCurrentRoute() {
		return controller.getCurrentRoute(vehicleID);
	}

	@Override
	public boolean changeCurrentRoute(RoadNetworkRoute pRoute) {
		controller.rerouteVehicle(vehicleID, pRoute);
		return true;
	}

	@Override
	public RoadNetworkRoute findNewRoute() {
		try {
			return controller.findNewRoute(vehicleID);
		} catch (NoAdditionalRouteAvailableException e) {
			return null;
		}
	}

	@Override
	public RoadNetworkRoute findNewRoute(List<RoadNetworkEdge> pEdgesToAvoid,
			boolean pKeepDestination) {
		try {
			return controller.findNewRoute(vehicleID, pEdgesToAvoid, pKeepDestination);
		} catch (NoAdditionalRouteAvailableException
				| NoExitAvailableException e) {
			return null;
		}
	}

	@Override
	public boolean isValid() {
		return vehicleID != null;
	}

	@Override
	public void stopVehicle() {
		controller.stopVehicle(vehicleID);
	}

	@Override
	public double getCurrentSpeed() {
		return controller.getVehicleSpeed(vehicleID);
	}

	@Override
	public void addRouteListener(RouteListener listener) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void removeRouteListener(RouteListener listener) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void addRouteSegmentListener(RouteSegmentListener listener) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void removeRouteSegmentListener(RouteSegmentListener listener) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Route getRoute() {
		throw new UnsupportedOperationException();
	}

}
