/**********************************************************
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\GraniteBridgeStone.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 GraniteBridgeStone : Item
	{
		private Mobile m_User;
		
		public int plainGraniteCnt = 0, plainGraniteMax = 500;
		public int shadowIronGraniteCnt = 0, shadowIronGraniteMax = 200;
		
		[Constructable]
		public GraniteBridgeStone() : base(3797)
		{
			Name = "Donate Granite";
			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( BridgeGraniteGump ) );
			from.SendGump(new BridgeGraniteGump(this, from));
		}		
		
		
		public GraniteBridgeStone( Serial serial ) : base( serial )
		{
		}
		
		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
			writer.Write( (int) 0 );

			writer.Write((int)plainGraniteCnt );
			writer.Write((int)plainGraniteMax);
			writer.Write((int)shadowIronGraniteCnt);
			writer.Write((int)shadowIronGraniteMax);
		}
		
		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
			int version = reader.ReadInt();	

			plainGraniteCnt = reader.ReadInt();
			plainGraniteMax = reader.ReadInt();
			shadowIronGraniteCnt = reader.ReadInt();
			shadowIronGraniteMax = reader.ReadInt();				
		}
	}
}

namespace Server.Gumps
{
    public class BridgeGraniteGump : 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_plainGraniteCnt, m_plainGraniteMax, m_shadowIronGraniteCnt, m_shadowIronGraniteMax;
		private int plainGraniteHue, shadowIronGraniteHue;
		private GraniteBridgeStone m_stone;
			
        public BridgeGraniteGump(GraniteBridgeStone stone, Mobile from) : base( 0, 0 )
        {
			m_plainGraniteCnt = stone.plainGraniteCnt; 
			m_plainGraniteMax = stone.plainGraniteMax;
			m_shadowIronGraniteCnt = stone.shadowIronGraniteCnt;
			m_shadowIronGraniteMax = stone.shadowIronGraniteMax;
			m_stone=stone;
			
            this.Closable=true;
			this.Disposable=true;
			this.Dragable=true;
			
			if(m_plainGraniteCnt >= m_plainGraniteMax)
				plainGraniteHue = 152;
			else 
				plainGraniteHue = 0;
				
			if(m_shadowIronGraniteCnt >= m_shadowIronGraniteMax)
				shadowIronGraniteHue = 152;
			else 
				shadowIronGraniteHue = 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, 6009);
			AddLabel(81, 76, plainGraniteHue, @"Plain Granite" + " (" + m_plainGraniteCnt + "/" + m_plainGraniteMax + " needed)");
			AddLabel(82, 120, shadowIronGraniteHue, @"Shadow Iron Granite" + " (" + m_shadowIronGraniteCnt + "/" + m_shadowIronGraniteMax + " needed)");
			AddItem(27, 113, 6009, 2405);
			AddLabel(30, 166, 0, @"Donate Granite");
			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, shadowAmt=0;

            switch(info.ButtonID)
            {
                case (int)Buttons.Button1:
				{
					plainAmt = from.Backpack.GetAmount(typeof(Granite));
					shadowAmt = from.Backpack.GetAmount(typeof(ShadowIronGranite));
					if(plainAmt<=0 && shadowAmt<=0) {
						from.SendMessage(135, "Return when you have plain or shadow iron granite in your pack");
						break;
					}
					else {
					
						if(m_plainGraniteCnt >= m_plainGraniteMax && m_shadowIronGraniteCnt >= m_shadowIronGraniteMax) {
							from.SendMessage(152, "This bridge has complete granite supplies");
							break;
						}
					
						if((m_plainGraniteCnt + plainAmt) > m_plainGraniteMax && m_plainGraniteCnt < m_plainGraniteMax)
							plainAmt = m_plainGraniteMax - m_plainGraniteCnt;
						else if (m_plainGraniteCnt >= m_plainGraniteMax) {
							from.SendMessage(1162, "This bridge has already reached its requirement for regular granite");
							plainAmt = 0;
						}
					
						if((m_shadowIronGraniteCnt + shadowAmt) > m_shadowIronGraniteMax && m_shadowIronGraniteCnt < m_shadowIronGraniteMax)
							shadowAmt = m_shadowIronGraniteMax - m_shadowIronGraniteCnt;
						else if (m_shadowIronGraniteCnt >= m_shadowIronGraniteMax) {
							from.SendMessage(1162, "This bridge has already reached its requirement for shadow iron granite");
							shadowAmt = 0;
						}
					
						from.Backpack.ConsumeTotal(typeof(Granite),plainAmt );
						from.Backpack.ConsumeTotal(typeof(ShadowIronGranite),shadowAmt );
						
						m_plainGraniteCnt += plainAmt;
						m_shadowIronGraniteCnt += shadowAmt;
						from.SendMessage(200, "You donate " + (plainAmt <= 0 ? "0" : String.Format("{0}",plainAmt)) + " regular granite and " + (shadowAmt <= 0 ? "0" : String.Format("{0}",shadowAmt)) + " shadow iron granite");
						from.CloseGump( typeof( BridgeGraniteGump ) );
						
						m_stone.plainGraniteCnt += plainAmt;
						m_stone.shadowIronGraniteCnt += shadowAmt;
						
						bool gainedPath = false;
						
						int compassionReward = 0;
						
						compassionReward = (plainAmt * 20) + (shadowAmt * 50);
						
						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 granite towards the bridge construction");
					break;
				}
				
				default:
					break;

            }
        }
    }
}