/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by 
Filepath: scripts\_custom\engines\Event System\events\CTF\CtfFlag.cs
Lines of code: 187
***********************************************************/


using Server;
using System;
using Server.Mobiles;

namespace Server.EventSystem
{
	public class CtfFlag : Item
	{		
		private CtfFlagBase m_Base;
		
		
		[CommandProperty( AccessLevel.GameMaster )]
		public CtfFlagBase Base
		{
			get { return m_Base; }
			set { m_Base = value;Modify(); }
		}
		
		
		[CommandProperty( AccessLevel.GameMaster )]
		public int OwnerTeam
		{
			get { return Base!=null ? Base.Team : 0; }
		}		
		
		[Constructable]
		public CtfFlag() : base( 2578 )
		{
			Name="Torch";
			Movable=false;
			Layer = Layer.TwoHanded;
		}
		
		
		public static void Initialize()
		{
		//this returns flags to their respective base:
			
			foreach(Item item in World.Items.Values)
			{
				if(item is CtfFlag)
				{
					CtfFlag flag = item as CtfFlag;
				
					if(flag.Base!=null && flag.Base.Controller!=null)
					{
						flag.ReturnToBase(false,null);				
					}			
				}
			}
		}
		
	
		
		public void DropToGround()
		{
			TimerTick();
			if(RootParentEntity!=null && RootParentEntity is Mobile)
			{
				Mobile holder = RootParentEntity as Mobile;
				this.MoveToWorld(holder.Location,holder.Map);
				lastdrop=DateTime.Now;
			}
		}
		

		public bool IsOnBase()
		{
			if(RootParentEntity==null && X==Base.X && Y==Base.Y)return true;
			return false;
		}
		
		
		public void ReturnToBase(bool showmsg,Mobile holder)
		{
			if(Base==null || Base.Controller==null)return;
			else if(IsOnBase())return; //already at base
			
			if(holder!=null && Base!=null && Base.Controller!=null)
			{
				holder.SolidHueOverride=Base.Controller.GetTeamHueOverride(holder.Team);
			}
			
			MoveToWorld(Base.Location,Base.Map);
			Base.Controller.OnReturnFlag(Base.Team,showmsg);
		}
		
		public override bool OnMoveOver(Mobile m)
		{
			if(!m.Alive || !(m is PlayerMobile) || Base==null || m.Backpack==null || Base.Controller==null || m.Team==0)return true;
			else if(Base.Controller.EventStatus!=2 || Base.Team>Base.Controller.TeamsAmt)return true;
			
			if(Base.Team!=m.Team && CtfEventController.GetFlag(m)==null)
			{
				if(!Base.Controller.CanTakeFlag(m.Team))
				{
					m.SendNRMessage("A flag is already off it's base.  Return it first.",5);
					return true;
				}
				
				if(IsOnBase())lasttake=DateTime.Now;				
				
				Spells.Seventh.PolymorphSpell.RemoveForms(m);
				m.CloseAllGumps();
				
				if(m.Spell!=null) m.Spell=null;
				if(m.Target!=null)Targeting.Target.Cancel(m);
				
				Item todisarm = m.FindItemOnLayer(Layer.OneHanded);
				if(todisarm!=null)m.Backpack.DropItem(todisarm);
				
				todisarm = m.FindItemOnLayer(Layer.TwoHanded);
				if(todisarm!=null)m.Backpack.DropItem(todisarm);			
				
				Base.Controller.OnTakeFlag(m.Team,m);
				
				m.AddItem(this);
				TimerTick();
			}
			else if(OwnerTeam==m.Team)ReturnToBase(true,m);
			
			return true;
		}
		
		private FlagTimer timer;
		private DateTime lastdrop;		
		private DateTime lasttake;
		
		public bool TimerTick()
		{
			if(Base==null || Base.Deleted || Base.Controller==null || Base.Controller.Deleted)
			{				
				if(timer!=null)
				{
					timer.Stop();
					timer=null;
				}
				return false;
			}
			
			if(Base.Controller.EventStatus!=2)
			{
				ReturnToBase(false,null);
				return false;
			}
			
			
			if(timer==null)
			{
				timer = new FlagTimer(this);
				timer.Start();
			}
			else if(!timer.Running)
			{
				timer.Start();
				return true;
			}
			
			
			if(RootParentEntity==null && (lastdrop+Base.Controller.TimeOutDropFlag)<DateTime.Now)
			{
				ReturnToBase(true,null);
			}
			else if(RootParentEntity!=null)
			{
				if(RootParentEntity is Mobile)
				{
					if(RootParentEntity is PlayerMobile)
					{
						PlayerMobile pm = RootParentEntity as PlayerMobile;
						if(pm.NetState==null)
						{
							ReturnToBase(true,pm);
							return false;
						}							
						else if((lasttake+Base.Controller.TimeOutHoldFlag)<DateTime.Now)
						{
							pm.SendMessage("Flag time out.");
							ReturnToBase(true,pm);
							return false;
						}
					}
				}
			}			
			
			return !IsOnBase();
		}
		
		
		
		public override void OnDoubleClick(Mobile m)
		{
			if(CtfEventController.GetFlag(m)==this)
			{
				DropToGround();
				if(Base!=null && Base.Controller!=null)m.SolidHueOverride=Base.Controller.GetTeamHueOverride(m.Team);
			}
		}
		
		
			public class FlagTimer : Timer
			{
				private CtfFlag owner;
			
				public FlagTimer(CtfFlag flag) : base( TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2) )
				{
					Priority = TimerPriority.FiveSeconds;
					owner=flag;
				}
				
				protected override void OnTick()
				{
					if(owner==null || owner.Deleted || !owner.TimerTick())Stop();
				}
				
			
			}

		
		

		public CtfFlag( Serial serial ) : base( serial )
		{
		}
		
		
		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );

			writer.Write( (int) 0 ); // version
			writer.Write(m_Base);

		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );

			int version = reader.ReadInt();
			m_Base=(CtfFlagBase)reader.ReadItem();
		}	
	}
}