<?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 ProducerFieldsQueryType
{
	/**
	 * @return array
	 */
	public static function config()
	{
		return [

			'fields' => [
				'producerfields' => [
					'type' => [
						'listOf' => 'ProducerFields',
					],
					'args' => [
						'ids' => [
							'type' => [
								'listOf' => 'String',
							],
						],
						'offset' => [
							'type' => 'Int',
						],
						'delimeter' => [
							'type' => 'String',
						],
						'labelShow' => [
							'type' => 'String',
						],
					],
					'extensions' => [
						'call' => __CLASS__ . '::resolve',
					],
					'metadata' => [
						'label' => 'Producer Extra Fields',
						'group' => 'Page',
						'fields' => [
							'_offset' => [
								'description' => 'Set the starting point and limit the number of images.',
								'type' => 'grid',
								'width' => '1',
								'fields' => [
									'ids' => [
										'label' => 'Select Extra Field',
										'type' => 'select',
										'options' => self::getExtraFieldsOptions(),
										'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',
									],


								],
							],
							'_labelShow' => [
								'description' => 'Choose if you want to show label of a extra field.',
								'type' => 'grid',
								'width' => '1',
								'fields' => [
									'labelShow' => [
										'label' => "Show extra fields' label",
										'type' => 'select',
										'default' => '1',
										'options' => [
											'Yes' => '1',
											'No' => '0',
										],

									],
								],
							],

						],
					],
				],
			],
		];
	}

	public static function resolve($root, array $args)
	{
		$attributes = self::getExtraFields();
		if (isset($root['item'])) {
			$producer = $root['item'];
			$attributesChosen = array();

			foreach ($attributes as $attr) {
				if (in_array($attr->id, $args['ids'])) {
					$alias = '_ef_' . $attr->alias;
					$attr->alias = $alias;
					array_push($attributesChosen,$attr);
				}else continue;
			}
			//assign chosen attributes to producer's attributes
			foreach($attributesChosen as $attrC){
				$x = $attrC->alias;
				$attrC->value = $producer->$x;
				$attrC->delimeter = $args['delimeter'];
				$attrC->showLabel = $args['labelShow'];
			}
			return $attributesChosen;
		}
	}

	static function getExtraFields()
	{
		if (!class_exists('Djcatalog2ModelProducer')) {
			require_once JPATH_SITE . '/components/com_djcatalog2/models/producer.php';
		}
		$model = \JModelLegacy::getInstance('Producer', 'DJCatalog2Model', ['ignore_request' => true]);

		//Gain all available extra fields
		$extraFields = $model->getAttributes();
		return $extraFields;
	}

	static function getExtraFieldsOptions()
	{
		$extraFields = static::getExtraFields();
		$list = array();
		foreach ($extraFields as $field) {
			$fieldName = str_replace(array('&#34;', '&#38;'), array("'", "&"), $field->name);
			$list[$fieldName] = $field->id;
		}
		return $list;
	}
}
