/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by Red Squirrel on Jan-02-2010 02:32:38pm
Filepath: scripts\_custom\items\misc\staff\eventstone.cs
Lines of code: 318
***********************************************************/


using System;
using Server.Items;
using Server.Gumps;
using System.Collections;
using System.Collections.Generic;
using Server.Network;
using Server.Mobiles;

namespace Server.Items
{
	public class eventstone : Item
	{
	public override bool DisplayWeight { get { return ( false ); } }
	
	private string m_eventname;
	private string m_message;
	bool m_active;
	
	private Point3D m_location;
	private Map m_map;

	private ArrayList m_moongates;	
	

		[CommandProperty( AccessLevel.GameMaster )]
		public string EventName
		{
			get { return m_eventname; }
			set { m_eventname = value; InvalidateProperties();Modify(); }
		}
		
		[CommandProperty( AccessLevel.GameMaster )]
		public bool Active
		{
			get { return m_active; }
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public string PopupMessage
		{
			get { return m_message; }
			set { m_message = value;Modify(); }
		}
		
		[CommandProperty( AccessLevel.GameMaster )]
		public Point3D TeleLocation
		{
			get { return m_location; }
			set { m_location = value;Modify(); }
		}
	
		[CommandProperty( AccessLevel.GameMaster )]
		public Map EventMap
		{
			get { return m_map; }
			set { m_map = value;Modify(); }
		}
	
	
	
		public override string DefaultName
		{
			get { return String.Format("Event Stone for {0}",m_eventname); }
		}

		[Constructable]
		public eventstone() : base( 0xED4 )
		{
			Movable = false;
			m_eventname="Undefined Event";
			m_active=false;
			
			m_moongates = new ArrayList();
		}

		public override void OnDoubleClick( Mobile from )
		{	
			if(from.AccessLevel<AccessLevel.Counselor)
			{
			//send player gump
			if(m_active) TeleportPlayer(from);
			return;
			}
		
			//send GM gump
			from.SendGump(new eventadmingump(this));
		}
		
		public void TeleportPlayer(Mobile from)
		{
			if(!m_active)
			{
			from.SendMessage("Sorry, this event is now closed.");
			
			return;
			}
		
		
		from.MoveToWorld(m_location,m_map);
		from.SendMessage("You have been teleported to the event");
		}
		
		
		
		
		
		private static Point3D[] m_TramFelLocs = new Point3D[]
		{
			new Point3D(1430,1697,0), //brit bank
			new Point3D(2240,1195,0), //cove bank
			new Point3D(1336,3769,0), //jhelom bank
			new Point3D(3731,2163,20), //magencia bank
			new Point3D(2511,561,0), //minoc bank
			new Point3D(4455,1168,0), //moonglow bank
			new Point3D(3765,1315,0), //nujelm bank
			new Point3D(3503,2574,14), //haven bank / occelo (middle of nowhere due to map difference)
			new Point3D(2892,3482,12), //serpents hold bank
			new Point3D(603,2150,0), //skara bank
			new Point3D(1823,2329,0), //trinsic bank
			new Point3D(1914,2688,2), //trinsic bank 2
			new Point3D(2898,687,0), //vesper bank
			new Point3D(5344,93,15), //wind bank
			new Point3D(634,863,0), //yew bank
			
			new Point3D(1461,844,0), //chaos shrine
			new Point3D(1857,865,0), //compassion shrine
			new Point3D(4220,568,36), //honesty shrine
			new Point3D(1729,3519,0), //honor shrine
			new Point3D(4269,3703,0), //humility shrine
			new Point3D(1300,645,6), //justice shrine
			new Point3D(3355,301,9), //sacrifice shrine
			new Point3D(1601,2485,5), //spirituality shrine
			new Point3D(2489,3926,0) //valor shrine			
		};
		
		private static Point3D[] m_IlshLocs = new Point3D[]
		{
			new Point3D(1217,469,-13), //compassion
			new Point3D(722,1354,-61), //honesty
			new Point3D(748,726,-28), //honor
			new Point3D(289,1016,0), //humilty
			new Point3D(984,999,-27), //justice
			new Point3D(1179,1287,-30),//sacrifice
			new Point3D(1540,1340,-3), //spirituality
			new Point3D(523,216,-48), //valor
			new Point3D(1716,218,96) //chaos
		};

		private static Point3D[] m_MalasLocs = new Point3D[]
		{
			new Point3D(1015,507,-70), //luna
			new Point3D(1100,485,-89), //gate library
			new Point3D(2047,1355,-85) //umbra bank	
		};

		private static Point3D[] m_TokunoLocs = new Point3D[]
		{
			new Point3D(1044,517,15), //isamu shrine
			new Point3D(1170,991,39), //isamu gate
			new Point3D(800,1202,25), //zento gate
			new Point3D(735,1255,30), //zento bank
			new Point3D(264,622,7) //homare gate		
		};		
		
		
		
		
	
		private void AddGate(Point3D location, Map map)
		{
		Moongate tmp = new Moongate(m_location,m_map);
		tmp.Hue=1150;
		tmp.Name=String.Format("{0} Event",m_eventname);
		tmp.MoveToWorld(location,map);		
		
		Effects.PlaySound( tmp.GetWorldLocation(), tmp.Map, 0x20E );
		
		m_moongates.Add(tmp);		
		Modify();
		}
		
		
		public void Activate(Mobile from)
		{
		if(from.AccessLevel<AccessLevel.Counselor || m_active)return;
		
		Modify();
		
		from.SendMessage("Activating event...");
		
		//add all moongates
		
			for(int i=0;i<m_TramFelLocs.Length;i++)
			{
			AddGate(m_TramFelLocs[i], Map.Trammel);
			AddGate(m_TramFelLocs[i], Map.Felucca);		
			}
			
			for(int i=0;i<m_IlshLocs.Length;i++)	AddGate(m_IlshLocs[i], Map.Ilshenar);	
			for(int i=0;i<m_MalasLocs.Length;i++)	AddGate(m_MalasLocs[i], Map.Malas);	
			for(int i=0;i<m_TokunoLocs.Length;i++)	AddGate(m_TokunoLocs[i], Map.Tokuno);	

			
			
		//send gump to all online players
		
				ArrayList list = new ArrayList();

				List<NetState> states = NetState.Instances;

				for ( int i = 0; i < states.Count; ++i )
				{
					NetState ns = states[i];
					Mobile mob = ns.Mobile;

					if ( mob == null )
						continue;

						list.Add( mob );
				}	
				
				for(int i=0;i<list.Count;i++)
				{
					if(list[i] is PlayerMobile)
					{
					PlayerMobile pm = list[i] as PlayerMobile;
					pm.SendGump(new eventgump(this));
					}
				}
		
		
		m_active=true;
		from.SendMessage("Done");		
		}
		
		
		
		
		public void DeActivate(Mobile from)
		{		
		if(from.AccessLevel<AccessLevel.Counselor || !m_active)return;
		
		from.SendMessage("Deactivating Event");
		
		Modify();
		
			//clear gates		
			for(int i=0;i<m_moongates.Count;i++)
			{
				if(m_moongates[i] is Item)
				{
				Item tmp= m_moongates[i] as Item;
				Effects.PlaySound( tmp.GetWorldLocation(), tmp.Map, 0x201 );
				tmp.Delete();		
				}		
			}
			
			
			//remove gump to all online players
		
				ArrayList list = new ArrayList();

				List<NetState> states = NetState.Instances;

				for ( int i = 0; i < states.Count; ++i )
				{
					NetState ns = states[i];
					Mobile mob = ns.Mobile;

					if ( mob == null )
						continue;

						list.Add( mob );
				}	
				
				for(int i=0;i<list.Count;i++)
				{
					if(list[i] is PlayerMobile)
					{
					PlayerMobile pm = list[i] as PlayerMobile;
					pm.CloseGump(typeof(eventgump));
					}
				}			
			
		
		
		m_active=false;
		from.SendMessage("Done");		
		}
		
		
		

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

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
			writer.Write( (int) 0 ); // version
			
			writer.Write(m_eventname);
			writer.Write(m_message);			
			writer.Write(m_active);
			
			writer.Write(m_location);
			writer.Write(m_map);			
			
			writer.Write(m_moongates.Count);
			if(m_moongates.Count>0)writer.WriteItemList(m_moongates,false);
			
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
			int version = reader.ReadInt();
			
			m_eventname=reader.ReadString();
			m_message=reader.ReadString();
			m_active=reader.ReadBool();
			
			m_location=reader.ReadPoint3D();
			m_map=reader.ReadMap();
			
			int gates = reader.ReadInt();
			
			m_moongates = new ArrayList();
			
			if(gates>0)	m_moongates=reader.ReadItemList();			
		}
	}
}



namespace Server.Gumps
{
//PLAYER GUMP:

	public class eventgump : Gump
	{
	eventstone m_stone;
		public eventgump(eventstone stone): base( 200, 200 )
		{
			m_stone=stone;
			this.Closable=true;
			this.Disposable=true;
			this.Dragable=true;
			this.Resizable=false;
			this.AddPage(0);
			this.AddBackground(0, 0, 407, 401, 83);
			this.AddLabel(28, 12, 1149, @stone.EventName);
			this.AddButton(177, 350, 1154, 1155, (int)Buttons.Button1, GumpButtonType.Reply, 0);
			this.AddLabel(208, 350, 1149, @"Join now!");
			this.AddImage(326, 4, 11567);
			
			this.AddHtml( 25, 42, 355, 310, @String.Format("<font color=#FFFFFF>A public event has been opened.  Click the button to join from here, or head to the nearest bank or shrine.<br><br><i>{0}</i></font>",stone.PopupMessage), (bool)false, (bool)true);
			
			
		}
		
		public enum Buttons
		{
			Exit,
			Button1,
		}
		
		public override void OnResponse( NetState state, RelayInfo info )
		{
			Mobile from = state.Mobile;

			if ( info.ButtonID == 1 )
			{
			m_stone.TeleportPlayer(from);
			}
		}	
	}
	
	

	//GM GUMP:
	
	public class eventadmingump : Gump
	{
	eventstone m_stone;
	
		public eventadmingump(eventstone stone): base( 200, 200 )
		{
		m_stone=stone;
		
		string statustxt="Status: OFF";
		if(stone.Active)statustxt="Status: ON";
		
			this.Closable=true;
			this.Disposable=true;
			this.Dragable=true;
			this.Resizable=false;
			this.AddPage(0);
			this.AddBackground(0, 0, 416, 270, 83);
			this.AddLabel(40, 12, 1149, @"Event Control Stone for");
			this.AddLabel(195, 12, 1149, @stone.EventName);
			this.AddButton(17, 213, 1154, 1155, (int)Buttons.Button1, GumpButtonType.Reply, 0);
			this.AddLabel(51, 214, 1149, @"Activate");
			this.AddButton(135, 214, 1154, 1155, (int)Buttons.Button2, GumpButtonType.Reply, 0);
			this.AddLabel(167, 216, 1149, @"Deactivate");
			
			this.AddLabel(290, 216, 1149, statustxt);
			
			this.AddImage(326, 4, 11567);
			this.AddHtml( 24, 60, 359, 129, @String.Format("<font color=#FFFFFF>{0}</font>",stone.PopupMessage), (bool)false, (bool)true);
		}
		
		public enum Buttons
		{
			Exit,
			Button1,
			Button2,
		}
		
		
		public override void OnResponse( NetState state, RelayInfo info )
		{
			Mobile from = state.Mobile;

			if ( info.ButtonID == 1 )
			{
			m_stone.Activate(from);
			}
			else if(info.ButtonID == 2)
			{
			m_stone.DeActivate(from);
			}

		}		
		

	}	
		
}









