<?php

//only extends Atmail_provider because must also impliment access using same methods to all required providers
//This class aggregates access to all provider streams to enable stream view/searching/posting to all/inteligently selected streams

class Atmail_Providers 
{
	
	/* use this factory class to talk to all available providers and types or single provider by pid */
	/* This class is also the base abstract class for providers */

	protected $_accounts;
	protected $id = null;
	public static $errorCodes = array();
	public static $errorMessages = array();
	public $mimeType = null;
	private $_providers = array();
	    
	public function __construct( $accounts = array() ) 
	{
	
		$this->dbAdapter = Zend_Registry::get('dbAdapter');
		//set up instances of all providers specified in $configArray
		$this->_providers = array();
		foreach( $accounts as $account )
		{
			//some provider accounts do not need to be instantiated here (email will be conventionally handled for now but ultimately should be instantiated for stream)
			if( $account['Type'] == 'email' )
				$this->_providers[$account['id']] = null;
			else
				$this->_providers[$account['id']] = Atmail_Provider_Factory::factory($account);
		}
		$this->_accounts = $accounts;
			
    }

	public function getStreamToRefactorHere( Array $arguments = array() )
	{
		
		throw new Exception(__METHOD__ . ' #' . __LINE__ . ': TODO: implement methods to operate on all providers');
		
	}
	// the key purpose of this object if to enable methods that will require group access/control/management over all enabled service accounts
	// individual methods that do not need global awareness are directly implimented in the provider objects, and calls are passed directly in (requires pid and message id (id))
	
	public function __call( $name, $arguments )
	{
//Zend_Registry::get('log')->debug( __METHOD__ . ' #' . __LINE__ . ": Calling providers method $name with: \n" . print_r($arguments[0], true) );
		
		//for some reason $args nested in array now
		$args = $arguments[0];
		if( array_key_exists('pid', $args) )
			if( method_exists($this->_providers[$args['pid']], $name))
				return $this->_providers[$args['pid']]->$name($args);
			else
				throw new Exception("Error: Provider does not provide method " . $name);
				

		//if type and/or provider name/pid supplied then restrict queries, else try apply to global
		throw new Exception(__METHOD__ . ' #' . __LINE__ . ': TODO: if no single provider specified, query all matching providers');
		
	}

}
