<?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 DJExtensions\DJCatalog2\YOOtheme\Builder\Source\ProducerHelper;

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

			'fields' => [

				'customProducer' => [
					'type' => 'Producer',
					'args' => [
						'id' => [
							'type' => 'String',
						],
						'cat_id' => [
							'type' => [
								'listOf' => 'String',
							],
						],
						'offset' => [
							'type' => 'Int',
						],
						'order' => [
							'type' => 'String',
						],
						'order_direction' => [
							'type' => 'String',
						],
						'order_alphanum' => [
							'type' => 'Boolean',
						],
					],

					'metadata' => [
						'label' => 'Custom Producer',
						'group' => 'DJ-Catalog2 - Custom',
						'fields' => [
							'id' => [
								'label' => 'Select a Producer',
								'description' => 'Pick a producer manually or use filter options to specify which producer should be loaded dynamically.',
								'type' => 'select',
								'options' => [
									'Pick an existing producer' => 'djc2_yootheme_modal_producers',
								],
								'attrs' => [
									'class' => 'djc2_yootheme_modal_producers',
									'onclick' => "pickProducer(this)",
									'size' => '2',

								],
								'labels' => ['type' => 'Producers'],
							],
							'cat_id' => [
								'label' => 'Limit by Categories',
								'description' => 'The producer is only loaded from the selected categories. Producers from child categories are not included. Use the <kbd>shift</kbd> or <kbd>ctrl/cmd</kbd> key to select multiple categories.',
								'type' => 'select-category',
								'default' => [],
								'attrs' => [
									'multiple' => true,
									'class' => 'uk-height-small uk-resize-vertical',
								],
								'enable' => '!id',
							],
							'offset' => [
								'label' => 'Offset',
								'description' => 'Set the offset to specify which article is loaded.',
								'type' => 'number',
								'default' => 0,
								'modifier' => 1,
								'attrs' => [
									'min' => 1,
									'required' => true,
								],
								'enable' => '!id',
							],
							'_order' => [
								'type' => 'grid',
								'width' => '1-2',
								'fields' => [
									'order' => [
										'label' => 'Order',
										'type' => 'select',
										'default' => 'publish_up',
										'options' => [
											'Published' => 'publish_up',
											'Created' => 'created',
											'Modified' => 'modified',
											'Alphabetical' => 'title',
											'Hits' => 'hits',
											'Producer Order' => 'ordering',
											'Random' => 'rand',
										],
										'enable' => '!id',
									],
									'order_direction' => [
										'label' => 'Direction',
										'type' => 'select',
										'default' => 'DESC',
										'options' => [
											'Ascending' => 'ASC',
											'Descending' => 'DESC',
										],
										'enable' => '!id',
									],
								],
							],
							'order_alphanum' => [
								'text' => 'Alphanumeric Ordering',
								'type' => 'checkbox',
								'enable' => '!id',
							],
						],
					],

					'extensions' => [
						'call' => __CLASS__ . '::resolve',
					],

				],

			],

		];
	}

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

		if (!empty($args['id'])) {
			$producers = ProducerHelper::get($args['id']);
		} else {
			$producers = ProducerHelper::query($args);
		}

		return array_shift($producers);
	}
}
