<?php
/**
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @copyright   Copyright (C) 2016-2024 CMExension
 * @license     GNU General Public License version 2 or later
 */

defined('_JEXEC') or die;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Component\Router\RouterBase;

/**
 * Routing class from com_cmgiftcard.
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       1.0.0
 */
class CMGiftCardRouter extends RouterBase
{
	/**
	 * Build the route for the com_cmgiftcard component
	 *
	 * @param   array  &$query  An array of URL arguments
	 *
	 * @return  array  The URL arguments to use to assemble the subsequent URL.
	 *
	 * @since   1.0.0
	 */
	public function build(&$query)
	{
		$segments = array();

		if (empty($query['Itemid']))
		{
			$menuItem = $this->menu->getActive();
		}
		else
		{
			$menuItem = $this->menu->getItem($query['Itemid']);
		}

		if (!$menuItem)
		{
			$menuItem = $this->menu->getDefault();
		}

		$view = '';

		if (isset($query['view']))
		{
			$view = $query['view'];

			if ($view == 'order' && isset($query['order']))
			{
				$segments[] = $query['order'];
				unset($query['order']);
				unset($query['view']);
			}
			elseif ($view == 'card' && isset($query['code']))
			{
				$segments[] = $query['code'];
				unset($query['code']);
				unset($query['view']);
			}
			elseif ($view == 'form' && $menuItem->query['view'] == 'default')
			{
				unset($query['view']);
			}
			elseif ($view == 'checkout' && $menuItem->query['view'] == 'cart')
			{
				$segments[] = $query['view'];
				unset($query['view']);
			}
			elseif (isset($menuItem->query['view']) && $menuItem->query['view'] == $query['view'])
			{
				unset($query['view']);
			}

			if (isset($query['type']) && $menuItem->query['view'] == 'form' && isset($menuItem->query['type']) && isset($query['type']))
			{
				unset($query['type']);
			}
			/*
			elseif ($menuItem->query['view'] == 'default' || !isset($menuItem->query['type']) && isset($query['type']))
			{
				if ($query['type'] == 'physical' || $query['type'] == 'electronic')
				{
					$segments[] = $query['type'];
				}
				else
				{
					$segments[] = 'electronic';
				}

				unset($query['type']);
			}
			*/
		}

		if ($view == 'cart' && isset($query['item_id']))
		{
			$segments[] = $query['item_id'];

			unset($query['item_id']);
		}

		return $segments;
	}

	/**
	 * Parse the segments of a URL.
	 *
	 * @param   array  &$segments  The segments of the URL to parse.
	 *
	 * @return  array  The URL attributes to be used by the application.
	 *
	 * @since   1.0.0
	 */
	public function parse(&$segments)
	{
		$total = count($segments);
		$vars = array();
		$menuItem = $this->menu->getActive();

		if (isset($segments[0]) && $segments[0] == 'checkout')
		{
			$vars['view'] = 'checkout';
		}
		elseif (isset($segments[0]) && is_numeric($segments[0]))
		{
			switch ($menuItem->query['view'])
			{
				case 'orders':
				case 'order':
					$vars['view'] = 'order';
					$vars['order'] = $segments[0];
					break;

				default:
					$vars['view'] = 'cart';
					$vars['layout'] = 'edit';
					$vars['item_id'] = $segments[0];
			}
		}
		elseif (isset ($segments[0]) && ($menuItem->query['view'] == 'card' || $menuItem->query['view'] == 'cards'))
		{
			$vars['view'] = 'card';
			$vars['code'] = $segments[0];
		}
		elseif (isset($segments[0]))
		{
			$params = ComponentHelper::getParams('com_cmgiftcard');
			$giftCardType = $params->get('gift_card_type', 'all');

			if ($menuItem->query['view'] == 'card')
			{
				$vars['view'] = 'card';
				$vars['code'] = $segments[0];
			}
			else
			{
				if ($giftCardType == 'all' && ($segments[0] == 'physical' || $segments[0] == 'electronic'))
				{
					$vars['type'] = $segments[0];
				}
				elseif ($giftCardType == 'physical')
				{
					$vars['type'] = 'physical';
				}
				else
				{
					$vars['type'] = 'electronic';
				}

				$vars['view'] = 'form';
			}
		}

		if (isset($segments[0]))
			unset($segments[0]);

		return $vars;
	}
}

/**
 * CMGiftCard router functions.
 *
 * These functions are proxys for the new router interface
 * for old SEF extensions.
 *
 * @param   array  &$query  An array of URL arguments
 *
 * @return  array  The URL arguments to use to assemble the subsequent URL.
 *
 * @deprecated  4.0  Use Class based routers instead
 */
function CMGiftCardBuildRoute(&$query)
{
	$router = new CMGiftCardRouter;

	return $router->build($query);
}

/**
 * CMGiftCard router functions
 *
 * These functions are proxys for the new router interface
 * for old SEF extensions.
 *
 * @param   array  $segments  The segments of the URL to parse.
 *
 * @return  array  The URL attributes to be used by the application.
 *
 * @deprecated  4.0  Use Class based routers instead
 */
function CMGiftCardParseRoute($segments)
{
	$router = new CMGiftCardRouter;

	return $router->parse($segments);
}
