//This BotBase was created by Apoc, I take no credit for anything within this code
//I just changed "!StyxWoW.Me.CurrentTarget.IsFriendly" to "!StyxWoW.Me.CurrentTarget.IsHostile"
//For the purpose of allowing RaidBot to work within Arenas
using System;
using System.Windows.Forms;
using System.Windows.Media;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Profiles;
using Styx.CommonBot.Routines;
using Styx.WoWInternals;
using Styx.TreeSharp;
using Action = Styx.TreeSharp.Action;

namespace RaidBot
{
	public class RaidBot : BotBase
	{
		private Composite _root;
		public override string Name { get { return "Raid Bot"; } }
		public override Composite Root { get { return _root ?? (_root = new PrioritySelector(CreateRootBehavior())); } }
		public override PulseFlags PulseFlags { get { return PulseFlags.All & ~(PulseFlags.Targeting | PulseFlags.Looting); } }
		public bool IsPaused { get; set; }
		public override void Start()
		{
			TreeRoot.TicksPerSecond = 30;
			//if (ProfileManager.CurrentProfile == null)
				ProfileManager.LoadEmpty();

			HotkeysManager.Register("RaidBot Pause",
				Keys.X,
				ModifierKeys.Alt,
				hk =>
					{
						IsPaused = !IsPaused;
						if (IsPaused)
						{
							AddToast("RaidBot Paused!", Colors.Red);
							// Make the bot use less resources while paused.
							TreeRoot.TicksPerSecond = 5;
						}
						else
						{
							AddToast("RaidBot Resumed!", Colors.Green);
							// Kick it back into overdrive!
							TreeRoot.TicksPerSecond = 30;
						}
					});
		}

		private void AddToast(string text, Color color)
		{
			StyxWoW.Overlay.AddToast(() => text, TimeSpan.FromSeconds(2), color, Colors.Black, new FontFamily("Courier"));			
		}

		private Composite CreateRootBehavior()
		{
			return
				new PrioritySelector(
					new Decorator(ret => IsPaused,
						new Action(ret => RunStatus.Success)),
					new Decorator(ret => !StyxWoW.Me.Combat,
						new PrioritySelector(
							RoutineManager.Current.PreCombatBuffBehavior)),
					new Decorator(ret => StyxWoW.Me.Combat,
						new LockSelector(
							RoutineManager.Current.HealBehavior,
							new Decorator(ret => StyxWoW.Me.GotTarget && !StyxWoW.Me.CurrentTarget.IsFriendly && !StyxWoW.Me.CurrentTarget.IsDead,
								new PrioritySelector(
									RoutineManager.Current.CombatBuffBehavior,
									RoutineManager.Current.CombatBehavior)))));
		}

		public override void Stop()
		{
			TreeRoot.ResetTicksPerSecond();
			HotkeysManager.Unregister("RaidBot Pause");
		}

		#region Nested type: LockSelector

		private class LockSelector : PrioritySelector
		{
			public LockSelector(params Composite[] children) : base(children)
			{
			}

			public override RunStatus Tick(object context)
			{
				using (StyxWoW.Memory.AcquireFrame())
				{
					return base.Tick(context);
				}
			}
		}

		#endregion
	}
}