<?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\Categories\CategoryNode;
use YOOtheme\Path;
use YOOtheme\View;

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

			'fields' => [

				'name' => [
					'type' => 'String',
					'metadata' => [
						'label' => 'Name',
						'filters' => ['limit'],
					],
					'extensions' => [
						'call' => __CLASS__ . '::category_name',
					],
				],

				'description' => [
					'type' => 'String',
					'metadata' => [
						'label' => 'Description',
						'filters' => ['limit'],
					],
				],

				'producer_category_image' => [
					'type' => 'String',
					'extensions' => [
						'call' => __CLASS__ . '::producer_category_image',
					],
					'metadata' => [
						'label' => 'Category Image',
					],
				],

				'link' => [
					'type' => 'String',
					'metadata' => [
						'label' => 'Link to Category',
					],
					'extensions' => [
						'call' => __CLASS__ . '::link',
					],
				],

				'parent' => [
					'type' => 'ProducerCategory',
					'metadata' => [
						'label' => 'Parent Category',
					],
					'extensions' => [
						'call' => __CLASS__ . '::parent',
					],
				],

			],

			'metadata' => [
				'type' => true,
				'label' => 'Producer Category',
			]

		];
	}

	public static function params($category)
	{
		return is_string($category->params) ? json_decode($category->params) : $category->params;
	}

	public static function link($category)
	{
		if (!class_exists('DJCatalogHelperRoute')) {
			\JLoader::register('DJCatalogHelperRoute', JPATH_SITE . '/components/com_djcatalog2/helpers/route.php');
		}
		return \DJCatalogHelperRoute::getProducerCategoryRoute($category->id);
	}

	/**
	 * @param CategoryNode $category
	 *
	 * @return CategoryNode
	 */
	public static function parent($category)
	{
		return $category->getParent();
	}

	/**
	 * @param CategoryNode $category
	 *
	 * @return CategoryNode[]
	 */
	public static function category($category)
	{
		return $category;
	}

	public static function category_name($category)
	{
		return $category->name;
	}

	public static function producer_category_image($category)
	{
		if (!empty($category->image_fullpath)) {
			return  DJCATIMGPATH.'/'.$category->image_fullpath;
		} else {
			return;
		}
	}
}
