/**********************************************************
RunUO AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by Red Squirrel on Oct-13-2011 08:59:11pm
Filepath: scripts\Accounting\Accounts.cs
Lines of code: 66

Description: 

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


using System;
using Server;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using Server.DataStorage;

namespace Server.Accounting
{
	public class Accounts
	{
		public static Dictionary<string, IAccount> m_Accounts = new Dictionary<string, IAccount>();
		public static Dictionary<int, bool> m_TakenSerials = new Dictionary<int, bool>();
		
		
		public static void Configure()
		{
			EventSink.WorldLoad += new WorldLoadEventHandler( Load );
		}		

		//returns next serial and assigns it as used
		public static int GetNextSerial()
		{
			return SerialDB.GetNewSerial(SqlCont.Account);
		}
		
		public static void ReleaseSerial(int serial)
		{
			SerialDB.ReleaseSerial(SqlCont.Account,serial);			
		}
		
		


		public static int Count { get { return m_Accounts.Count; } }

		public static ICollection<IAccount> GetAccounts()
		{
			return m_Accounts.Values;
		}

		public static IAccount GetAccount( string username )
		{
			IAccount a;
			

			m_Accounts.TryGetValue( username, out a );
			

			return a;
		}

		public static void Add( IAccount a )
		{
			m_Accounts[a.Username] = a;
		}
		
		public static void Remove( string username )
		{
			m_Accounts.Remove( username );
		}

		public static void Load()
		{
			SqlLoad();
		}

		
		public static void SqlLoad()
		{
			m_Accounts = new Dictionary<string, IAccount>( 32, StringComparer.OrdinalIgnoreCase );
			
			SqlConnector connector = SqlStorageSystem.Connector;
			
			connector.LoadContainer((int)SqlCont.Account);	
			
			SqlDataEntry entry;
			
			while(!(entry = connector.FetchNextEntry()).IsBlank())
			{
				//create object and add to world:		

				Account account = new Account(entry.GetSerial());				
				
				
				SqlReader reader = new SqlReader(entry.GetData(),SqlCont.Account,entry.GetSerial());
				account.Deserialize(reader);
				Add(account);
				
				SerialDB.LoadSerialInto(SqlCont.Account,entry.GetSerial());

				SqlStorageSystem.m_totalloadobjects[(int)SqlCont.Account]++;
			}
		
		
		}
				

		
		
		
		
		
		
		
		
	}
}










