using System;
using Server;
using Server.Misc;
using System.Collections.Generic;
using System.Collections;
using Server.Network;
using Server.Mobiles;
using Server.Items;
using Server.ContextMenus;

namespace Server.Mobiles
{
	public abstract class BaseGuildmaster : BaseVendor
	{
		private List<SBInfo> m_SBInfos = new List<SBInfo>();
		protected override List<SBInfo> SBInfos{ get { return m_SBInfos; } }

		public override bool IsActiveVendor{ get{ return true; } }

		public override bool ClickTitle{ get{ return false; } }

		public virtual int JoinCost{ get{ return 1000; } }

		public override void InitSBInfo()
		{
		}

		public virtual void SayGuildTo( Mobile m )
		{
			SayTo( m, 1008055 + (int)NpcGuild );
		}

		public virtual void SayWelcomeTo( Mobile m )
		{
			SayTo( m, "Welcome to the guild! Thou shalt find it beneficial to your future endeavors." );
		}

		public virtual void SayPriceTo( Mobile m )
		{
			m.Send( new MessageLocalizedAffix( Serial, Body, MessageType.Regular, SpeechHue, 3, 1008052, Name, AffixType.Append, JoinCost.ToString(), "" ) );
		}

		public virtual bool WasNamed( string speech )
		{
			string name = this.Name;

			return ( name != null && Insensitive.StartsWith( speech, name ) );
		}

		public override bool HandlesOnSpeech( Mobile from )
		{
			if ( from.InRange( this.Location, 2 ) )
				return true;

			return base.HandlesOnSpeech( from );
		}

		public override void GetContextMenuEntries( Mobile from, List<ContextMenuEntry> list ) 
		{
			PlayerMobile pm = (PlayerMobile)from;
			base.GetContextMenuEntries( from, list ); 
			if ( pm.NpcGuild != this.NpcGuild ){ list.Add( new JoinEntry( from, this ) ); }
			else if ( pm.NpcGuild == this.NpcGuild ){ list.Add( new ResignEntry( from, this ) ); }
		} 

		public class JoinEntry : ContextMenuEntry
		{
			private Mobile m_Mobile;
			private Mobile m_Guildmaster;
			
			public JoinEntry( Mobile from, Mobile guildmaster ) : base( 6116, 3 )
			{
				m_Mobile = from;
				m_Guildmaster = guildmaster;
			}

			public override void OnClick()
			{
			    if( !( m_Mobile is PlayerMobile ) )
				return;
				
				JoinGuild( m_Mobile, (BaseGuildmaster)m_Guildmaster );
            }
        }

		public class ResignEntry : ContextMenuEntry
		{
			private Mobile m_Mobile;
			private Mobile m_Guildmaster;
			
			public ResignEntry( Mobile from, Mobile guildmaster ) : base( 6115, 3 )
			{
				m_Mobile = from;
				m_Guildmaster = guildmaster;
			}

			public override void OnClick()
			{
			    if( !( m_Mobile is PlayerMobile ) )
				return;
				
				ResignGuild( m_Mobile, (BaseGuildmaster)m_Guildmaster );
            }
        }

		public override void OnSpeech( SpeechEventArgs e )
		{
			Mobile from = e.Mobile;

			if ( !e.Handled && from is PlayerMobile && from.InRange( this.Location, 2 ) && WasNamed( e.Speech ) )
			{
				PlayerMobile pm = (PlayerMobile)from;

				if ( e.HasKeyword( 0x0004 ) ) // *join* | *member*
				{
					if ( pm.NpcGuild == this.NpcGuild )
						SayTo( from, 501047 ); // Thou art already a member of our guild.
					else if ( pm.NpcGuild != NpcGuild.None )
						SayTo( from, 501046 ); // Thou must resign from thy other guild first.
					else
						SayPriceTo( from );

					e.Handled = true;
				}
				else if ( e.HasKeyword( 0x0005 ) ) // *resign* | *quit*
				{
					if ( pm.NpcGuild != this.NpcGuild )
					{
						SayTo( from, 501052 ); // Thou dost not belong to my guild!
					}
					else
					{
						SayTo( from, 501054 ); // I accept thy resignation.
						pm.NpcGuild = NpcGuild.None;
						pm.InvalidateProperties();
					}

					e.Handled = true;
				}
			}

			base.OnSpeech( e );
		}

		public static void JoinGuild( Mobile player, BaseGuildmaster guildmaster )
		{
			PlayerMobile pm = (PlayerMobile)player;

			if ( pm.NpcGuild == ((BaseVendor)guildmaster).NpcGuild )
				guildmaster.SayTo( player, 501047 ); // Thou art already a member of our guild.
			else if ( pm.NpcGuild != NpcGuild.None )
				guildmaster.SayTo( player, 501046 ); // Thou must resign from thy other guild first.
			else
				guildmaster.SayPriceTo( player );
		}

		public static void ResignGuild( Mobile player, BaseGuildmaster guildmaster )
		{
			PlayerMobile pm = (PlayerMobile)player;

			if ( pm.NpcGuild != ((BaseVendor)guildmaster).NpcGuild )
			{
				guildmaster.SayTo( player, 501052 ); // Thou dost not belong to my guild!
			}
			else
			{
				guildmaster.SayTo( player, 501054 ); // I accept thy resignation.
				pm.NpcGuild = NpcGuild.None;
				pm.InvalidateProperties();
			}
		}

		public override bool OnGoldGiven( Mobile from, Gold dropped )
		{
			if ( from is PlayerMobile && dropped.Amount == JoinCost )
			{
				PlayerMobile pm = (PlayerMobile)from;

				if ( pm.NpcGuild == this.NpcGuild )
				{
					SayTo( from, 501047 ); // Thou art already a member of our guild.
				}
				else if ( pm.NpcGuild != NpcGuild.None )
				{
					SayTo( from, 501046 ); // Thou must resign from thy other guild first.
				}
				else if ( pm.Kills > 4 && this.NpcGuild != NpcGuild.AssassinsGuild && this.NpcGuild != NpcGuild.ThievesGuild )
				{
					SayTo( from, 501522 ); // I shall not deal with scum like thee!
				}
				else
				{
					SayWelcomeTo( from );

					pm.NpcGuild = this.NpcGuild;
					pm.NpcGuildJoinTime = DateTime.Now;
					pm.NpcGuildGameTime = pm.GameTime;
					pm.InvalidateProperties();

					dropped.Delete();
					return true;
				}

				return false;
			}

			return base.OnGoldGiven( from, dropped );
		}

		public BaseGuildmaster( string title ) : base( title )
		{
			Title = String.Format( "the {0} {1}", title, Female ? "guildmistress" : "guildmaster" );
		}

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

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );

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

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

			int version = reader.ReadInt();
		}
	}
}