//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/.
//

#ifndef BATTERY_H_
#define BATTERY_H_

#include <BaseBattery.h>
#include <BatteryState.h>
#include <HostState.h>
#include <DeviceEntry.h>


class Battery: public BaseBattery {
public:
	virtual void initialize(int);
	virtual void handleMessage(cMessage*);
	virtual void handleHostState(const HostState& state);
	virtual void finish();

	/** @brief current state of charge of the battery, relative to its
	 * rated nominal capacity [0..1]
	 */
	double estimateResidualRelative();
	/** @brief current state of charge of the battery (mW-s) */
	double estimateResidualAbs();

	/**
	 * @brief Registers a power device by creating a new DeviceEntry for it.
	 */
	virtual int registerDevice(const std::string& name, int numAccounts);

	/**
	 * @brief Draws either a certain amount of energy in mWs or
	 * a defined current in mA over time, depending on passed DrawAmount.
	 */
	virtual void draw(int drainID, DrawAmount& amount, int activity);

	//Own Battery Module does noy use this function
	virtual double getVoltage()
	{
		return -1;
	}


protected:

	// INPUT parameters
	int numDevices;

	double nominalCapacity; 	//in Joules or mW-s
	double capacity;			//in Joules or mW-s
	double residualCapacity;
	double lastPublishCapacity;
	simtime_t lifetime;

	// debit battery at least once every resolution seconds
	simtime_t resolution;
	cMessage *timeout;

	// publish capacity to BatteryStats via the BB
	int batteryCat;
	cMessage *publish;
	double publishDelta;
	simtime_t publishTime;

	BatteryState *batteryState;
	DeviceEntry *devices;
	int registeredDevices;

	// publish the host failure notification (everyone should subscribe to this)
	int scopeHost;
	int hostStateCat;
	HostState hostState;

	enum msgType {
		AUTO_UPDATE, PUBLISH,
	};

	simtime_t lastUpdateTime;
	void deductAndCheck();
};

#endif
