/*
 * 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.energy.models;

import java.util.List;
import java.util.Set;
import java.util.Vector;

import de.tud.kom.p2psim.api.analyzer.EnergyAnalyzer;
import de.tud.kom.p2psim.api.common.SimHost;
import de.tud.kom.p2psim.api.energy.Battery;
import de.tud.kom.p2psim.api.energy.ComponentType;
import de.tud.kom.p2psim.api.energy.EnergyComponent;
import de.tud.kom.p2psim.api.energy.EnergyInfo;
import de.tud.kom.p2psim.api.energy.EnergyModel;
import de.tudarmstadt.maki.simonstrator.api.Monitor;
import de.tudarmstadt.maki.simonstrator.api.common.graph.INodeID;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.core.MonitorComponent.AnalyzerNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.battery.BatterySensor;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSComponent;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSDataCallback;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSInfoProperties;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSInformationProvider.SiSProviderHandle;
import de.tudarmstadt.maki.simonstrator.api.component.sis.exception.InformationNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.sis.type.SiSTypes;

/**
 * The default functionalities for energy models, composed of different
 * {@link EnergyComponent}s and configured via the factory.
 * 
 * @author Julian Zobel
 * @version 1.0, 07.09.2018
 */
public abstract class AbstractEnergyModel implements EnergyModel, BatterySensor, EnergyInfo {
	
	protected SimHost host;
	protected List<EnergyComponent> energyComponents;
	protected Battery bat;

	
	public AbstractEnergyModel(SimHost host, Battery bat) {
		energyComponents = new Vector<EnergyComponent>();
		this.bat = bat;
		this.host = host;
	}
	
	@Override
	public void initialize() {
		//
		try {
			SiSComponent sis = host.getComponent(SiSComponent.class);
			sis.provide().nodeState(SiSTypes.ENERGY_BATTERY_LEVEL,
					new SiSDataCallback<Double>() {

						Set<INodeID> localID = INodeID.getSingleIDSet(getHost()
								.getId());

						@Override
						public Double getValue(INodeID nodeID,
								SiSProviderHandle providerHandle)
								throws InformationNotAvailableException {
							if (nodeID.equals(getHost().getId())) {
								return getCurrentPercentage();
							} else {
								throw new InformationNotAvailableException();
							}
						}

						@Override
						public Set<INodeID> getObservedNodes() {
							return localID;
						}

						@Override
						public SiSInfoProperties getInfoProperties() {
							return new SiSInfoProperties();
						}
					});

			sis.provide().nodeState(SiSTypes.ENERGY_BATTERY_CAPACITY,
					new SiSDataCallback<Double>() {

						Set<INodeID> localID = INodeID.getSingleIDSet(getHost()
								.getId());

						@Override
						public Double getValue(INodeID nodeID,
								SiSProviderHandle providerHandle)
								throws InformationNotAvailableException {
							if (nodeID.equals(getHost().getId())) {
								return getCurrentEnergyLevel();
							} else {
								throw new InformationNotAvailableException();
							}
						}

						@Override
						public Set<INodeID> getObservedNodes() {
							return localID;
						}

						@Override
						public SiSInfoProperties getInfoProperties() {
							return new SiSInfoProperties();
						}
					});
		} catch (ComponentNotAvailableException e) {
			// OK
		}
	}
	
	@Override
	public <T extends EnergyComponent> List<T> getComponents(
			ComponentType type, Class<T> componentClass) {
		List<T> componentsOfType = new Vector<T>();
		for (EnergyComponent energyComponent : energyComponents) {
			if (energyComponent.getType().equals(type)
					&& componentClass.isInstance(energyComponent)) {
				componentsOfType.add(componentClass.cast(energyComponent));
			}
		}
		return componentsOfType;
	}

	@Override
	public <T extends EnergyComponent> T getComponent(ComponentType type,
			Class<T> componentClass) {
		for (EnergyComponent energyComponent : energyComponents) {
			if (energyComponent.getType().equals(type)
					&& componentClass.isInstance(energyComponent)) {
				return componentClass.cast(energyComponent);
			}
		}
		return null;
	}
	
	@Override
	public EnergyInfo getEnergyInfo() {
		/*
		 * We export our status to interested applications via this interface
		 */
		return this;
	}
	
	@Override
	public Battery getBattery() {
		return bat;
	}

	@Override
	public double getCurrentPercentage() {
		return bat.getCurrentPercentage();
	}

	@Override
	public double getCurrentEnergyLevel() {
		return bat.getCurrentEnergy();
	}

	@Override
	public SimHost getHost() {
		return host;
	}
	
	@Override
	public void shutdown() {
		throw new AssertionError(
				"You are not supposed to shutdown this component.");
	}

	@Override
	public void reset() {
		throw new AssertionError(
				"You are not supposed to reset this component.");
	}

	protected void monitorEnergyConsumation(EnergyComponent component, double consumedEnergy) {
		/*
		 * TODO Refactor the Energy-Analyzer to support EnergyComponents
		 * directly, rather than strings.
		 */
		try {
			Monitor.get(EnergyAnalyzer.class).consumeEnergy(getHost(),
					consumedEnergy, component);
		} catch (AnalyzerNotAvailableException e1) {
			//
		}
	}
	
	protected void monitorEmptyBattery() {
		/*
		 * Battery is now empty.
		 */
		try {
			Monitor.get(EnergyAnalyzer.class).batteryIsEmpty(getHost());
		} catch (AnalyzerNotAvailableException e) {
			//
		}
	}
}
