/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by 
Filepath: scripts\_custom\quests\bodquest\BodQuest.cs
Lines of code: 233
***********************************************************/


using System;
using Server;
using Server.Mobiles;
using Server.Items;

namespace Server.Engines.Quests.Bods
{

	public class BodQuest : QuestSystem
	{
		private static Type[] m_TypeReferenceTable = new Type[]
			{
				typeof( Bods.DontOfferConversation ),
				typeof( Bods.AcceptConversation ),
				typeof(	Bods.NotDoneConversation),
				typeof(	Bods.GotResourcesConversation),
				typeof(	Bods.DoneCraftConversation),
				typeof( Bods.EndConversation )				
			};

		public override Type[] TypeReferenceTable{ get{ return m_TypeReferenceTable; } }

		public override object Name
		{
			get
			{
				return "Bulk Order Quest";
			}
		}

		public override object OfferMessage
		{
			get
			{
				return "Good day to you!  Our corporate operations has changed.<br><br>We have been given the proper funding to create factories across all of Britania and no longer need to outsource all of our fabrication through bulk order deeds!<br><br>But we still need your help.  This mass production has lead to a shortage of supplies and resources!<br><br>Would you like to assist me with the harvesting and shipment of such resources?";
			}
		}
		
		public Mobile Vendor;
		public string ResourceName="default";
		public string ResourceType="none";
		public int ResourceAmount=10;
		public int PrizeLevel=10;		
		public int Gave=0;
		

		public override TimeSpan RestartDelay{ get{ return TimeSpan.Zero; } }
		public override bool IsTutorial{ get{ return false; } }

		public override int Picture{ get{ return 0x15b7; } }


		public BodQuest( PlayerMobile from,Mobile pVendor,BodQuestType BQT) : base( from )
		{
		Vendor=pVendor;
		ResourceName=BQT.ResourceName;
		ResourceType=BQT.ResourceType;
		ResourceAmount=BQT.ResourceAmount;
		PrizeLevel=BQT.PrizeLevel;
		}
		
		
		public bool ValidateDroppedItem(Item dropped,Mobile vendor)
		{
		if(vendor!=Vendor)return false;
		
		Type DT = dropped.GetType();
		Type NT = SpawnerType.GetType(ResourceType);
		
		if(DT==null || NT==null)return false;	
		
		if(DT != NT) return false;
			
		int togive=dropped.Amount;
		int left=ResourceAmount-Gave;
		if(left==0)return false;

			if(left<dropped.Amount)
			{
			togive=left;			
			}
			
		if(togive==dropped.Amount)dropped.Delete();
		else dropped.Amount-=togive;
		
		Gave+=togive;
				
		return true;		
		}
		
		public bool ResourceTypeIsCraftRes()
		{
		CraftResource ReqResource=CraftResources.GetFromType(SpawnerType.GetType(ResourceType));
		if(ReqResource==CraftResource.None)return false;
		return true;
		}
		
		
		public bool ValidateCraftedItem(Item item)
		{
		CraftResource ItemResource;
		CraftResource ReqResource=CraftResources.GetFromType(SpawnerType.GetType(ResourceType));
		
		if(item is BaseWeapon)ItemResource=((BaseWeapon)item).Resource;
		else if(item is BaseArmor)ItemResource=((BaseArmor)item).Resource;
		else return false;
			
			
		if(ReqResource==ItemResource)return true;
		
		return false;
		}
		
		

		// Serialization
		public BodQuest()
		{
		}

		public override void ChildDeserialize( GenericReader reader )
		{
			int version = reader.ReadEncodedInt();
			
			Vendor=reader.ReadMobile();
			Gave=reader.ReadInt();
			ResourceName=reader.ReadString();
			ResourceType=reader.ReadString();
			ResourceAmount=reader.ReadInt();
			PrizeLevel=reader.ReadInt();
		}

		public override void ChildSerialize( GenericWriter writer )
		{
			writer.WriteEncodedInt( (int) 0 ); // version
			
			writer.WriteMobile(Vendor);
			writer.Write(Gave);
			writer.Write(ResourceName);
			writer.Write(ResourceType);
			writer.Write(ResourceAmount);
			writer.Write(PrizeLevel);
		}

		public override void Accept()
		{
			base.Accept();

			AddConversation( new AcceptConversation() );
		}

		public static void GiveRewardTo( PlayerMobile from)
		{
			BodQuest qs = from.Quest as BodQuest;
			if(qs==null)return; //this should not happen, but just to be safe
			
			BaseVendor vend= qs.Vendor as BaseVendor;			
			
			if(vend !=null)
			{			
			Item reward=vend.GenerateBodQuestReward(new BodQuestType(qs.ResourceName,qs.ResourceType,qs.ResourceAmount,qs.PrizeLevel));		
			if(reward!=null)from.GiveItem(reward);			
			}
			
			from.NextBulkOrder=TimeSpan.FromMinutes(1+Utility.Random(5));
			
			int fame=10*qs.PrizeLevel*(1+(qs.ResourceAmount/75));
			
			Server.Misc.Titles.AwardFame( from, fame, true );
			
			//end quest
			qs.Complete();			
		}
	}


	
	

	public class CollectResourcesObjective : QuestObjective
	{
		public override object Message
		{
			get
			{
				if(System==null || !(System is BodQuest))return "Quest objective error: Contact the administrator.";
				
				BodQuest bq = System as BodQuest;	
				
				string vendorname,vendortitle;
				
				if(bq.Vendor==null || bq.Vendor.Deleted)
				{
				vendorname="any ";
				vendortitle="vendor";
				}
				else
				{
				vendorname=bq.Vendor.Name;
				vendortitle=bq.Vendor.Title;
				}					

				return (object)String.Format("Get {0} {1} and bring them to {2} {3}.",bq.ResourceAmount,bq.ResourceName,vendorname,vendortitle); 
			}
		}

		public CollectResourcesObjective()
		{
		}

		public override void OnComplete()
		{
		if(System is BodQuest && ((BodQuest)System).ResourceTypeIsCraftRes())System.AddConversation( new GotResourcesConversation() );
		else System.AddConversation( new EndConversation() );
		}
		
		public override void RenderMessage( BaseQuestGump gump )
		{
				if(System==null || !(System is BodQuest))return;				
				BodQuest bq = System as BodQuest;
		
				string vendorname,vendortitle;
				
				if(bq.Vendor==null || bq.Vendor.Deleted)
				{
				vendorname="any ";
				vendortitle="vendor";
				}
				else
				{
				vendorname=bq.Vendor.Name;
				vendortitle=bq.Vendor.Title;
				}		

		
			if ( bq.Gave > 0 && bq.Gave < bq.ResourceAmount )
				gump.AddHtmlObject( 70, 130, 270, 100,String.Format("Get {0} {1} and bring them to {2} {3}.",bq.ResourceAmount,bq.ResourceName,vendorname,vendortitle), BaseQuestGump.Blue, false, false ); 
			else	base.RenderMessage( gump );
		}

		public override void RenderProgress( BaseQuestGump gump )
		{
				if(System==null || !(System is BodQuest))return;				
				BodQuest bq = System as BodQuest;				
		
			if ( bq.Gave > 0 && bq.Gave < bq.ResourceAmount )
			{
				gump.AddHtmlObject( 70, 260, 270, 100, "Resources brought", BaseQuestGump.Blue, false, false ); 

				gump.AddLabel( 70, 280, 100, bq.Gave.ToString() );
				gump.AddLabel( 100, 280, 100, "/" );
				gump.AddLabel( 130, 280, 100, bq.ResourceAmount.ToString() );
			}
			else
			{
				base.RenderProgress( gump );
			}
		}
	}
	
	
	
	
	
	public class CraftItemObjective : QuestObjective
	{
		public override object Message
		{
			get
			{
				if(System==null || !(System is BodQuest))return "Quest objective 2 error: Contact the administrator.";
				
				BodQuest bq = System as BodQuest;	

				string vendorname,vendortitle;
				
				if(bq.Vendor==null || bq.Vendor.Deleted)
				{
				vendorname="any ";
				vendortitle="vendor";
				}
				else
				{
				vendorname=bq.Vendor.Name;
				vendortitle=bq.Vendor.Title;
				}
			
				return (object)String.Format("Craft some items with {0} and when your special bag turns blue, bring it to {1} {2}.  The items must be of decent challenge for your skill.",bq.ResourceName,vendorname,vendortitle); 
			}
		}

		public CraftItemObjective()
		{
		}

		public override void OnComplete()
		{
		System.AddConversation( new DoneCraftConversation() );
		}
		
		public override void RenderMessage( BaseQuestGump gump )
		{
		base.RenderMessage( gump );
		}

		public override void RenderProgress( BaseQuestGump gump )
		{
		base.RenderProgress( gump );
		}
	}	

	
}