/*
 * 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.ArrayList;
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.Environment;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.properties.RoadProperty;
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 VisionBasedVehicleDecisionComponent extends AbstractVehicleDecisionComponent {

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

    @Override
    public BenefitBasedRoute getOptimalRoute(RoadNetworkRoute pInvestigatedRoute,
            List<RoadInformation> pKnownInformation) {
        return new BenefitBasedRoute(0, pInvestigatedRoute);
    }

    @Override
    public BenefitBasedRoute getOptimalRoute(RoadNetworkRoute pInvestigatedRoute, List<RoadInformation> knownInformation, Environment pEnvironment) {
        List<RoadProperty> actualKnownInformation = new ArrayList<>();

        if (getVehicleInformation().isValid()) {
            if (knownInformation != null) {
                for (RoadInformation roadInformation : knownInformation) {
                    RoadProperty property = roadInformation.getValue();
                    if (!property.getValue().equals(property.getDefaultProperty().getValue())) {
                        if (pInvestigatedRoute.getStart().getAccessibleEdges().contains(roadInformation.getEdge())) {
                            actualKnownInformation.add(roadInformation.getValue());
                        }
                    }
                }
            }
            actualKnownInformation.addAll(pEnvironment.getProperties(RoadProperty.class));


            DijkstraAlgorithm dijkstraAlgorithm = new DijkstraAlgorithm();
            List<RoadNetworkEdge> blocked = new ArrayList<>();

            for (RoadProperty roadInformation : actualKnownInformation) {
                if (!blocked.contains(roadInformation.getEdge()) && roadInformation.getEdge().isActive()) {
                    blocked.add(roadInformation.getEdge());
                }
            }

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

                double newCosts = 0;
                for (RoadNetworkEdge edge : route.getRoute()) {
                    newCosts += edge.calculateStandardEdgeCosts();
                    for (RoadProperty roadInformation : actualKnownInformation) {
                        newCosts += edge.calculateAdditionalCostIfKnown(roadInformation);
                    }
                }

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

}
