<?php
/**
 * @package DJ-Catalog2-Yootheme
 * @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
 * @license http://www.gnu.org/licenses GNU/GPL
 * @author url: http://dj-extensions.com
 * @author email contact@dj-extensions.com
 */

namespace DJExtensions\DJCatalog2\YOOtheme\Builder\Source\Type\Producer;
defined('_JEXEC') or die;
use YOOtheme\Builder\Source;


class ProducerImagesQueryType
{
	/**
	 * @return array
	 */
	public static function config()
	{
		return [

			'fields' => [
				'producergallery' => [
					'type' => [
						'listOf' => 'ProducerImages',
					],
					'args' => [
						'offset' => [
							'type' => 'Int',
						],
						'limit' => [
							'type' => 'Int',
						],
					],
					'extensions' => [
						'call' => __CLASS__ . '::resolve',
					],
					'metadata' => [
						'label' => 'Producer Gallery',
						'group' => 'Page',
						'fields' => [
							'_offset' => [
								'description' => 'Set the starting point and limit the number of images.',
								'type' => 'grid',
								'width' => '1-2',
								'fields' => [
									'offset' => [
										'label' => 'Start',
										'type' => 'number',
										'default' => 0,
										'modifier' => 1,
										'attrs' => [
											'min' => 1,
											'required' => true,
										],
									],
									'limit' => [
										'label' => 'Quantity',
										'type' => 'limit',
										'attrs' => [
											'placeholder' => 'No limit',
											'min' => 0,
										],
									],
								],
							],
						],
					],
				],
			],
		];
	}

	public static function resolve($root, array $args)
	{
		$args += [
			'offset' => 0,
			'limit' => null,
		];

		if (isset($root['item'])) {
			$producer = $root['item'];
			$gallery = array(array());

			$images = \DJCatalog2ImageHelper::getImages('producer', $producer->id);
			$url =  DJCATIMGPATH;

			$count = 0;
			foreach ($images as $image) {
				$urlX = $image->fullpath;
				$gallery[$count]['image'] =  $urlX;
				$gallery[$count]['caption'] = $image->caption;
				$count++;
			}

			if ($args['offset'] || $args['limit']) {
				$gallery = array_slice($gallery, (int)$args['offset'], (int)$args['limit'] ?: null);
			}
			return $gallery;
		}
	}
}
