//	Darkstar Engine  (c) UnderWorld Interactive Studio (all coding by Svarog    2007-2008)
//
//      Данный файл является частью Darkstar Engine. Любое использование кода файла
//      без согласия компании, является нарушением закона об авторском праве (в слу-
//      чае если не оговорено обратное).
//
//      Имя файла: material.inl
//      Краткое описание: material class
//

namespace rs
{
	inline void Material::addTexture(const std::string& name, const shared_ptr<BaseTexture>& texture)
	{
		std::map<std::string, shared_ptr<BaseMaterialParameter> >::iterator it;
		it = params.find(name);
		if(it == params.end())
		{
			shared_ptr<TextureMaterialParameter> new_param(new TextureMaterialParameter());
			new_param->setName(name);
			new_param->setTexture(texture);
			params.insert(std::pair<std::string, shared_ptr<BaseMaterialParameter> >(name, new_param));
		}
		else
		{
			if(it->second->getType() == TEXTURE_TYPE)
				static_cast<TextureMaterialParameter*>(&(*it->second))->setTexture(texture);
			else
				return;
		};
	};

	inline void Material::addFloatArray(const std::string& name, const shared_array<float>& values, const unsigned int float_number)
	{
		std::map<std::string, shared_ptr<BaseMaterialParameter> >::iterator it;
		it = params.find(name);
		if(it == params.end())
		{
			shared_ptr<FloatArrayMaterialParameter> new_param(new FloatArrayMaterialParameter());
			new_param->setName(name);
			new_param->setValues(float_number, values);
			params.insert(std::pair<std::string, shared_ptr<BaseMaterialParameter> >(name, new_param));
		}
		else
		{
			if(it->second->getType() == FLOAT_ARRAY_TYPE)
				static_cast<FloatArrayMaterialParameter*>(&(*it->second))->setValues(float_number, values);
			else
				return;
		};
	};

	inline void Material::addVect2(const std::string& name, const math::Vect2& value)
	{
		std::map<std::string, shared_ptr<BaseMaterialParameter> >::iterator it;
		it = params.find(name);
		if(it == params.end())
		{
			shared_ptr<Vect2MaterialParameter> new_param(new Vect2MaterialParameter());
			new_param->setValue(value);
			new_param->setName(name);
			params.insert(std::pair<std::string, shared_ptr<BaseMaterialParameter> >(name, new_param));
		}
		else
		{
			if(it->second->getType() == VECT2_TYPE)
				static_cast<Vect2MaterialParameter*>(&(*it->second))->setValue(value);
			else
				return;
		};
	};

	inline void Material::addVect3(const std::string& name, const math::Vect3& value)
	{
		std::map<std::string, shared_ptr<BaseMaterialParameter> >::iterator it;
		it = params.find(name);
		if(it == params.end())
		{
			shared_ptr<Vect3MaterialParameter> new_param(new Vect3MaterialParameter());
			new_param->setValue(value);
			new_param->setName(name);
			params.insert(std::pair<std::string, shared_ptr<BaseMaterialParameter> >(name, new_param));
		}
		else
		{
			if(it->second->getType() == VECT3_TYPE)
				static_cast<Vect3MaterialParameter*>(&(*it->second))->setValue(value);
			else
				return;
		};
	};

	inline void Material::addVect4(const std::string& name, const math::Vect4& value)
	{
		std::map<std::string, shared_ptr<BaseMaterialParameter> >::iterator it;
		it = params.find(name);
		if(it == params.end())
		{
			shared_ptr<Vect4MaterialParameter> new_param(new Vect4MaterialParameter());
			new_param->setValue(value);
			new_param->setName(name);
			params.insert(std::pair<std::string, shared_ptr<BaseMaterialParameter> >(name, new_param));
		}
		else
		{
			if(it->second->getType() == VECT4_TYPE)
				static_cast<Vect4MaterialParameter*>(&(*it->second))->setValue(value);
			else
				return;
		};
	};
	
	inline void Material::setBlendState(const shared_ptr<gs::BlendRenderState>& bs)
	{
		blend_state = bs;
	};

	inline const shared_ptr<gs::BlendRenderState>& Material::getBlendState() const
	{
		return blend_state;
	};

	inline void Material::setCullMode(const shared_ptr<gs::CullModeRenderState>& cm)
	{
		cull_mode = cm;
	};

	inline const shared_ptr<gs::CullModeRenderState>& Material::getCullMode() const
	{
		return cull_mode;
	};

	inline void Material::setEffect(const shared_ptr<gs::Effect>& effect)
	{
		this->effect = effect;
	};

	inline const shared_ptr<gs::Effect>& Material::getEffect() const
	{
		return effect;
	};

	inline void Material::addFlag(const std::string& name, const unsigned int value)
	{
		flags.insert(std::pair<std::string, unsigned int>(name, value));
	};

	inline void Material::setTechnique(const std::string& name)
	{
		technique_name = name;
	};

	inline const std::string& Material::getTechnique() const
	{
		return technique_name;
	};
};