<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

class Builder_Mosaic_Nextgen extends Builder_Mosaic_Provider {
	
	protected static function init():void{
		add_filter( 'tb_select_dataset_nextgen_galleries', array( __CLASS__, 'tb_select_dataset_nextgen_galleries' ) );
	}

	public static function is_available():bool {
		return class_exists( 'C_NextGEN_Bootstrap',false );
	}

	/**
	 * Gets a list of galleries from NGG
	 *
	 * @return array
	 */
	public static function tb_select_dataset_nextgen_galleries() {
		if ( self::is_available()) {
			$galleries = C_Component_Registry::get_instance()->get_utility('I_Gallery_Mapper')->find_all();
			if ( ! empty( $galleries ) ) {
				return wp_list_pluck( $galleries, 'title', 'gid' );
			}
		}
	}

	public static function get_items(array &$settings,int $limit,int $paged ):array {
		global $nggdb;

		if ( empty( $settings['nextgen_gallery'] ) ) {
			return ['error'=>__( 'No Gallery has been specified.', 'builder-mosaic' )];
		}

		$items = array();
		$gallery = $nggdb->get_gallery( $settings['nextgen_gallery'], 'sortorder', 'DESC', true, $limit, ( $paged - 1 ) * $limit );
		if ( ! empty( $gallery ) ) {
			foreach( $gallery as $image ) {
				$items[] = array(
					'title' => $image->alttext,
					'image' => $image->imageURL,
					'text' => '<p>' . $image->description . '</p>',
					'link' => $image->imageURL,
					'css_classes' => '',
				);
			}
		}

		return array(
			'items' => $items,
			'total_items' => count( $nggdb->get_gallery( $settings['nextgen_gallery'] ) ),
		);
	}
}