/**********************************************************
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\GoldFundStone.cs
Lines of code: 152
***********************************************************/


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 GoldFundBridgeStone : Item
	{
		private Mobile m_User;
		
		public int plainGoldFundCnt = 0, plainGoldFundMax = 1000000;
		
		[Constructable]
		public GoldFundBridgeStone() : base(3797)
		{
			Name = "Donate Gold";
			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( BridgeGoldFundGump ) );
			from.SendGump(new BridgeGoldFundGump(this, from));
		}		
		
		
		public GoldFundBridgeStone( Serial serial ) : base( serial )
		{
		}
		
		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
			writer.Write( (int) 0 );

			writer.Write((int)plainGoldFundCnt );
			writer.Write((int)plainGoldFundMax);
		}
		
		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
			int version = reader.ReadInt();	

			plainGoldFundCnt = reader.ReadInt();
			plainGoldFundMax = reader.ReadInt();			
		}
	}
}

namespace Server.Gumps
{
    public class BridgeGoldFundGump : 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_plainGoldFundCnt, m_plainGoldFundMax;
		private int plainGoldFundHue;
		private GoldFundBridgeStone m_stone;
		
			
        public BridgeGoldFundGump(GoldFundBridgeStone stone, Mobile from) : base( 0, 0 )
        {
			m_plainGoldFundCnt = stone.plainGoldFundCnt; 
			m_plainGoldFundMax = stone.plainGoldFundMax;
			m_stone=stone;
			
            this.Closable=true;
			this.Disposable=true;
			this.Dragable=true;
			
			if(m_plainGoldFundCnt >= m_plainGoldFundMax)
				plainGoldFundHue = 152;
			else 
				plainGoldFundHue = 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, 3823);
			AddLabel(81, 76, plainGoldFundHue, @"Gold Pieces" + " (" + m_plainGoldFundCnt + "/" + m_plainGoldFundMax + " needed)");
			AddLabel(30, 166, 0, @"Donate Gold");
			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;

            switch(info.ButtonID)
            {
                case (int)Buttons.Button1:
				{
					plainAmt = from.Backpack.GetAmount(typeof(Gold));
					if(plainAmt<=0) {
						from.SendMessage(135, "Return when you have gold pieces in your pack");
						break;
					}
					else {
					
						if(m_plainGoldFundCnt >= m_plainGoldFundMax) {
							from.SendMessage(152, "This bridge has complete gold funding");
							break;
						}
					
						if((m_plainGoldFundCnt + plainAmt) > m_plainGoldFundMax && m_plainGoldFundCnt < m_plainGoldFundMax)
							plainAmt = m_plainGoldFundMax - m_plainGoldFundCnt;
						else if (m_plainGoldFundCnt >= m_plainGoldFundMax) {
							from.SendMessage(1162, "This bridge has already reached its requirement for gold funds");
							plainAmt = 0;
						}
					
						from.Backpack.ConsumeTotal(typeof(Gold),plainAmt );
						
						m_plainGoldFundCnt += plainAmt;
						from.SendMessage(200, "You donate " + (plainAmt <= 0 ? "0" : String.Format("{0}",plainAmt)) + " gold pieces");
						from.CloseGump( typeof( BridgeGoldFundGump ) );
						
						m_stone.plainGoldFundCnt += plainAmt;
						
						bool gainedPath = false;
						
						int compassionReward = 0;
						
						if (plainAmt < 20) 
							compassionReward = 0;
						else 
							compassionReward = (int)(plainAmt/20);
							
						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 gold towards the bridge construction");
					break;
				}
				
				default:
					break;

            }
        }
    }
}