<?php
/**
 * @brief		Background Task
 * @author		<a href='https://www.invisioncommunity.com'>Invision Power Services, Inc.</a>
 * @copyright	(c) Invision Power Services, Inc.
 * @license		https://www.invisioncommunity.com/legal/standards/
 * @package		Invision Community
 * @subpackage	Downloads
 * @since		08 Jul 2024
 */

namespace IPS\downloads\extensions\core\Queue;

use IPS\Db;
use IPS\Extensions\QueueAbstract;
use IPS\Member;
use IPS\Task\Queue\OutOfRangeException as QueueOutOfRangeException;
use OutOfRangeException;
use function defined;
use const IPS\REBUILD_NORMAL;

/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
	header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
	exit;
}

/**
 * Background Task
 */
class StoreMemberViews extends QueueAbstract
{
	/**
	 * Parse data before queuing
	 *
	 * @param	array	$data
	 * @return	array|null
	 */
	public function preQueueData( array $data ): ?array
	{
		if( !Db::i()->checkForTable( 'downloads_view_method'))
		{
			return null;
		}
		$data['count'] = (int) Db::i()->select( 'count(*)', 'downloads_view_method' )->first();
		return $data;
	}

	/**
	 * Run Background Task
	 *
	 * @param	array						$data	Data as it was passed to \IPS\Task::queue()
	 * @param	int							$offset	Offset
	 * @return	int							New offset
	 * @throws	QueueOutOfRangeException	        Indicates offset doesn't exist and thus task is complete
	 */
	public function run( array &$data, int $offset ): int
	{
		if( !Db::i()->checkForTable( 'downloads_view_method'))
		{
			throw new QueueOutOfRangeException;
		}
		
		$rows = iterator_to_array(
			Db::i()->select( '*', 'downloads_view_method', null, 'member_id', [ $offset, REBUILD_NORMAL] )
		);

		if( !count( $rows ) )
		{
			throw new QueueOutOfRangeException;
		}

		foreach( $rows as $row )
		{
			Member::load( $row['member_id'] )->setLayoutValue( 'downloads_categories', $row['method'] );
		}

		$offset += REBUILD_NORMAL;

		if( $offset >= $data['count'] )
		{
			throw new QueueOutOfRangeException;
		}

		return $offset;
	}
	
	/**
	 * Get Progress
	 *
	 * @param	array					$data	Data as it was passed to \IPS\Task::queue()
	 * @param	int						$offset	Offset
	 * @return	array( 'text' => 'Doing something...', 'complete' => 50 )	Text explaining task and percentage complete
	 * @throws	OutOfRangeException	Indicates offset doesn't exist and thus task is complete
	 */
	public function getProgress( array $data, int $offset ): array
	{
		return array(
			'text' => Member::loggedIn()->language()->addToStack( 'downloads_updating_views' ),
			'complete' => ( $offset ? round( 100 / $data['count'] * $offset, 2 ) : 0 )
		);
	}

	/**
	 * Perform post-completion processing
	 *
	 * @param	array	$data		Data returned from preQueueData
	 * @param	bool	$processed	Was anything processed or not? If preQueueData returns NULL, this will be FALSE.
	 * @return	void
	 */
	public function postComplete( array $data, bool $processed = TRUE ) : void
	{
		Db::i()->dropTable( 'downloads_view_method', true );
	}
}