#include "light.h"

namespace CBK
{
	Light::Light(IDirect3DDevice9* device) : Object(device)
	{
		::ZeroMemory(&_light, sizeof(_light));
		setType(D3DLIGHT_SPOT);
	}

	Light::~Light(void)
	{
	}

	Object& Light::load(const std::string& name)
	{
		return *this;
	}

	Light& Light::setColor(const D3DCOLORVALUE& ambient, const D3DCOLORVALUE& diffuse, const D3DCOLORVALUE& specular)
	{
		setAmbientColor(ambient);
		setDiffuseColor(diffuse);
		setSpecularColor(specular);

		return *this;
	}

	Light& Light::setAmbientColor(const D3DCOLORVALUE& color)
	{
		_light.Ambient = color;

		return *this;
	}

	Light& Light::setDiffuseColor(const D3DCOLORVALUE& color)
	{
		_light.Diffuse = color;

		return *this;
	}

	Light& Light::setSpecularColor(const D3DCOLORVALUE& color)
	{
		_light.Specular = color;

		return *this;
	}

	Light& Light::setType(const D3DLIGHTTYPE& type)
	{
		_light.Type = type;

		return *this;
	}

	D3DLIGHTTYPE& Light::getType(void)
	{
		return _light.Type;
	}

	void Light::apply(void)
	{
		apply(0);
	}

	void Light::apply(const DWORD& index) const
	{
		if (device == 0) return;

		device->SetLight(index, &_light);
		device->LightEnable(index, true);
	}

	void Light::disable(IDirect3DDevice9* device, const DWORD& index)
	{
		if (device == 0) return;

		device->SetLight(index, 0);
		device->LightEnable(index, false);
	}

	Light& Light::setCones(const float& theta, const float& phi, const float& falloff)
	{
		_light.Theta = theta;
		_light.Phi = phi;
		_light.Falloff = falloff;

		return *this;
	}


	Light& Light::setAttenuation(const float& constant, const float& linear, const float& quadratic)
	{
		_light.Attenuation0 = constant;
		_light.Attenuation1 = linear;
		_light.Attenuation2 = quadratic;

		return *this;
	}

	Light& Light::setFalloff(const float& falloff)
	{
		_light.Falloff = falloff;

		return *this;
	}

	Light& Light::setPosition(const D3DVECTOR& position)
	{
		_light.Position = position;

		return *this;
	}

	Light& Light::setDirection(const D3DVECTOR& direction)
	{
		_light.Direction = direction;

		return *this;
	}
}