<?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 function YOOtheme\app;
use Joomla\CMS\Factory;
use YOOtheme\Builder\Source;
use YOOtheme\Path;
use YOOtheme\View;

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

			'fields' => [
				'producer_image' => [
					'type' => 'String',
					'extensions' => [
						'call' => __CLASS__ . '::producer_image',
					],
					'metadata' => [
						'label' => 'Producer Image',
					],
				],
				'image_caption' => [
					'type' => 'String',
					'extensions' => [
						'call' => __CLASS__ . '::image_caption',
					],
					'metadata' => [
						'label' => 'Full Producer Image Caption',
						'filters' => ['limit'],
					],
				],

			],
			'metadata' => [
				'type' => true,
				'label' => 'Producer Image Gallery',
			],

		];
	}
	public static function producer_image($producer)
	{
		if (!empty($producer->images)) {
			$url = $producer->images[0];

			return $url;
		}  else {
			//This function can be called from other classes, that provide argument as an object.
			if(is_object($producer)) $images = \DJCatalog2ImageHelper::getImages('producer', $producer->id);
			if(empty($images)){
				return $producer['image'] ?? '&nbsp';
			} else {
				$gallery = array();
				$url =  DJCATIMGPATH;
				foreach ($images as $image) {
					$urlX = $image->fullpath;
					$gallery[] =  $url.'/'.$urlX;
				}
				$producer->images = $gallery;
				$url = $producer->images[0];
				return $url;
			}
		}
	}
	public static function image_caption($producer){
		return $producer['caption'];
	}
}
