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


import de.tud.kom.p2psim.api.energy.Battery;
import de.tud.kom.p2psim.api.energy.ComponentType;
import de.tud.kom.p2psim.api.energy.EnergyEventListener;
import de.tud.kom.p2psim.api.energy.EnergyState;
import de.tud.kom.p2psim.impl.energy.DefaultEnergyState;
import de.tudarmstadt.maki.simonstrator.api.Time;


/**
 * Energy component for actuators, representing the electrical consumers of actuators.
 * This models uses several states, that define the used energy. 
 * 
 * @author Julian Zobel
 * @version 1.0, 11.09.2018
 */
public class StatefulActuatorComponent implements ActuatorComponent {
	
	/**
	 * States supported by this energy component
	 * 
	 * TODO More states reflecting a more accurate energy consumption?
	 */
	public final EnergyState OFF, FLY, MAX;
	
	private EnergyState currentState;
	
	private EnergyEventListener energyModel;
	
	private long lastEnergyConsumationEvent;
	
	private double actuatorLoad;
		
	public StatefulActuatorComponent(int numberOfActuators, double volt, double hoverAmp, double maxAmp) {
		OFF = new DefaultEnergyState("OFF", 0);		
		FLY = new DefaultEnergyState("FLY", numberOfActuators * (hoverAmp * volt) * Battery.uJconverison);		
		MAX = new DefaultEnergyState("MAX", numberOfActuators * (maxAmp * volt) * Battery.uJconverison);
		
		this.currentState = OFF;
		this.lastEnergyConsumationEvent = Time.getCurrentTime();
		this.actuatorLoad = 0;
	}
	
	public void doStateChange(EnergyState newState) {		
		long timeSpentInState = Time.getCurrentTime() - lastEnergyConsumationEvent;
		
		energyModel.componentConsumedEnergy(this, calculateEnergyConsumation(currentState, timeSpentInState));			
		
		currentState = newState;		
		lastEnergyConsumationEvent = Time.getCurrentTime();				
	}
	
	@Override
	public double calculateEnergyConsumation(EnergyState state, long timeInState) {
		if(state.equals(FLY)) {
			double consumationDelta = MAX.getEnergyConsumption() - FLY.getEnergyConsumption();			
			double consumation = FLY.getEnergyConsumption() + consumationDelta * actuatorLoad;
			return consumation * ( (double) timeInState / (double) Time.SECOND);
		}
		else
			return state.getEnergyConsumption() * ( (double) timeInState / (double) Time.SECOND);
	}
	
	public void useActuator(double load) {
		if(load < 0 || load > 1.0) {
			throw new AssertionError("Actuator load must be between 0 and 1!");
		}
		else {			
			this.actuatorLoad = load;
			doStateChange(FLY);
		}
	}
	
	/**
	 * Estimates the energy consumption based on a given load.
	 * @param load: Actuator load between 0.0 and 1.0
	 * @return Energy consumption in J/s
	 */
	public double estimateEnergyConsumptionWatt(double load) {
		double consumationDelta = MAX.getEnergyConsumption() - FLY.getEnergyConsumption();			
		double estimation = FLY.getEnergyConsumption() + consumationDelta * load;
		
//		System.out.println("MAX " + ((MAX.getEnergyConsumption() / 14.8) /  Battery.uJconverison));
//		System.out.println("MIN" + ((FLY.getEnergyConsumption() / 14.8) /  Battery.uJconverison));
//		System.out.println(load);
		// this estimation is in uJ, but has to return J, thus, do conversion
		return estimation / Battery.uJconverison;
	}
	
	@Override
	public void eventOccurred(Object content, int type) {
		// TODO Auto-generated method stub		
	}

	@Override
	public ComponentType getType() {	
		return ComponentType.ACTUATOR;
	}

	@Override
	public boolean turnOff() {
		doStateChange(OFF);
		return true;
	}

	@Override
	public boolean turnOn() {
		if (isAvailable()) {
			if (currentState.equals(OFF)) {
				doStateChange(FLY);
			}			
			return true;
		}
		return false;
	}
	
	public boolean isAvailable() {
		if (energyModel.componentCanBeActivated(this))			
				return true;
		return false;
	}

	@Override
	public boolean isOn() {
		if(!currentState.equals(OFF) && isAvailable()) 
		{
			return true;			
		}
		return false;
	}
	
	@Override
	public void setEnergyEventListener(EnergyEventListener listener) {
		energyModel = listener;		
	}

	public EnergyState getCurrentState() {
		return currentState;
	}
	
	
}
