/*
 * 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 de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.routing.DijkstraAlgorithm;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.routing.GlobalKnowledgeDijkstraAlgorithm;

public class OptimalVehicleDecisionComponent extends StaticInformationVehicleDecisionComponent {

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

    @Override
    protected DijkstraAlgorithm getRoutingAlgorithm() {
        return new GlobalKnowledgeDijkstraAlgorithm();
    }

    /*
    @Override
    public BenefitBasedRoute getOptimalRoute(RoadNetworkRoute pInvestigatedRoute, List<RoadInformation> knownInformation) {
        if (getVehicleInformation().isValid()) {
            DijkstraAlgorithm dijkstraAlgorithm = new GlobalKnowledgeDijkstraAlgorithm();
            List<RoadInformation> relevant = new ArrayList<>();
            if (knownInformation != null) {
                for (RoadInformation roadInformation : knownInformation) {
                    RoadProperty property = roadInformation.getValue();
                    if (!property.getValue().equals(property.getDefaultProperty().getValue())) {
                        if (roadInformation.getValue().getDuration() == -1
                                || roadInformation.getValue().getDetectionDate()
                                        + roadInformation.getValue().getDuration() > Time.getCurrentTime()
                                                + getTimeToEvent(pInvestigatedRoute, roadInformation.getEdge())) {
                            if (!relevant.contains(roadInformation)) {
                                relevant.add(roadInformation);
                            }
                        }
                    }
                }
            }

            RoadNetworkRoute route = dijkstraAlgorithm.findRoute(RoadNetwork.CURRENT_ROAD_NETWORK, getVehicleInformation().getCurrentRoute().getStart(), getVehicleInformation().getCurrentRoute().getDestination(), Collections.emptyList(), Collections.emptyList(), relevant);
            if (route != null) {
                double oldCosts = 0;
                for (RoadNetworkEdge edge : pInvestigatedRoute.getRoute()) {
                    oldCosts += edge.calculateStandardEdgeCosts();
                    if (relevant != null) {
                        for (RoadInformation roadInformation : relevant) {
                            double additionalCosts = edge.calculateAdditionalCostIfKnown(roadInformation.getValue());
                            if (additionalCosts > (Long) roadInformation
                                    .getAttribute(AvailableInformationAttributes.EXPECTED_DURATION) / Time.SECOND) {
                                additionalCosts = (Long) roadInformation
                                        .getAttribute(AvailableInformationAttributes.EXPECTED_DURATION) / (double) Time.SECOND;
                            }
                            oldCosts += additionalCosts;
                        }
                    }
                }

                double newCosts = 0;
                for (RoadNetworkEdge edge : route.getRoute()) {
                    newCosts += edge.calculateStandardEdgeCosts();
                    if (relevant != null) {
                        for (RoadInformation roadInformation : relevant) {
                            double additionalCosts = edge.calculateAdditionalCostIfKnown(roadInformation.getValue());
                            if (additionalCosts > (Long) roadInformation
                                    .getAttribute(AvailableInformationAttributes.EXPECTED_DURATION) / Time.SECOND) {
                                additionalCosts = (Long) roadInformation
                                        .getAttribute(AvailableInformationAttributes.EXPECTED_DURATION) / (double) Time.SECOND;
                            }
                            newCosts += additionalCosts;
                        }
                    }
                }

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

    private long getTimeToEvent(RoadNetworkRoute pRoute, RoadNetworkEdge pEventEdge) {
        long timeToEvent = 0;

        if (pRoute.getRoute().indexOf(pEventEdge) > 0) {
            for (int i = 1; i < pRoute.getRoute().size(); i++) {
                if (!pRoute.getRoute().get(i).equals(pEventEdge)) {
                    timeToEvent += VehiclePathTrackerFactory.getVehiclePathTracker().getTravelTime(pEventEdge);
                } else {
                    break;
                }
            }
        }

        return (long) (timeToEvent * Time.SECOND * 0.9);
    }
    */

}
