namespace BP
{
	namespace Actors
	{
		const float PICKUP_SPIN_SPEED = Math::Deg2Rad(1.0f);
		
		class Pickup : BP::ScriptActor
		{
			int rotationType;
			float respawnTime;
			kStr message;
			kStr pickupSoundPath;
			float delaySoundTime;
			kStr pickupDelaySoundPath;
			kStr respawnSoundPath;
			kStr particlePath;
			kStr respawnParticlePath;
			
			kVec3 initialScale;
			float timeToRespawn;
			//------------------------------------------------------------------------------------------------------------------------
			Pickup(kActor@ actor)
			{
				super(actor);
				initialScale = self.Scale();
				if (self.Definition() !is null)
				{
					self.Definition().GetInt("BP.rotationType", rotationType);
					self.Definition().GetString("BP.message", message);
					self.Definition().GetString("BP.sound", pickupSoundPath);
					self.Definition().GetFloat("BP.delayedSoundTime", delaySoundTime);
					self.Definition().GetString("BP.delayedSound", pickupDelaySoundPath);
					self.Definition().GetFloat("BP.respawnTime", respawnTime);
					self.Definition().GetString("BP.respawnSound", respawnSoundPath);
					self.Definition().GetString("BP.particle", particlePath);
					self.Definition().GetString("BP.respawnParticle", respawnParticlePath);
				}
			}
			//------------------------------------------------------------------------------------------------------------------------
			// void BP_OnLevelLoad(kDictMem@ pDict)
			// {
				// pDict.GetInt("BP.rotationType", rotationType);
				// pDict.GetString("BP.message", message);
				// pDict.GetString("BP.sound", pickupSoundPath);
				// pDict.GetFloat("BP.delayedSoundTime", delaySoundTime);
				// pDict.GetString("BP.delayedSound", pickupDelaySoundPath);
				// pDict.GetFloat("BP.respawnTime", respawnTime);
				// pDict.GetString("BP.respawnSound", respawnSoundPath);
				// pDict.GetString("BP.particle", particlePath);
				// pDict.GetString("BP.respawnParticle", respawnParticlePath);
				// DebugLog("sadgashdgaskjfhdasjldhlaskdj - " + pickupSoundPath);
			// }
			//------------------------------------------------------------------------------------------------------------------------
			void BP_OnSerialize(kDict dict) override
			{
				SERIALIZE_FLOAT(timeToRespawn);
			}
			//------------------------------------------------------------------------------------------------------------------------
			void BP_OnDeserialize(kDict& in dict) override
			{
				DESERIALIZE_FLOAT(timeToRespawn);
			}
			//------------------------------------------------------------------------------------------------------------------------
			void BP_OnStart() override
			{
				if (self.IsMarked())
				{
					self.Remove();
					return;
				}
			}
			//------------------------------------------------------------------------------------------------------------------------
			void BP_OnTouch(kActor@ instigator) override
			{
		        if (instigator is null || timeToRespawn > 0.0f)
				{
					return;
				}
			
				if (@instigator == @BP::LocalPlayer::Actor && !BP::Actor::IsDead(@instigator))
				{
					bool remove = BP::LocalPlayer::Script.OnPickup(@self);
					if (remove)
					{
						self.PlaySound(pickupSoundPath);
												
						if (!BP::String::IsEmpty(pickupDelaySoundPath))
						{
							BP::Tasks::Add(delaySoundTime, TaskCallBack(this.PlayPowerupSound));
						}
						
						if (!BP::String::IsEmpty(particlePath))
						{
							BP::Spawn::Particle(particlePath, @self, self.Origin(), kQuat(self.Pitch(), 0, self.Yaw()));
						}
											
						if (!BP::String::IsEmpty(message))
						{
							Hud.AddMessage(message);
						}
						
						if (respawnTime > 0.0f)
						{
							timeToRespawn = respawnTime;
							self.Scale() = Math::vecZero;
						}
						else
						{
							if (self.GetTarget() !is null)
							{
								self.GetTarget().Mark(true);
								self.GetTarget().Remove();
							}
							self.Mark(true);
							self.Remove();
						}
					}
				}
			}
			//------------------------------------------------------------------------------------------------------------------------
			void BP_OnTick() override
			{
				switch (rotationType)
				{
					case PICKUP_ROT_SPIN:
					{
						self.Yaw() += PICKUP_SPIN_SPEED;
						break;
					}
					case PICKUP_ROT_PLAYER_YAW:
					{
						self.Yaw() = BP::LocalPlayer::Actor.Yaw() + Math::pi;
						break;
					}
					case PICKUP_ROT_PLAYER_YAWPITCH:
					{
						self.Yaw() = BP::LocalPlayer::Actor.Yaw() + Math::pi;
						self.Pitch() = -BP::LocalPlayer::Actor.Pitch();
						break;
					}
				}
				
				if (timeToRespawn > 0.0f)
				{
					timeToRespawn -= GAME_DELTA_TIME;
					if (timeToRespawn < 0.0f)
					{
						self.Scale() = initialScale;
						if (!BP::String::IsEmpty(message))
						{
							self.PlaySound(respawnSoundPath);
						}
						if (!BP::String::IsEmpty(respawnParticlePath))
						{
							BP::Spawn::Particle(respawnParticlePath, @self, self.Origin(), kQuat(self.Pitch(), 0, self.Yaw()));
						}
					}
				}
			}
			//------------------------------------------------------------------------------------------------------------------------
			void PlayPowerupSound()
			{
				BP::LocalPlayer::Actor.PlaySound(pickupDelaySoundPath);
			}
			//------------------------------------------------------------------------------------------------------------------------
		};
	}
}
