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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.replacement.CacheReplacementStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.AvailableInformationAttributes;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;

public class TTLbasedCacheReplacementStrategy implements CacheReplacementStrategy {

	private Map<Object, Map<Object, List<Long>>> detectionTimeStorage = new HashMap<>();

	@Override
	public boolean replaceInformation(PointInformation pCachedInformation,
			PointInformation pSensedInformation) {
		if (!pCachedInformation.hasAttribute(AvailableInformationAttributes.TTL)
				|| !pSensedInformation.hasAttribute(AvailableInformationAttributes.TTL)) {
			throw new AssertionError("Cannot use TTL caching without TTL set!");
		}

		keepDetectionTimes(pCachedInformation);
		keepDetectionTimes(pSensedInformation);

		if (pCachedInformation.getValue().equals(pSensedInformation.getValue())) {
			return pSensedInformation.getDetectionDate() > pCachedInformation.getDetectionDate();
		} else {
			Object cachedPosition = getEdgeOrPosition(pCachedInformation);
			Object sensedPosition = getEdgeOrPosition(pSensedInformation);

			if (cachedPosition.equals(sensedPosition)) {
				Map<Object, List<Long>> detectedStates = detectionTimeStorage.get(cachedPosition);

				long currentTime = Time.getCurrentTime();

				long weightCached = 0;
				List<Long> detectionTimes = detectedStates.get(pCachedInformation.getValue());

				for (int i = 0; i < detectionTimes.size(); i++) {
					if (detectionTimes.get(i) < currentTime) {
						detectionTimes.remove(i);
					} else {
						weightCached += detectionTimes.get(i) - currentTime;
					}
				}

				long weightSensed = 0;
				detectionTimes = detectedStates.get(pSensedInformation.getValue());

				for (int i = 0; i < detectionTimes.size(); i++) {
					if (detectionTimes.get(i) < currentTime) {
						detectionTimes.remove(i);
					} else {
						weightSensed += detectionTimes.get(i) - currentTime;
					}
				}

				return weightSensed > weightCached;
			}
		}

		return false;
	}

	public void keepDetectionTimes(PointInformation information) {
		Object key = getEdgeOrPosition(information);
		if (!detectionTimeStorage.containsKey(key)) {
			detectionTimeStorage.put(key, new HashMap<>());
		}

		Map<Object, List<Long>> detectedStates = detectionTimeStorage.get(key);

		Object value = information.getValue();

		if (!detectedStates.containsKey(value)) {
			detectedStates.put(value, new ArrayList<>());
		}

		List<Long> detectionDates = detectedStates.get(value);

		if (!detectionDates.contains(information.getDetectionDate()
				+ (long) information.getAttribute(AvailableInformationAttributes.TTL))) {
			detectionDates.add(information.getDetectionDate() + (long)information.getAttribute(AvailableInformationAttributes.TTL));
		}
	}

	public Object getEdgeOrPosition(PointInformation information) {
		if (information.hasAttribute(AvailableInformationAttributes.EDGE)) {
			return information.getAttribute(AvailableInformationAttributes.EDGE);
		} else {
			return information.getLocation();
		}
	}

}
