<?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\item;
defined('_JEXEC') or die;
use Joomla\CMS\Component\ComponentHelper;
use DJExtensions\DJCatalog2\YOOtheme\Builder\Source\ItemHelper;

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

			'fields' => [
				'itemExtraFields' => [
					'type' => [
						'listOf' => 'ItemExtraFields',
					],
					'args' => [
						'ids' => [
							'type' => [
								'listOf' => 'Int',
							],
						],
						'offset' => [
							'type' => 'Int',
						],
						'delimeter' => [
							'type' => 'String',
						],
					],
					'extensions' => [
						'call' => __CLASS__ . '::resolve',
					],
					'metadata' => [
						'label' => "Product's Extra Fields",
						'group' => 'DJ-Catalog2',
						'fields' => [
							'_offset' => [
								'description' => 'Set the starting point and limit the number of extra fields.',
								'type' => 'grid',
								'width' => '1',
								'fields' => [
									'ids' => [
										'label' => 'Select Extra Fields',
										'type' => 'select',
										'options' => self::getExtraFieldsOptions(null),
										'default' => '0',
										'attrs' => [
											'multiple' => true,
											'class' => 'uk-select uk-height-small uk-resize-vertical',
										],
									],
								],
							],
							'_delimeter' => [
								'description' => 'Set the delimeter for extra fields.',
								'type' => 'grid',
								'width' => '1',
								'fields' => [
									'delimeter' => [
										'label' => 'Provide a delimeter',
										'type' => 'text',
										'default' => ':',
									],
								],
							],
						],
					],
				],
			],
		];
	}

	public static function resolve($root, array $args)
	{
		if (isset($root['item'])) {
			$item = $root['item'];
			if (!class_exists('Djcatalog2Modelitem')) {
				require_once JPATH_SITE . '/components/com_djcatalog2/models/item.php';
			}
			$model = \JModelLegacy::getInstance('item', 'DJCatalog2Model', ['ignore_request' => true]);
			$item = $model->getItem($item->id);
			$extraFields = ItemHelper::getExtraFields($item);
			$extraFieldsChosen = array();

			$params = ComponentHelper::getParams('com_djcatalog2');
			$attributes = $model->getAttributes();

			foreach ($attributes as $attrd) {
				if (in_array($attrd->id, $args['ids'])) {
					//Save item_id in order to render values for a specific item (layout - attributevalue.php)
					$attrd->itemId = $item->id;
					$extraFieldsChosen[] = $attrd;
				} else continue;
			}
			return $extraFieldsChosen;
		}
		return '';
	}

	static function getExtraFields($item = null)
	{
		//Gain all available extra fields for available item from ItemHelper class
		$extraFields = ItemHelper::getExtraFields($item);
		return $extraFields;
	}

	static function getExtraFieldsOptions()
	{
		$list = ItemHelper::getExtraFieldsOptions();
		return $list;
	}
}
