/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by 
Filepath: scripts\_custom\engines\ML Peerless + misc areas\shimering effusion\items\LinkedPillar.cs
Lines of code: 325
***********************************************************/


 /*
##############################
Official Age of Valor Script
http://www.uovalor.com

Coded by Red Squirrel
redsquirrel@iceteks.com
##############################
*/



using System;
using Server.Targeting;
using Server.Mobiles;
using System.Collections;
using System.Collections.Generic;
 
namespace Server.Items
{
	public class LinkedPillarController : Item
	{
		private int m_totalpillars;
		private string m_giveitem;
		private TimeSpan m_DeactivateTime;		
		private List<LinkedPillar> m_ActivePillars;		
		
	
		[CommandProperty( AccessLevel.GameMaster )]
		public int completed
		{
			get { return m_ActivePillars.Count; }
		}	
		
		[CommandProperty( AccessLevel.GameMaster )]
		public int totalpillars
		{
			get { return m_totalpillars; }
			set { m_totalpillars=value;}
		}	
	
		[CommandProperty( AccessLevel.GameMaster )]
		public string giveitem
		{
			get { return m_giveitem; }
			set { m_giveitem=value;}
		}	
		
		[CommandProperty( AccessLevel.GameMaster )]
		public TimeSpan DeactivateTime
		{
			get { return m_DeactivateTime; }
			set { m_DeactivateTime=value;}
		}			
		
		
	
		[Constructable]
		public LinkedPillarController() : base(5498)
		{		
		Movable=false;
		Visible=false;
		Name="Linked Pillar Controller";
		
		m_ActivePillars = new List<LinkedPillar>();
		}
		
		public void Add(LinkedPillar newpillar)
		{
		m_ActivePillars.Add(newpillar);		
		}
		
		public void Remove(LinkedPillar newpillar)
		{
		m_ActivePillars.Remove(newpillar);		
		}		
		
		public bool IsAllActive()
		{
		return completed>=totalpillars;
		}
		
		
		public void StartDeactivate()
		{
		Timer.DelayCall( DeactivateTime, DeactivateAll );
		}
		
		public void DeactivateAll()
		{
			int count = m_ActivePillars.Count;
		
			for(int i=0;i<count;i++)
			{
			m_ActivePillars[i].active=false;		
			}
			
			m_ActivePillars.Clear();
		}
		
		


		public LinkedPillarController( Serial serial ) : base( serial )
		{
		}

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
			writer.Write( (int) 0); // version
			
			writer.Write((int)m_totalpillars);
			writer.Write(m_giveitem);			
			writer.Write(m_DeactivateTime);
			
			writer.Write((int)m_ActivePillars.Count);
			
			for(int i=0;i<m_ActivePillars.Count;i++)writer.WriteItem(m_ActivePillars[i]);			
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
			int version = reader.ReadInt();	
			
			m_totalpillars=reader.ReadInt();
			m_giveitem=reader.ReadString();			
			m_DeactivateTime=reader.ReadTimeSpan();			
			
			int active=reader.ReadInt();
			m_ActivePillars = new List<LinkedPillar>();
			for(int i=0;i<active;i++)m_ActivePillars.Add(reader.ReadItem() as LinkedPillar);
			
			
			if(IsAllActive())DeactivateAll();
		}
	}
 
	public class LinkedPillar : Item
	{	
		private int m_Hue1=0x966,m_Hue2=1150;
		private int m_ItemID1=8317,m_ItemID2=8317;
		private string m_requireditem,m_requireditemname;
		private bool m_active=false;
		private LinkedPillarController m_linkedpillarcontroller;


		
		public void UpdateStuff()
		{
			if(!active)
			{
			Hue=Hue1;
			ItemID=ItemID1;		
			}
			else
			{
			Hue=Hue2;
			ItemID=ItemID2;				
			}		
		}
		
		
				
		[CommandProperty( AccessLevel.GameMaster )]
		public int Hue1
		{
			get { return m_Hue1; }
			set { m_Hue1=value;UpdateStuff();}
		}	
		
		[CommandProperty( AccessLevel.GameMaster )]
		public int Hue2
		{
			get { return m_Hue2; }
			set { m_Hue2=value;UpdateStuff();}
		}	

		[CommandProperty( AccessLevel.GameMaster )]
		public int ItemID1
		{
			get { return m_ItemID1; }
			set { m_ItemID1=value;UpdateStuff();}
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public int ItemID2
		{
			get { return m_ItemID2; }
			set { m_ItemID2=value;UpdateStuff();}
		}	
				
		[CommandProperty( AccessLevel.GameMaster )]
		public string requireditem
		{
			get { return m_requireditem; }
			set { m_requireditem=value;}
		}
		
		[CommandProperty( AccessLevel.GameMaster )]
		public string requireditemname
		{
			get { return m_requireditemname; }
			set { m_requireditemname=value;}
		}			

		[CommandProperty( AccessLevel.GameMaster )]
		public bool active
		{
			get { return m_active; }
			set {m_active=value;UpdateStuff();	}
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public LinkedPillarController controller
		{
			get { return m_linkedpillarcontroller; }
			set { m_linkedpillarcontroller=value;UpdateStuff();}
		}	
		

		[CommandProperty( AccessLevel.GameMaster )]
		public string CompletionPrize
		{
			get { 
			if(controller==null)return "";
			return controller.giveitem; 
			}
		}	

		//activates pillar and checks against controller to see if this was the last one, returns true if it was
		public bool Activate()
		{
		if(this.controller==null || this.controller.Deleted)return false;
		
		controller.Add(this);
		active=true;
		
			if(controller.IsAllActive())
			{		
			controller.StartDeactivate();
			
			return true;
			}	
		
		
		return false;
		}


		
		public override void OnDoubleClick( Mobile from )
		{	
		if(!from.InRange(this,2) || this.controller==null || this.controller.Deleted)return;
			
			if(active)
			{
			from.SendMessage("This has already been activated");
			return;
			}
		
		from.Target = new InternalTarget( from,this );
		}	
	
		public override bool DisplayWeight { get { return ( false ); } }		

		public class InternalTarget : Target
		{
		LinkedPillar pillar;
		Mobile mob;

			public InternalTarget(Mobile pmob,LinkedPillar ppillar) : base( 12, false, TargetFlags.None )
			{
			pillar= ppillar;
			mob=pmob;
						
			mob.SendMessage("Target {0}",pillar.requireditemname);
			}

			protected override void OnTarget( Mobile from, object o )
			{
				if(!(o is Item))
				{
				from.SendMessage("This only works on items!");
				return;
				}
				
				Item item = o as Item;
				
				if(from.Backpack==null || !item.IsChildOf( from.Backpack ) || !from.InRange(pillar,2))return;
				
			
				if(pillar.requireditem==null || pillar.requireditem=="")
				{
				from.SendMessage("ERROR: Pillar not configured");
				return;
				}
			
				Type reqtype = SpawnerType.GetType( pillar.requireditem );
				Type giventype = item.GetType();
				
				if(reqtype==null || giventype==null)
				{
				from.SendMessage("ERROR: reqtype or giventype returned null");
				return;
				}
				
				if(reqtype==giventype)
				{

					if(pillar.CompletionPrize==null || pillar.CompletionPrize=="")
					{
					from.SendMessage("ERROR: Pillar prize not properly configured");
					return;
					}
				
					if(item.Amount>1)item.Amount--;
					else item.Delete();
				
					if(pillar.Activate()) //returns true if it was the last pillar
					{				
					Type type = SpawnerType.GetType( (string)pillar.CompletionPrize );
					
						if(type==null)
						{
						from.SendMessage("ERROR: type returned null");
						return;
						}					
					
						try
						{
							object obj = Activator.CreateInstance( type );
							
							if(obj==null)
							{
							from.SendMessage("ERROR: Activator.CreateInstance() returned null");
							return;
							}
							
							if( obj is Item )
							{
								Item prize = (Item)obj;
								
								if(prize!=null)
								{
								from.SendMessage("You have activated the last pillar and received your prize!");
								from.GiveItem(prize);
								}
								else from.SendMessage("ERROR: Failed generating item of type {0}.  Contact a GM.",pillar.CompletionPrize);
							}
							else if( obj is BaseCreature )
							{
								BaseCreature prizepet = (BaseCreature)obj;
								
								if(prizepet!=null)
								{
								from.SendMessage("You have activated the last pillar and received a new pet!");
								
								prizepet.Controlled=true;
								prizepet.ControlMaster=from;
								prizepet.IsBonded=true;		
								prizepet.MoveToWorld(from.Location,from.Map);
								}
								else from.SendMessage("ERROR: Failed generating item of type {0}.  Contact a GM.",pillar.CompletionPrize);
							}	
							
						}
						catch
						{
						from.SendMessage("ERROR: Exeption generating item of type {0}.  Contact a GM.",pillar.CompletionPrize);
						Console.WriteLine("ERROR: Linked Pillar {0} Could not create prize",pillar.Serial);
						}
						
					}				
				}
				else from.SendMessage("That is an invalid item!");
				
			}

			protected override void OnTargetFinish( Mobile from )
			{				
			}
		}		
		


		[Constructable]
		public LinkedPillar() : base(8317)
		{		
		Movable=false;
		Hue=0x966;
		Name="Linked Pillar";
		}


		public LinkedPillar( Serial serial ) : base( serial )
		{
		}

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
			writer.Write( (int) 0); // version
			writer.Write(m_Hue1);
			writer.Write(m_Hue2);			
			writer.Write(m_ItemID1);
			writer.Write(m_ItemID2);
			
			writer.Write(m_requireditem);
			writer.Write(m_requireditemname);
			
			writer.WriteItem(m_linkedpillarcontroller);
			writer.Write(m_active);	

		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
			int version = reader.ReadInt();
			
			m_Hue1=reader.ReadInt();
			m_Hue2=reader.ReadInt();
			m_ItemID1=reader.ReadInt();
			m_ItemID2=reader.ReadInt();
			
			m_requireditem=reader.ReadString();
			m_requireditemname=reader.ReadString();
			
			m_linkedpillarcontroller=reader.ReadItem() as LinkedPillarController;
			m_active=reader.ReadBool();		
			}
	}
}