using System;
using Server.Engines.Craft;

namespace Server.Items
{
	public enum GemType
	{
		None,
		StarSapphire,
		Emerald,
		Sapphire,
		Ruby,
		Citrine,
		Amethyst,
		Tourmaline,
		Amber,
		Diamond
	}

	public abstract class BaseJewel : Item, ICraftable
	{
		private CraftResource m_Resource;
		private GemType m_GemType;
		private int m_StrBonus = -1, m_DexBonus = -1, m_IntBonus = -1;

		public virtual int UOStrBonus{ get{ return 0; } }
		public virtual int UODexBonus{ get{ return 0; } }
		public virtual int UOIntBonus{ get{ return 0; } }

		[CommandProperty( AccessLevel.GameMaster )]
		public int StrBonus
		{
			get{ return ( m_StrBonus == -1 ? UOStrBonus : m_StrBonus ); }
			set{ m_StrBonus = value; InvalidateProperties(); }
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public int DexBonus
		{
			get{ return ( m_DexBonus == -1 ? UODexBonus : m_DexBonus ); }
			set{ m_DexBonus = value; InvalidateProperties(); }
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public int IntBonus
		{
			get{ return ( m_IntBonus == -1 ? UOIntBonus : m_IntBonus ); }
			set{ m_IntBonus = value; InvalidateProperties(); }
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public CraftResource Resource
		{
			get{ return m_Resource; }
			set{ m_Resource = value; Hue = CraftResources.GetHue( m_Resource ); }
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public GemType GemType
		{
			get{ return m_GemType; }
			set{ m_GemType = value; InvalidateProperties(); }
		}

		public virtual int BaseGemTypeNumber{ get{ return 0; } }

		public override int LabelNumber
		{
			get
			{
				if ( m_GemType == GemType.None )
					return base.LabelNumber;

				return BaseGemTypeNumber + (int)m_GemType - 1;
			}
		}

		public override void OnAfterDuped( Item newItem )
		{
			BaseJewel jewel = newItem as BaseJewel;

			if ( jewel == null )
				return;
		}

		public BaseJewel( int itemID, Layer layer ) : base( itemID )
		{
			m_Resource = CraftResource.Iron;
			m_GemType = GemType.None;

			switch ( Utility.Random( 9 ) )
			{
				case 0: m_GemType = GemType.StarSapphire; break;
				case 1: m_GemType = GemType.Emerald; break;
				case 2: m_GemType = GemType.Sapphire; break;
				case 3: m_GemType = GemType.Ruby; break;
				case 4: m_GemType = GemType.Citrine; break;
				case 5: m_GemType = GemType.Amethyst; break;
				case 6: m_GemType = GemType.Tourmaline; break;
				case 7: m_GemType = GemType.Amber; break;
				case 8: m_GemType = GemType.Diamond; break;
			}

			Layer = layer;
		}

		public int ComputeStatBonus( StatType type )
		{
			if ( type == StatType.Str )
				return StrBonus;
			else if ( type == StatType.Dex )
				return DexBonus;
			else
				return IntBonus;
		}

		public override void OnAfterSpawn()
		{
		}

		public virtual void AddStatBonuses( Mobile parent )
		{
			if ( parent == null )
				return;

			int strBonus = ComputeStatBonus( StatType.Str );
			int dexBonus = ComputeStatBonus( StatType.Dex );
			int intBonus = ComputeStatBonus( StatType.Int );

			if ( strBonus == 0 && dexBonus == 0 && intBonus == 0 )
				return;

			string modName = this.Serial.ToString();

			if ( strBonus != 0 )
				parent.AddStatMod( new StatMod( StatType.Str, modName + "Str", strBonus, TimeSpan.Zero ) );

			if ( dexBonus != 0 )
				parent.AddStatMod( new StatMod( StatType.Dex, modName + "Dex", dexBonus, TimeSpan.Zero ) );

			if ( intBonus != 0 )
				parent.AddStatMod( new StatMod( StatType.Int, modName + "Int", intBonus, TimeSpan.Zero ) );
		}

		public override bool OnEquip( Mobile from )
		{
			from.CheckStatTimers();

			int strBonus = ComputeStatBonus( StatType.Str );
			int dexBonus = ComputeStatBonus( StatType.Dex );
			int intBonus = ComputeStatBonus( StatType.Int );

			if ( strBonus != 0 || dexBonus != 0 || intBonus != 0 )
			{
				string modName = this.Serial.ToString();

				if ( strBonus != 0 )
					from.AddStatMod( new StatMod( StatType.Str, modName + "Str", strBonus, TimeSpan.Zero ) );

				if ( dexBonus != 0 )
					from.AddStatMod( new StatMod( StatType.Dex, modName + "Dex", dexBonus, TimeSpan.Zero ) );

				if ( intBonus != 0 )
					from.AddStatMod( new StatMod( StatType.Int, modName + "Int", intBonus, TimeSpan.Zero ) );
			}

			return base.OnEquip( from );
		}

		public override void OnAdded( object parent )
		{
			Mobile mob = parent as Mobile;

			if ( mob != null )
			{
				AddStatBonuses( mob );
				mob.CheckStatTimers();
			}

			base.OnAdded( parent );
		}

		public override void OnRemoved( object parent )
		{
			if ( parent is Mobile )
			{
				Mobile m = (Mobile)parent;
				string modName = this.Serial.ToString();

				m.RemoveStatMod( modName + "Str" );
				m.RemoveStatMod( modName + "Dex" );
				m.RemoveStatMod( modName + "Int" );

				m.CheckStatTimers();
			}

			base.OnRemoved( parent );
		}

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

		public override void GetProperties( ObjectPropertyList list )
		{
			base.GetProperties( list );

			if ( m_StrBonus > 0 && m_StrBonus < 3 )
				list.Add( 1038027 ); // Great Strength
			else if ( m_StrBonus > 2 && m_StrBonus < 5 )
				list.Add( 1038028 ); // Greater Strength
			else if ( m_StrBonus > 4 )
				list.Add( 1038029 ); // Ultimate Strength

			if ( m_DexBonus > 0 && m_DexBonus < 3 )
				list.Add( 1038033 ); // Great Agility
			else if ( m_DexBonus > 2 && m_DexBonus < 5 )
				list.Add( 1038034 ); // Greater Agility
			else if ( m_DexBonus > 4 )
				list.Add( 1038035 ); // Ultimate Agility

			if ( m_IntBonus > 0 && m_IntBonus < 3 )
				list.Add( 1038030 ); // Great Intellect
			else if ( m_IntBonus > 2 && m_IntBonus < 5 )
				list.Add( 1038031 ); // Greater Intellect
			else if ( m_IntBonus > 4 )
				list.Add( 1038032 ); // Ultimate Intellect

			if ( Magical > 0 && Uses > 0 )
			{
				list.Add( 1062520, "\t#{0}", Magical );
			}

			if ( Uses > 0 && Server.Misc.Settings.DisplayInfo() )
			{
				if ( Uses > 1 )
					list.Add( 1062516, "{0}", Uses );
				else
					list.Add( 1062517, "{0}", Uses );
			}
			else if ( Uses > 0 )
			{
				double usage = (double)Uses / (double)UsesMax;

				if ( usage > 0.95 )
					list.Add( 1063269 ); // Fully Charged
				else if ( usage > 0.75 )
					list.Add( 1063270 ); // Mostly Charged
				else if ( usage > 0.50 )
					list.Add( 1063271 ); // Partially Charged
				else if ( usage > 0.25 )
					list.Add( 1063272 ); // Somewhat Charged
				else if ( usage > 0.05 )
					list.Add( 1063273 ); // Barely Charged
				else
					list.Add( 1063274 ); // Almost Drained
			}

			if ( m_GemType == GemType.StarSapphire )
				list.Add( 1044231 );
			else if ( m_GemType == GemType.Emerald )
				list.Add( 1044232 );
			else if ( m_GemType == GemType.Sapphire )
				list.Add( 1044233 );
			else if ( m_GemType == GemType.Ruby )
				list.Add( 1044234 );
			else if ( m_GemType == GemType.Citrine )
				list.Add( 1044235 );
			else if ( m_GemType == GemType.Amethyst )
				list.Add( 1044236 );
			else if ( m_GemType == GemType.Tourmaline )
				list.Add( 1044237 );
			else if ( m_GemType == GemType.Amber )
				list.Add( 1044238 );
			else if ( m_GemType == GemType.Diamond )
				list.Add( 1044239 );
		}

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

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

			writer.WriteEncodedInt( (int) m_StrBonus );
			writer.WriteEncodedInt( (int) m_DexBonus );
			writer.WriteEncodedInt( (int) m_IntBonus );
			writer.WriteEncodedInt( (int) m_Resource );
			writer.WriteEncodedInt( (int) m_GemType );
		}

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

			int version = reader.ReadInt();

			m_StrBonus = reader.ReadEncodedInt();
			m_DexBonus = reader.ReadEncodedInt();
			m_IntBonus = reader.ReadEncodedInt();
			m_Resource = (CraftResource)reader.ReadEncodedInt();
			m_GemType = (GemType)reader.ReadEncodedInt();

			Mobile parent = Parent as Mobile;

			if ( parent != null )
			{
				AddStatBonuses( parent );
				parent.CheckStatTimers();
			}
		}

		#region ICraftable Members

		public int OnCraft( int quality, bool makersMark, Mobile from, CraftSystem craftSystem, Type typeRes, BaseTool tool, CraftItem craftItem, int resHue )
		{
			Type resourceType = typeRes;

			if ( resourceType == null )
				resourceType = craftItem.Resources.GetAt( 0 ).ItemType;

			Resource = CraftResources.GetFromType( resourceType );

			CraftContext context = craftSystem.GetContext( from );

			if ( context != null && context.DoNotColor )
				Hue = 0;

			if ( 1 < craftItem.Resources.Count )
			{
				resourceType = craftItem.Resources.GetAt( 1 ).ItemType;

				if ( resourceType == typeof( StarSapphire ) )
					GemType = GemType.StarSapphire;
				else if ( resourceType == typeof( Emerald ) )
					GemType = GemType.Emerald;
				else if ( resourceType == typeof( Sapphire ) )
					GemType = GemType.Sapphire;
				else if ( resourceType == typeof( Ruby ) )
					GemType = GemType.Ruby;
				else if ( resourceType == typeof( Citrine ) )
					GemType = GemType.Citrine;
				else if ( resourceType == typeof( Amethyst ) )
					GemType = GemType.Amethyst;
				else if ( resourceType == typeof( Tourmaline ) )
					GemType = GemType.Tourmaline;
				else if ( resourceType == typeof( Amber ) )
					GemType = GemType.Amber;
				else if ( resourceType == typeof( Diamond ) )
					GemType = GemType.Diamond;
			}

			return 1;
		}

		#endregion
	}
}