#include "Weapon.h"
#include "Level.h"
#include "Parser.h"
#include "Bullet.h"
#include "Sprite.h"
#include "glfuncs.h"
#include "Command.h"
#include "CommandManager.h"

#include <sstream>

using namespace std;

Weapon::Weapon()
{

}

Weapon::Weapon(std::string filename, Person *shooter)
:_waitShoot(15), _framesSinceShoot(0), _bulletSpeed(500), _ammo(0), _ammoSize(0), _id(""), _shooter(shooter) ,_icon(NULL),
_damage(1),_spray(false),_sprayCount(0)
{
	List *info = Parser::parse(filename);

	load(info);

	delete info;
}

Weapon *Weapon::copy(Person *shooter)
{
	Weapon *copy = new Weapon(*this);
	copy->_shooter = shooter;
	return copy;
}

void Weapon::load(List *info)
{
	_id = info->firstString();
	info = info->rest();

	while(!info->empty()) {
		List *current = info->firstList();

		if(current->firstString() == "waitShoot")
			_waitShoot = current->rest()->firstInt();
		else if(current->firstString() == "bulletSpeed")
			_bulletSpeed = current->rest()->firstInt();
		else if(current->firstString() == "ammo")
			_ammo = current->rest()->firstInt();
		else if(current->firstString() == "ammoSize")
			_ammoSize = current->rest()->firstInt();
		else if(current->firstString() == "icon")
			_icon = Sprite::loadSprite(current->rest()->firstString());
		else if(current->firstString() == "damage")
			_damage = current->rest()->firstInt();
		else if(current->firstString() == "spray")
			_spray = true;

		info = info->rest();
	}
}

void Weapon::addAmmo(int dAmmo)
{
	PlaySoundCommand("sounds/gun_boltclose.wav").execute();
	_ammo += dAmmo;
	PlaySoundCommand("sounds/gun_boltclose_b.wav").execute();
}

SDL_Rect Weapon::drawAmmo(SDL_Rect raster)
{
	//glfuncs::instance()->glRasterPos2f(raster.x,raster.y);
	stringstream ammo;
	ammo << "Ammo: " << _ammo;
	glfuncs::instance()->glPrint(raster.x,raster.y,ammo.str());
	raster.y += 30;

	//glfuncs::instance()->glEnable(GL_TEXTURE_2D);
	_icon->draw(raster.x,raster.y,Sprite::SCREEN);
	//glfuncs::instance()->glDisable(GL_TEXTURE_2D);

	return raster;
}

void Weapon::shoot()
{
	if(_ammoSize <= _ammo && _framesSinceShoot >= _waitShoot)
	{
		//This is here to make sure that the sound effect only plays once per shot
		//And we check to see that it's goudy because we'll want a much softer sound
		//for enemy shots
		if(_shooter->getId() == "goudy1")
		{
			if(_id == "goudyPistol")
				PlaySoundCommand("sounds/gunshot01.wav").execute();
			else if(_id == "machineGun")
				PlaySoundCommand("sounds/goudymachinegun.wav").execute();
		}
		else
		{
			if(_id == "easyPistol")
				PlaySoundCommand("sounds/gun_glock9mm_02.wav").execute();
			else if(_id == "machineGun")
				PlaySoundCommand("sounds/enemymachinegun_b.wav").execute();
		}

		if(_spray) {
			_sprayCount++;
			_sprayCount %= 4;
			Level::instance()->registerObject( new Bullet( _shooter, _bulletSpeed, _damage, _sprayCount+1 ) );
		}
		else {
			Level::instance()->registerObject( new Bullet( _shooter, _bulletSpeed, _damage ) );
		}
		_framesSinceShoot = 0;
		_ammo -= _ammoSize;
	}
}

void Weapon::update()
{
	_framesSinceShoot++;
}