<?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\Category;
defined('_JEXEC') or die;
use function YOOtheme\app;
use Joomla\CMS\Component\ComponentHelper;
use YOOtheme\Http\Exception;
use Joomla\CMS\Factory;
use YOOtheme\Path;
use YOOtheme\View;

class SingleCategoryProductType
{
	/**
	 * @return array
	 */

	public static function config()
	{

		return [
			'fields' => [

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

				'alias' => [
					'type' => 'String',
					'metadata' => [
						'label' => 'Alias',
						'filters' => ['limit'],
					],
					'extensions' => [
						'call' => __CLASS__ . '::alias',
					],
				],

				'image' => [
					'type' => 'String',
					'metadata' => [
						'label' => 'Image',
						'filters' => ['limit'],
					],
					'extensions' => [
						'call' => __CLASS__ . '::image',
					],
				],

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

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

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

				'link_products' => [
					'type' => 'String',
					'metadata' => [
						'label' => 'Link to Products',
						'filters' => ['limit'],
					],
					'extensions' => [
						'call' => __CLASS__ . '::link_products',
					],
				],

				'description' => [
					'type' => 'String',
					'metadata' => [
						'label' => 'Description',
						'filters' => ['limit'],
					],
					'extensions' => [
						'call' => __CLASS__ . '::description',
					],
				],
				'id' => [
					'type' => 'Int',
					'metadata' => [
						'label' => 'Id',
					],
				],
				'published' => [
					'type' => 'String',
					'metadata' => [
						'label' => 'Published',
						'filters' => ['date'],
					],
				],

				'created' => [
					'type' => 'String',
					'metadata' => [
						'label' => 'Created',
						'filters' => ['date'],
					],
				],

				'modified' => [
					'type' => 'String',
					'metadata' => [
						'label' => 'Modified',
						'filters' => ['date'],
					],
				],

				'author' => [
					'type' => 'User',
					'metadata' => [
						'label' => 'Author',
					],
					'extensions' => [
						'call' => __CLASS__ . '::author',
					],
				],
			],

			'metadata' => [
				'type' => true,
				'label' => 'SingleCategoryProduct',
			],

		];
	}


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

	public static function image_caption($category)
	{
		return $category->image_caption ?? '';
	}

	public static function image_name($category)
	{
		return $category->img_names[0] ?? '';
	}

	public static function link_products($category)
	{
		return \DJCatalogHelperRoute::getCategoryRoute($category->id) ?? '';
	}

	public static function description($category){
		return $category->description ?? '';
	}

	public static function author($category)
	{
		$user = Factory::getUser($category->created_by);

		if ($user) {
			$user = clone $user;
		}else return '';

		return $user;
	}

	public static function name($category){
		return $category->name ?? \JText::sprintf('PLG_SYSTEM_DJC2YOOTHEME_SINGLE_CATEGORY_HEADLINE_VALUE');
	}

	public static function alias($category){
		return $category->alias ?? '';
	}

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

	public static function metaString($category, array $args)
	{
		$args += [
			'format' => 'list',
			'separator' => '|',
			'link_style' => '',
			'metadesc' => true,
			'metakey' => true,
			'metatitle' => true,
			'date_format' => '',
		];

		return app(View::class)->render(Path::get('../../templates/meta'), compact('category', 'args'));
	}
}


