/**********************************************************
RunUO AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by Red Squirrel on Jan-01-2012 10:08:52pm
Filepath: scripts\Engines\donation\DonationHandler.cs
Lines of code: 52

Description: 

***********************************************************/


using Server;
using Server.DataStorage;
using System;
using System.Collections.Generic;
using Server.Accounting;

/**
How this system works:

Durring SQL snapshots, the donation SQL table is checked for any donation entries that have not been processed.  These entries are added by Paypal IPN when a payment has gone through.

Any new entries are added to the m_PendingDonationEntries dictionary.  Account name / amount.  

Meanwhile, durring buffer saves, the dictionary is checked for any entries, and updates that user's account.  


Note: No threadsafe checks are required between HandlePendingDonationEntry() and HandleDonationEntry() because that is already handled by the
storage system.  These two functions will NEVER run at the same time.

**/

namespace Server.DonationSystem
{
	
	public class DonationHandler
	{
		public static int PointsPerDollar{ get { return 100;} }   //Points to give for each dollar - default is 100
		
		private static bool m_PendingDonations;
		public static bool PendingDonations { get { return m_PendingDonations; }}
		
		private static List<DonationEntry> m_PendingDonationEntries = new List<DonationEntry>();	//to process					
		
		
		
		//this executed by snapshot: Gets data from SQL and adds to array for processing  (cannot do it here, to avoid multithread issues)
		public static void HandlePendingDonationEntry()
		{
			if(SqlStorageSystem.LostCon)return;
			
			
			//update SQL DB with entries that have been processed:
			


			foreach(DonationEntry entry in m_PendingDonationEntries)
			{
				SqlStorageSystem.Connector.Query(String.Format("UPDATE donate set d_processed=1, d_status='{0}' WHERE d_id={1}",(entry.ProcessError() ? "Processed Err" : "Processed"),entry.GetID()));
			}
			
			
			m_PendingDonationEntries.Clear();
			
			
			
			
			//Check for any new entries:
			
			
			System.Debugger.Write("donationsystem",false,"Checking for pending donation entries...");	
			
			
			bool res = SqlStorageSystem.Connector.Query("SELECT d_id,d_uoaccount,d_amount FROM donate WHERE d_canbeprocessed=1 AND d_processed=0;");
			
			if(!res) return;		
			
			while(SqlStorageSystem.Connector.Reader!=null && SqlStorageSystem.Connector.Reader.Read())
			{
				m_PendingDonationEntries.Add(new DonationEntry(SqlStorageSystem.Connector.Reader.GetInt32(0),SqlStorageSystem.Connector.Reader.GetString(1),SqlStorageSystem.Connector.Reader.GetInt32(2)));
			}
			
			
			
			System.Debugger.Write("donationsystem",false,"{0} donation entries pending.",m_PendingDonationEntries.Count);	
			
			if(m_PendingDonationEntries.Count>0) m_PendingDonations=true;
			else m_PendingDonations=false;
		}
		

		
		//executed by buffersave.  This is real time, so it has to be fast. Only happens once every minute at most.
		public static void HandleDonationEntry()
		{
			if(m_PendingDonationEntries.Count==0)return;			
			
			System.Debugger.Write("donationsystem",false,"Handling donation entries...");
			
			
			foreach(DonationEntry entry in m_PendingDonationEntries)
			{
				entry.Process();
			}
		
		
		
		}


		public static int GetPoints(Mobile from)
		{
			if(from.Account==null)return 0;
			

			if(from.Account is Account)
			{
				return ((Account)from.Account).DonationPoints;
			}
			return 0;
		}


	}
}