<?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;
defined('_JEXEC') or die;
use Joomla\CMS\Document\Document;
use YOOtheme\Builder\Templates\TemplateHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;
use YOOtheme\Builder;
use YOOtheme\Config;
use YOOtheme\Event;
use YOOtheme\Str;

class TemplateListener {

	public static function loadTemplate(TemplateHelper $helper, Builder $builder, Config $config, $event) {

		list($view, $tpl) = $event->getArguments();

		$template = Event::emit('builder.template', $view, $tpl);
		if (empty($template['type'])) {
			return;
		}

		if ($config('app.isCustomizer')) {
			$config->set('customizer.view', $template['type']);
		}

		if ($matched = $helper->match($template)) {

			$template += $matched + ['layout' => [], 'params' => []];

			// set template identifier
			if ($config('app.isCustomizer')) {
				$config->set('customizer.template', $template['id']);
			}

			// get template from request?
			if ($templ = $config('req.customizer.template') and $templ['id'] == $template['id']) {
				$template['layout'] = $templ['layout'];
			}

			// get output from builder
			$output = $builder->render(json_encode($template['layout']), $template['params'] + [
					'prefix' => "template-{$template['id']}",
				]);

			// append frontend edit button?
			if ($output && isset($template['editUrl']) && !$config('app.isCustomizer')) {
				$output .= "<a style=\"position: fixed!important\" class=\"uk-position-medium uk-position-bottom-right uk-button uk-button-primary\" href=\"{$template['editUrl']}\">" . Text::_('JACTION_EDIT') . '</a>';
			}

			if ($output) {
				$view->set('_output', $output);
				$config->set('app.isBuilder', true);
			}
		}
	}

	public static function afterDispatch(Config $config, Document $document)
	{
		// Check for builder comment, when cache is enabled
		$config->set(
			'app.isBuilder',
			$config('app.isBuilder')
			|| $config('joomla.config')->get('caching', 0)
			&& Str::contains($document->getBuffer('component'), '<!-- Builder #template-')
		);
	}
	public static function matchTemplate(Document $document, $view, $tpl)
	{
		$layout = $view->getLayout();
		$context = $view->get('context');
		$params = $view->get('params');
		//$pagination = $view->get('pagination');

		$app = \JFactory::getApplication();

		// Match context and layout from view object

		if ($context === 'com_djcatalog2.item' && $layout === 'default' && !$tpl) {
			$pagination = null;
			$item = $view->get('item');
			return  [
				'type' => $context,
				'query' => ['cat_id' => $item->cat_id, 'id','lang' => $document->language],
				'params' => ['item' => $item,'pagination' => $pagination],
			];

		}

		if ($context === 'com_djcatalog2.category' && $layout === 'default' && !$tpl) {
			$pagination = $view->get('pagination');
			$item = $view->get('item');
			return  [
				'type' => $context,
				'query' => ['id' => $item->id],
				'params' => ['item' => $item,'pagination' => $pagination],
			];

		}
		if ($context === 'com_djcatalog2.items' && $layout === 'default') {
			$pagination = $view->get('pagination');
			$items = $view->get('items');
			$cid = $app->input->get('cid', '', 'array');
			if(!empty($cid)){
				$cidString = $cid[0];
				$cid = explode(":",$cidString);
				$cid = $cid[0];
			}
			if(empty($cid)) $cid = 0;
			if($params['list_layout'] == 'items'){
				if(!is_null($pagination->pagesCurrent)){
					return [
						'type' => $context.'.list',
						'query' => [
							'pages' => $pagination->pagesCurrent === 1 ? 'first' : 'except_first',
							'cat_id' => $cid,
							'only_cat' => $maincat ?? false,
							'lang' => $document->language
						],
						'params' => ['items' => $items, 'pagination' => $pagination],
					];
				} else{
					return [
						'type' => $context.'.list',
						'query' => [
							'cat_id' => $cid,
							'only_cat' => $maincat ?? false,
							'lang' => $document->language
						],
						'params' => ['items' => $items, 'pagination' => $pagination],
					];
				}
			}
			else {
				if(!is_null($pagination->pagesCurrent)){
					return [
						'type' => $context.'.table',
						'query' => [
							'pages' => $pagination->pagesCurrent === 1 ? 'first' : 'except_first',
							'cat_id' => $cid,
							'lang' => $document->language
						],
						'params' => ['items' => $items, 'pagination' => $pagination],
					];
				} else {
					return [
						'type' => $context.'.table',
						'params' => ['items' => $items, 'pagination' => $pagination],
					];
				}
			}
		}

		if ($context === 'com_djcatalog2.producers' && $layout === 'default' && !$tpl) {
			$pagination = $view->get('pagination');

			if(!is_null($pagination->pagesCurrent)){
				return [
					'type' => $context,
					'query' => [
						'params' => ['items' => $view->get('items'), 'pagination' => $pagination],
					],
					'pages' => $pagination->pagesCurrent === 1 ? 'first' : 'except_first',
				];
			}else{
				return [
					'type' => $context,
					'pages' => $pagination->pagesCurrent === 1 ? 'first' : 'except_first',
				];
			}
		}

		if ($context === 'com_djcatalog2.producer' && $layout === 'default' && !$tpl) {
			$pagination = null;
			$item = $view->get('item');
			return [
				'type' => $context,
				'query' => [
					'prod_id' => $item->id,'id'
				],
				'params' => ['item' => $item, 'pagination' => $pagination],
			];
		}

		if ($context === 'com_djcatalog2.producercategory' && $layout === 'default' && !$tpl) {
			$pagination = $view->get('pagination');
			if(!is_null($pagination->pagesCurrent)){
				return [
					'type' => $context,
					'query' => [
						'pages' => $pagination->pagesCurrent === 1 ? 'first' : 'except_first',
					],
					'params' => ['item' => $view->get('item'), 'pagination' => $pagination],
				];
			}else {
				return [
					'type' => $context,
					'params' => ['item' => $view->get('item'), 'pagination' => $pagination],
				];
			}
		}
	}

}
