/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of Simonstrator.KOM.
 *
 * Simonstrator.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;

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

import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.component.network.NetInterface;
import de.tudarmstadt.maki.simonstrator.api.component.network.NetworkComponent.NetInterfaceName;
import de.tudarmstadt.maki.simonstrator.api.component.transport.ConnectivityListener;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.CachingComponent;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.invalidation.CacheInvalidationStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.replacement.CacheReplacementStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;

public class DefaultCachingComponent implements CachingComponent, ConnectivityListener {
	private Map<Class<? extends Object>, List<PointInformation>> _cache = new HashMap<>();
	private Host _host;

	private CacheInvalidationStrategy _invalidationStrategy;
	private CacheReplacementStrategy _replacementStrategy;

	public DefaultCachingComponent(Host pHost, CacheInvalidationStrategy pInvalidationStrategy, CacheReplacementStrategy pReplacementStrategy) {
		_host = pHost;

		_host.getNetworkComponent().getByName(NetInterfaceName.WIFI).addConnectivityListener(this);

		_invalidationStrategy = pInvalidationStrategy;
		_replacementStrategy = pReplacementStrategy;
	}

	@Override
	public <T extends PointInformation> List<T> getCacheEntries(Class<T> pCacheEntryClass) {
		if (_cache.containsKey(pCacheEntryClass)) {
			List<? extends PointInformation> cacheEntries = _cache.get(pCacheEntryClass);
			List<T> results = new ArrayList<>();

			for (int i = 0; i < cacheEntries.size(); i++) {
				PointInformation object = cacheEntries.get(i);
				if (_invalidationStrategy.checkInformation(object)) {
					cacheEntries.remove(i--);
					continue;
				}
				results.add((T) object);
			}

			return results;
		}
		return null;
	}

	@Override
	public <T extends PointInformation> boolean containsEntry(T pCacheEntry) {
		if (_cache.containsKey(pCacheEntry)) {
			List<? extends Object> cacheEntries = _cache.get(pCacheEntry);
			return cacheEntries.contains(pCacheEntry);
		}
		return false;
	}

	@Override
	public <T extends PointInformation> void storeCacheEntry(T pCacheEntry) {
		if (!_cache.containsKey(pCacheEntry.getClass())) {
			_cache.put(pCacheEntry.getClass(), new ArrayList<>());
		}
		List<PointInformation> entries = _cache.get(pCacheEntry.getClass());
		int index;
		if ((index = entries.indexOf(pCacheEntry)) != -1) {
			if (_replacementStrategy.replaceInformation(entries.get(index), pCacheEntry)) {
				entries.remove(index);
				entries.add(pCacheEntry);
			} else {
				// Nothing to do
			}
		} else {
			entries.add(pCacheEntry);
		}
	}

	@Override
	public void initialize() {
		_cache.clear();
	}

	@Override
	public void shutdown() {
	}

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

	@Override
	public void wentOnline(Host pHost, NetInterface pNetInterface) {
		_cache.clear();
	}

	@Override
	public void wentOffline(Host pHost, NetInterface pNetInterface) {

	}

	@Override
	public void clear() {
		_cache.clear();
	}

}
