namespace BP
{
	class Team
	{
		int id;
		array<BP::BotInfo@> bots;
		int points;
		SActor flagHolder;
		
		//------------------------------------------------------------------------------------------------------------------------
		Team()
		{
			
		}
		//------------------------------------------------------------------------------------------------------------------------
		Team(int id)
		{
			this.id = id;
		}
		//------------------------------------------------------------------------------------------------------------------------
		bool opEquals(const Team &in other)
		{
			return id == other.id;
		}
		//------------------------------------------------------------------------------------------------------------------------
		Team@ opAssign(const Team &in other)
		{
			id = other.id;
			points = other.points;
			flagHolder = other.flagHolder;
			int botsLength = int(other.bots.length());
			bots.resize(botsLength);
			for (int i = 0; i < botsLength; i++)
			{
				@bots[i] = @other.bots[i];
			}
			
			return this;
		}
		//------------------------------------------------------------------------------------------------------------------------
		void Serialize(kDict dict, kStr& in key)
		{
			SERIALIZEKEY_INT(key, id);
			SERIALIZEKEY_INT(key, points);
			SERIALIZEKEY_SACTOR(key, flagHolder);
			
			int botsLength = int(bots.length());
			SERIALIZEKEY_INT(key, botsLength);
			for (int i = 0; i < botsLength; i++)
			{
				bots[i].Serialize(dict, key + "bot" + i);
			}
		}
		//------------------------------------------------------------------------------------------------------------------------
		void Deserialize(kDict& in dict, kStr& in key)
		{
			DESERIALIZEKEY_INT(key, id);
			DESERIALIZEKEY_INT(key, points);
			DESERIALIZEKEY_SACTOR(key, flagHolder);
			
			int botsLength;
			DESERIALIZEKEY_INT(key, botsLength);
			bots.resize(botsLength);
			for (int i = 0; i < botsLength; i++)
			{
				if (@bots[i] == null)
				{
					@bots[i] = BP::BotInfo();
				}
				bots[i].Deserialize(dict, key + "bot" + i);
			}
		}
		//------------------------------------------------------------------------------------------------------------------------
		#ifdef BP_DEBUG
		kStr ToString()
		{
			int botsLength = int(bots.length());
			kStr s = "id: " + id + " points: " + points + " flagHolder: " + flagHolder.ToString() + " botsLength: " + botsLength + " ";
			for (int i = 0; i < botsLength; i++)
			{
				if (@bots[i] != null)
				{
					s += bots[i].ToString();
				}
				else
				{
					s += "Null ";
				}
			}
			return s;
		}
		#endif
		//------------------------------------------------------------------------------------------------------------------------
		void OnDeserializeStart()
		{
			flagHolder.SetDeserializedActor();
			
			int botsLength = int(bots.length());
			for (int i = 0; i < botsLength; i++)
			{
				bots[i].OnDeserializeStart();
			}
		}
		//------------------------------------------------------------------------------------------------------------------------
		void OnTick()
		{
			int botsLength = int(bots.length());
			for (int i = 0; i < botsLength; i++)
			{
				bots[i].OnTick();
			}
		}
		//------------------------------------------------------------------------------------------------------------------------
		void AddBot(const int actorID)
		{
			BP::BotInfo@ botInfo = BP::BotInfo(actorID, id);
			if (botInfo.IsValid())
			{
				bots.insertLast(botInfo);
			}
		}
		//------------------------------------------------------------------------------------------------------------------------
		void OnBotDeath(BP::BotInfo@ bot)
		{
			//remove from bot list
			int index = bots.findByRef(@bot);
			DebugAssert(index >= 0, "[BP::Team::OnBotDeath] bot doesn't exist in bot list for team " + id);
			if (index >= 0)
			{
				bots.removeAt(index);
			}
			
			DebugAssert(bot.IsValid(), "[BP::Team::OnBotDeath] bot is not valid");
			//spawn a new bot with the same parameters as the old one
			kActor@ botActor = @bot.sActor.actor;
			AddBot(botActor.Type());
		}
		//------------------------------------------------------------------------------------------------------------------------
	};
}
