/*
 * 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.analyzer.metric.filter;

import java.util.List;

import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.common.metric.Metric;
import de.tudarmstadt.maki.simonstrator.api.common.metric.Metric.MetricValue;

public class OnShutdownFilter extends AbstractFilter<MetricValue<?>> {

	protected String description = "Shutdown Value";
	private String prefix;
	@Override
	public void onStop() {
		notifyListenersOfUpdate();
	}

	@Override
	protected void onInitialize(List<Metric<?>> incomingMetrics) {
		for (Metric metric : incomingMetrics) {			
			createDerivedMetric(metric, metric.isOverallMetric(), metric.getUnit(),
					description + " of " + metric.getName(), true);
		}
	}

	@Override
	protected MetricValue<?> getDerivedMetricValueFor(Metric<?> derivedMetric,
			List<Metric<?>> inputs, Host host) {
		
		if (inputs.size() != 1) {
			throw new AssertionError();
		}
		Metric input = inputs.get(0);
		MetricValue inputMv = null;
		if (input.isOverallMetric()) {
			inputMv = input.getOverallMetric();
		} else {
			inputMv = input.getPerHostMetric(host.getId());
		}
		
		return inputMv;		
	}
	
	@Override
	protected String getNameForDerivedMetric(List<Metric<?>> inputs) {
		if (inputs.size() != 1) {
			throw new AssertionError();
		}
		
		if (prefix == null) {
			return "OnShutdown_"
					+ inputs.get(0).getName();
		} else {
			return prefix + "_" + inputs.get(0).getName();
		}
	}

	/**
	 * Prefix to use instead of the Filtername
	 * 
	 * @param prefix
	 * @return
	 */
	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}
}
