//<?php

/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
	exit;
}

class chatbox_hook_Donation extends _HOOK_CLASS_
{
	/**
	 * On Paid
	 *
	 * @param	\IPS\nexus\Invoice	$invoice	The invoice
	 * @return	void
	 */
	public function onPaid( \IPS\nexus\Invoice $invoice )
	{
		try
		{
			parent::onPaid( $invoice );
	
			$authorUrl = \IPS\Theme::i()->getTemplate( 'room', 'chatbox', 'front' )->chatterName(
				\IPS\Member\Group::load( $invoice->member->member_group_id )->formatName( $invoice->member->name ), 
				$invoice->member->member_id, 
				NULL
			);
			
			$data = json_encode( array( $authorUrl, (string) $this->price ) );
			
			foreach( \IPS\chatbox\Rooms\Room::roots() as $room )
			{
				if ( $room->nexus_on == 1 && $room->nexus_donate_goal == $this->id )
				{
					$room->addSystemMsg( 'donation', $data, FALSE );
				}
			}
			
			if ( isset( \IPS\Data\Store::i()->chatbox_rooms_data ) )
			{
				unset( \IPS\Data\Store::i()->chatbox_rooms_data );
			}		
		}
		catch ( \Error | \RuntimeException $e )
		{
			if ( method_exists( get_parent_class(), __FUNCTION__ ) )
			{
				return \call_user_func_array( 'parent::' . __FUNCTION__, \func_get_args() );
			}
			else
			{
				throw $e;
			}
		}
	}

}
