/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by Death on Apr-22-2011 06:24:28pm
Filepath: scripts\_custom\events and scenarios\Trinsic Bridge Building\WoodBridgeStone.cs
Lines of code: 172
***********************************************************/


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

namespace Server.Items
{
	public class WoodBridgeStone : Item
	{
		private Mobile m_User;
		
		public int plainWoodCnt = 0, plainWoodMax = 5000;
		public int oakWoodCnt = 0, oakWoodMax = 2500;
		
		[Constructable]
		public WoodBridgeStone() : base(3797)
		{
			Name = "Donate Wood";
			Movable=false;
		}
		
		public override void OnDoubleClick( Mobile from )
		{
			if ( !from.InRange( this, 3 ) )
			{
				from.SendLocalizedMessage( 500446 ); // That is too far away.
				return;
			}
			
			from.CloseGump( typeof( BridgeWoodGump ) );
			from.SendGump(new BridgeWoodGump(this, from));
		}		
		
		
		public WoodBridgeStone( Serial serial ) : base( serial )
		{
		}
		
		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
			writer.Write( (int) 0 );

			writer.Write((int)plainWoodCnt );
			writer.Write((int)plainWoodMax);
			writer.Write((int)oakWoodCnt);
			writer.Write((int)oakWoodMax);
		}
		
		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
			int version = reader.ReadInt();	

			plainWoodCnt = reader.ReadInt();
			plainWoodMax = reader.ReadInt();
			oakWoodCnt = reader.ReadInt();
			oakWoodMax = reader.ReadInt();				
		}
	}
}

namespace Server.Gumps
{
    public class BridgeWoodGump : Gump
    {
        Mobile caller;

        [Usage("CommandName")]
        [Description("Makes a call to your custom gump.")]
        public static void CommandName_OnCommand(CommandEventArgs e)
        {
            Mobile from = e.Mobile;
        }
		
		private Mobile m_From;
		private int m_plainWoodCnt, m_plainWoodMax, m_oakWoodCnt, m_oakWoodMax;
		private int plainWoodHue, oakWoodHue;
		private WoodBridgeStone m_stone;
			
        public BridgeWoodGump(WoodBridgeStone stone, Mobile from) : base( 0, 0 )
        {
			m_plainWoodCnt = stone.plainWoodCnt; 
			m_plainWoodMax = stone.plainWoodMax;
			m_oakWoodCnt = stone.oakWoodCnt;
			m_oakWoodMax = stone.oakWoodMax;
			m_stone=stone;
			
            this.Closable=true;
			this.Disposable=true;
			this.Dragable=true;
			
			if(m_plainWoodCnt >= m_plainWoodMax)
				plainWoodHue = 152;
			else 
				plainWoodHue = 0;
				
			if(m_oakWoodCnt >= m_oakWoodMax)
				oakWoodHue = 152;
			else 
				oakWoodHue = 0;

			AddPage(0);
			AddBackground(12, 15, 557, 187, 9200);
			AddImage(26, 54, 2700);
			AddLabel(26, 26, 1195, @"Help the citizens of Britannia and the town of Trinsic construct a new bridge here");
			AddItem(28, 65, 7127);
			AddLabel(81, 76, plainWoodHue, @"Plain Boards" + " (" + m_plainWoodCnt + "/" + m_plainWoodMax + " needed)");
			AddLabel(82, 120, oakWoodHue, @"Oak Boards" + " (" + m_oakWoodCnt + "/" + m_oakWoodMax + " needed)");
			AddItem(27, 113, 7127, 1190);
			AddLabel(30, 166, 0, @"Donate Wood");
			AddLabel(271, 165, 0, @"Do Not Donate");
			AddButton(131, 164, 2151, 2151, (int)Buttons.Button1, GumpButtonType.Reply, 0);
			AddButton(366, 164, 2472, 2472, (int)Buttons.Button2, GumpButtonType.Reply, 0);
        }

        public enum Buttons
		{
			Button0,
			Button1,
			Button2,
		}


        public override void OnResponse(NetState sender, RelayInfo info)
        {
            Mobile from = sender.Mobile;

            int plainAmt=0, oakAmt=0;

            switch(info.ButtonID)
            {
                case (int)Buttons.Button1:
				{
					plainAmt = from.Backpack.GetAmount(typeof(Board));
					oakAmt = from.Backpack.GetAmount(typeof(OakBoard));
					if(plainAmt<=0 && oakAmt<=0) {
						from.SendMessage(135, "Return when you have plain or oak wood in your pack");
						break;
					}
					else {
					
						if(m_plainWoodCnt >= m_plainWoodMax && m_oakWoodCnt >= m_oakWoodMax) {
							from.SendMessage(152, "This bridge has complete wood supplies");
							break;
						}
					
						if((m_plainWoodCnt + plainAmt) > m_plainWoodMax && m_plainWoodCnt < m_plainWoodMax)
							plainAmt = m_plainWoodMax - m_plainWoodCnt;
						else if (m_plainWoodCnt >= m_plainWoodMax) {
							from.SendMessage(1162, "This bridge has already reached its requirement for regular wood");
							plainAmt = 0;
						}
					
						if((m_oakWoodCnt + oakAmt) > m_oakWoodMax && m_oakWoodCnt < m_oakWoodMax)
							oakAmt = m_oakWoodMax - m_oakWoodCnt;
						else if (m_oakWoodCnt >= m_oakWoodMax) {
							from.SendMessage(1162, "This bridge has already reached its requirement for oak wood");
							oakAmt = 0;
						}
					
						from.Backpack.ConsumeTotal(typeof(Board),plainAmt );
						from.Backpack.ConsumeTotal(typeof(OakBoard),oakAmt );
						
						m_plainWoodCnt += plainAmt;
						m_oakWoodCnt += oakAmt;
						from.SendMessage(200, "You donate " + (plainAmt <= 0 ? "0" : String.Format("{0}",plainAmt)) + " regular wood and " + (oakAmt <= 0 ? "0" : String.Format("{0}",oakAmt)) + " oak wood");
						from.CloseGump( typeof( BridgeWoodGump ) );
						
						m_stone.plainWoodCnt += plainAmt;
						m_stone.oakWoodCnt += oakAmt;
						
						bool gainedPath = false;
						
						int compassionReward = 0;
						
						compassionReward = (plainAmt * 2) + (oakAmt * 5);
						
						if(compassionReward != 0) {
							if ( VirtueHelper.Award( from, VirtueName.Compassion, compassionReward, ref gainedPath ) )
							{
								if ( gainedPath )
									from.SendLocalizedMessage( 1053005 ); // You have achieved a path in compassion!
								else
									from.SendLocalizedMessage( 1053002 ); // You have gained in compassion.
							}
						}
						break;
					}
				}
				case (int)Buttons.Button2:
				{
					from.SendMessage(135, "You have chosen not to donate any wood towards the bridge construction");
					break;
				}
				
				default:
					break;

            }
        }
    }
}