/*
 * 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.decision;

import java.util.Collections;
import java.util.List;

import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.properties.JamProperty;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.RoadInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetwork;
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.routing.DijkstraAlgorithm;

public class StaticInformationVehicleDecisionComponent extends AbstractVehicleDecisionComponent {

    public StaticInformationVehicleDecisionComponent(Host pHost) {
        super(pHost);
    }

    @Override
    public BenefitBasedRoute getOptimalRoute(RoadNetworkRoute pInvestigatedRoute, List<RoadInformation> knownInformation) {
        if (getVehicleInformation().isValid() && pInvestigatedRoute.getRoute().size() > 1) {
            DijkstraAlgorithm dijkstraAlgorithm = getRoutingAlgorithm();

            RoadNetworkRoute route = dijkstraAlgorithm.findRoute(RoadNetwork.CURRENT_ROAD_NETWORK, getVehicleInformation().getCurrentRoute().getStart(), getVehicleInformation().getCurrentRoute().getDestination(), Collections.emptyList(), Collections.emptyList(), knownInformation);
            if (route != null) {
                double oldCosts = 0;
                for (RoadNetworkEdge edge : pInvestigatedRoute.getRoute()) {
                    oldCosts += edge.calculateStandardEdgeCosts();
                    if (knownInformation != null) {
                        for (RoadInformation roadInformation : knownInformation) {
                            double additionalCosts = edge.calculateAdditionalCostIfKnown(roadInformation.getValue());
                            oldCosts += additionalCosts;
                        }
                    }
                }

                double newCosts = 0;
                for (RoadNetworkEdge edge : route.getRoute()) {
                    newCosts += edge.calculateStandardEdgeCosts();
                    if (knownInformation != null) {
                        for (RoadInformation roadInformation : knownInformation) {
                            double additionalCosts = edge.calculateAdditionalCostIfKnown(roadInformation.getValue());
                            newCosts += additionalCosts;
                        }
                    }
                }

                if (oldCosts > newCosts) {
                    return new BenefitBasedRoute(oldCosts - newCosts, route);
                }
            }
            return new BenefitBasedRoute(0, pInvestigatedRoute);
        }
        return new BenefitBasedRoute(0, pInvestigatedRoute);
    }

    protected DijkstraAlgorithm getRoutingAlgorithm() {
        return new DijkstraAlgorithm();
    }

}
