#region Copyright & License Information
/*
 * Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
 * This file is part of OpenRA, which is free software. It is made
 * available to you under the terms of the GNU General Public License
 * as published by the Free Software Foundation. For more information,
 * see COPYING.
 */
#endregion

using System.Collections.Generic;
using System.Linq;
using OpenRA.Activities;
using OpenRA.Mods.Common.Traits;

namespace OpenRA.Mods.Common.Activities
{
	public class Rearm : Activity
	{
		readonly AmmoPool[] ammoPools;
		readonly Dictionary<AmmoPool, int> ammoPoolsReloadTimes;

		public Rearm(Actor self)
		{
			ammoPools = self.TraitsImplementing<AmmoPool>().Where(p => !p.Info.SelfReloads).ToArray();

			if (ammoPools == null)
				return;

			ammoPoolsReloadTimes = ammoPools.ToDictionary(x => x, y => y.Info.ReloadTicks);
		}

		public override Activity Tick(Actor self)
		{
			if (IsCanceled || ammoPoolsReloadTimes == null)
				return NextActivity;

			var needsReloading = false;

			foreach (var pool in ammoPools)
			{
				if (pool.FullAmmo())
					continue;

				needsReloading = true;

				if (--ammoPoolsReloadTimes[pool] > 0)
					continue;

				// HACK to check if we are on the helipad/airfield/etc.
				var hostBuilding = self.World.ActorMap.GetUnitsAt(self.Location).FirstOrDefault(a => a.HasTrait<RenderBuilding>());

				if (hostBuilding == null || !hostBuilding.IsInWorld)
					return NextActivity;

				if (!pool.GiveAmmo())
					continue;

				hostBuilding.Trait<RenderBuilding>().PlayCustomAnim(hostBuilding, "active");

				var sound = pool.Info.RearmSound;
				if (sound != null)
					Sound.Play(sound, self.CenterPosition);

				ammoPoolsReloadTimes[pool] = pool.Info.ReloadTicks;
			}

			return needsReloading ? this : NextActivity;
		}
	}
}
