/*
   core/inventory.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include <cassert>

#include "core/application.h"
#include "core/inventory.h"
#include "sys/sys.h"

namespace core
{

/* ---- class Inventory -------------------------------------------- */

Inventory::Inventory(const float capacity)
{
	inventory_timestamp = 0;
	inventory_timestamp_erase = 0;
	inventory_capacity = capacity;
	inventory_capacity_used = 0;
	inventory_dirty = false;
}

Inventory::~Inventory()
{
	clear();
	inventory_timestamp = 0;
	inventory_timestamp_erase = 0;
	inventory_capacity = 0;
	inventory_capacity_used = 0;
}

void Inventory::set_capacity(const float capacity)
{
	inventory_capacity = capacity;
}

void Inventory::set_timestamp(const unsigned long timestamp)
{
	inventory_timestamp = timestamp;
}

void Inventory::set_dirty(const bool dirty)
{
	inventory_dirty = dirty;
	if (dirty)
		recalculate();
}

void Inventory::add(Item *item)
{
	// assign an id
	unsigned int id = 0;
	for (Items::iterator it = inventory_items.begin(); it != inventory_items.end(); ++it) {
		if ((*it)->id() > id) {
			id = (*it)->id();
		}
	}
	id++;
	item->set_id(id);
	
	inventory_items.push_back(item);
}

void Inventory::erase(Item *item)
{
	for (Items::iterator it = inventory_items.begin(); it != inventory_items.end(); ++it) {
		if ((*it) == item) {
			delete (*it);
			(*it) = 0;
			inventory_items.erase(it);
			inventory_timestamp_erase = (game() ? game()->timestamp() : 1);
			return;
		}
	}
}

void Inventory::erase(const unsigned int id)
{
	for (Items::iterator it = inventory_items.begin(); it != inventory_items.end(); ++it) {
		if ((*it)->id() == id) {
			delete (*it);
			(*it) = 0;
			inventory_items.erase(it);
			inventory_timestamp_erase = (game() ? game()->timestamp() : 1);
			return;
		}
	}
}

Item *Inventory::find(Item *item) const
{
	for (Items::const_iterator it = inventory_items.begin(); it != inventory_items.end(); ++it) {
		if ((*it) == item) {
			return item;
		}
	}
	return 0;
}

Item *Inventory::find(const Info *info) const
{
	// sarch the inventory for a specified item type
	for (Items::const_iterator it = inventory_items.begin(); it != inventory_items.end(); ++it) {
		Item *item = (*it);
		if (item->info() == info) {
			return item;
		}
	}
	// not found
	return 0;
}

Item *Inventory::find(const unsigned int id) const
{
	// sarch the inventory for a specified item id
	for (Items::const_iterator it = inventory_items.begin(); it != inventory_items.end(); ++it) {
		Item *item = (*it);
		if (item->id() == id) {
			return item;
		}
	}
	// not found
	return 0;
}

void Inventory::clear()
{
	for (Items::iterator it = inventory_items.begin(); it != inventory_items.end(); ++it) {
		Item *item = (*it);
		delete item;
	}
	inventory_items.clear();
	inventory_capacity_used = 0;
}

void Inventory::recalculate()
{
	inventory_capacity_used = 0;
	for (Items::const_iterator it = inventory_items.begin(); it != inventory_items.end(); ++it) {
		const Item *item = (*it);
		inventory_capacity_used += item->amount() * item->info()->volume();
	}
}

void Inventory::serialize_server_update(std::ostream & os) const
{
	os << capacity() << " ";
}

void Inventory::receive_server_update(std::istream &is)
{
	is >> inventory_capacity;
}

const long Inventory::max_amount(const long credits, const long price, const float volume) const
{
	if ((price * volume) == 0) {
		return 0;
	}
	
	return math::min((long)(capacity_available() / volume), credits / price);
}

} // namespace core