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


import de.tud.kom.p2psim.impl.topology.component.UAVTopologyComponent;

import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.component.HostComponent;
import de.tudarmstadt.maki.simonstrator.api.component.HostComponentFactory;
import de.tudarmstadt.maki.simonstrator.api.uavsupport.callbacks.BatteryReplacementCallback;

/**
 * Ground-based charger for UAVs. Replaces the battery completely, i.e., 
 * sets battery capacity to full levels, but requires some time to do so.
 * 
 * 
 * @author Julian Zobel
 * @version 1.0, 21.09.2018
 */
public class UAVReplacementCharger implements HostComponent {
	
	private Host host;
	private long replacementDuration;
	
	public UAVReplacementCharger(Host host, long replacementDuration) {
		this.host = host;
		this.replacementDuration = replacementDuration;		
	}
	
	/**
	 * Charge a UAV by replacing the battery (i.e. reset to full battery levels). Replacement takes some time.
	 * The UAV is informed about the full replacement when it is finished by a callback.
	 * 
	 * @param uav
	 * @param cb
	 */
	public void charge(UAVTopologyComponent uav, BatteryReplacementCallback cb) {
		
		Event.scheduleWithDelay(replacementDuration, new EventHandler() {
			@Override
			public void eventOccurred(Object content, int type) {
				replacementComplete(uav, cb);
			}
		}, null, 0);			
	}
	
	/**
	 * When this is called (after the replacement duration) the UAV's battery is set 
	 * to full level/max capacity.
	 * 
	 * @param uav
	 * @param cb
	 */
	protected void replacementComplete(UAVTopologyComponent uav, BatteryReplacementCallback cb) {		
		uav.getBattery().setToMaximumLoad();
		cb.rechargeComplete();
	}
	
	@Override
	public void initialize() {
		// TODO Auto-generated method stub		
	}

	@Override
	public void shutdown() {
		throw new UnsupportedOperationException("Charger shutdown not supported!");
	}

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

	
	public static class Factory implements HostComponentFactory {

		private long replacementDuration;
				
		/**
		 * Duration of the battery replacement in SECONDS
		 * @param replacementDuration
		 */
		public void setReplacementDuration(long replacementDuration) {
			this.replacementDuration = replacementDuration;
		}	
		
		@Override
		public HostComponent createComponent(Host host) {
			return new UAVReplacementCharger(host, replacementDuration);
		}
	
	}
}
