#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.Mods.Common.Warheads;
using OpenRA.Traits;

namespace OpenRA.Mods.Common.Traits
{
	[Desc("Make the unit go prone when under attack, in an attempt to reduce damage.")]
	public class TakeCoverInfo : TurretedInfo
	{
		[Desc("How long (in ticks) the actor remains prone.")]
		public readonly int ProneTime = 100;

		[Desc("Prone movement speed as a percentage of the normal speed.")]
		public readonly int SpeedModifier = 50;

		[FieldLoader.Require]
		[Desc("Damage types that trigger prone state. Defined on the warheads.")]
		public readonly string[] DamageTriggers = null;

		[FieldLoader.LoadUsing("LoadModifiers")]
		[Desc("Damage modifiers for each damage type (defined on the warheads) while the unit is prone.")]
		public readonly Dictionary<string, int> DamageModifiers = new Dictionary<string, int>();

		public readonly WVec ProneOffset = new WVec(85, 0, -171);

		[SequenceReference(null, true)] public readonly string ProneSequencePrefix = "prone-";

		public override object Create(ActorInitializer init) { return new TakeCover(init, this); }

		public static object LoadModifiers(MiniYaml yaml)
		{
			var md = yaml.ToDictionary();

			return md.ContainsKey("DamageModifiers")
				? md["DamageModifiers"].ToDictionary(my => FieldLoader.GetValue<int>("(value)", my.Value))
				: new Dictionary<string, int>();
		}
	}

	public class TakeCover : Turreted, INotifyDamage, IDamageModifier, ISpeedModifier, ISync, IRenderInfantrySequenceModifier
	{
		readonly TakeCoverInfo info;
		[Sync] int remainingProneTime = 0;
		bool IsProne { get { return remainingProneTime > 0; } }

		public bool IsModifyingSequence { get { return IsProne; } }
		public string SequencePrefix { get { return info.ProneSequencePrefix; } }

		public TakeCover(ActorInitializer init, TakeCoverInfo info)
			: base(init, info)
		{
			this.info = info;
		}

		public void Damaged(Actor self, AttackInfo e)
		{
			var warhead = e.Warhead as DamageWarhead;
			if (e.Damage <= 0 || warhead == null || !warhead.DamageTypes.Any(x => info.DamageTriggers.Contains(x)))
				return;

			if (!IsProne)
				localOffset = info.ProneOffset;

			remainingProneTime = info.ProneTime;
		}

		public override void Tick(Actor self)
		{
			base.Tick(self);

			if (IsProne && --remainingProneTime == 0)
				localOffset = WVec.Zero;
		}

		public int GetDamageModifier(Actor attacker, IWarhead warhead)
		{
			if (!IsProne)
				return 100;

			var damageWh = warhead as DamageWarhead;
			if (damageWh == null)
				return 100;

			var modifierPercentages = info.DamageModifiers.Where(x => damageWh.DamageTypes.Contains(x.Key)).Select(x => x.Value);
			return Util.ApplyPercentageModifiers(100, modifierPercentages);
		}

		public int GetSpeedModifier()
		{
			return IsProne ? info.SpeedModifier : 100;
		}
	}
}
