<?php
/**
 * @package Helix_Ultimate_Framework
 * @author JoomShaper <support@joomshaper.com>
 * Copyright (c) 2010 - 2025 JoomShaper
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or Later
 */

defined('_JEXEC') or die();

use HelixUltimate\Framework\Platform\Helper;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Uri\Uri;

/**
 * Helix Ultimate Site Logo.
 *
 * @since	1.0.0
 */
class HelixUltimateFeatureLogo
{
	/**
	 * Template parameters
	 *
	 * @var		object	$params		The parameters object
	 * @since	1.0.0
	 */
	private $params;

	/**
	 * Position to load the feature
	 *
	 * @var		string	$position	The position name
	 * @since	1.0.0
	 */
	public $position;

	/**
	 * Load position
	 *
	 * @var		string $load_pos	The load position
	 * @since	1.0.0
	 */
	public $load_pos;

	/**
	 * Constructor function
	 *
	 * @param	object	$params		The template parameters
	 *
	 * @since	1.0.0
	 */
	public function __construct($params)
	{
		$this->params = $params;
		$this->position = 'logo';
		$this->load_pos = $this->params->get('logo_load_pos', 'default');
	}

	/**
	 * Render the logo features.
	 * 
	 * @param boolean $isToggle 	Will show toggler if true
	 *
	 * @return	string
	 * @since	1.0.0
	 */
	public function renderFeature($isToggle = true)
	{

		$template_name = HelixUltimate\Framework\Platform\Helper::loadTemplateData()->template;

		$menu_type = $this->params->get('menu_type');
		$offcanvas_position = $this->params->get('offcanvas_position', 'right');

		$doc = Factory::getDocument();

		$presetVars = (array) json_decode(Helper::CheckNull($this->params->get('preset')));
		$preset = (isset($presetVars['preset']) && $presetVars['preset']) ? $presetVars['preset'] : 'default';

		$html = '';

		$predefinedHeader = $this->params->get('predefined_header', 'none');

		if ($isToggle && $offcanvas_position === 'left' && $predefinedHeader === 'none')
		{
			if ($menu_type === 'mega')
			{
				$html .= '<a id="offcanvas-toggler" aria-label="' . Text::_('HELIX_ULTIMATE_NAVIGATION') . '" class="offcanvas-toggler-left offcanvas-toggler-custom d-lg-none" href="#" title="' . Text::_('HELIX_ULTIMATE_NAVIGATION') . '"><div class="burger-icon" aria-hidden="true"><span></span><span></span><span></span></div></a>';
			}
			else
			{
				$html .= '<a id="offcanvas-toggler" aria-label="' . Text::_('HELIX_ULTIMATE_NAVIGATION') . '" class="offcanvas-toggler-left offcanvas-toggler-custom align-items-center" href="#" title="' . Text::_('HELIX_ULTIMATE_NAVIGATION') . '"><div class="burger-icon" aria-hidden="true"><span></span><span></span><span></span></div></a>';
			}
		}

		$custom_logo_class = '';
		$sitename = Factory::getApplication()->get('sitename');

		if ($this->params->get('mobile_logo'))
		{
			$custom_logo_class = ' d-none d-lg-inline-block';
		}

		if ($this->params->get('logo_type') === 'image')
		{
			$altText = htmlspecialchars($this->params->get('logo_alt', $sitename), ENT_QUOTES, 'UTF-8');

			if ($this->params->get('logo_image'))
			{
				$customLink = $this->params->get('logo_custom_link');
				if ($customLink) {
					$logoUrl = htmlspecialchars($customLink, ENT_QUOTES, 'UTF-8');
				} else {
					$logoUrl = Uri::base(true) . '/';
				}

				$html .= '<div class="logo">';
				$html .= '<a href="' . $logoUrl . '">';

				$defaultLogo = $this->params->get('logo_image', null);
				$retinaLogo	= $this->params->get('retina_logo', null);
				$srcset = '';
				if (file_exists($defaultLogo))
				{
					$srcset .= Uri::root() . $defaultLogo . ' 1x';
				}
				if (file_exists($defaultLogo) && (!is_null($retinaLogo) && file_exists($retinaLogo)))
				{
					$srcset .= ', ';
				}
				if (!is_null($retinaLogo) && file_exists($retinaLogo))
				{
					$srcset .= Uri::root() . $retinaLogo . ' 2x';
				}
				$logoWithUrl = Uri::root() . $defaultLogo;
				$attrLogoHeightRaw = $this->params->get('logo_height', '');
				$attrLogoHeight = (int) filter_var($attrLogoHeightRaw, FILTER_SANITIZE_NUMBER_INT);
			
				// Add sticky logo class if sticky logo exists
				$mainLogoClass = 'logo-image';
				if ($this->params->get('sticky_logo'))
				{
					$mainLogoClass .= ' logo-default';
				}
				
				$siteLogo = "
				<img class='{$mainLogoClass} {$custom_logo_class}'
					srcset='{$srcset}'
					src='{$logoWithUrl}'
					height='{$attrLogoHeight}'
					alt='{$altText}'
				/>
				";

				$html .= $siteLogo;

				// Add sticky logo if set
				if ($this->params->get('sticky_logo'))
				{
					$stickyLogo = $this->params->get('sticky_logo', null);
					$stickyLogoWithUrl = Uri::root() . $stickyLogo;
					$attrStickyLogoHeightRaw = $this->params->get('sticky_logo_height', '');
					$attrStickyLogoHeight = (int) filter_var($attrStickyLogoHeightRaw, FILTER_SANITIZE_NUMBER_INT);
					
					// Use default logo height if sticky logo height is not set
					if (empty($attrStickyLogoHeight))
					{
						$attrStickyLogoHeight = $attrLogoHeight;
					}
					
					$html .= "<img class='logo-image logo-sticky {$custom_logo_class}' src='{$stickyLogoWithUrl}' height='{$attrStickyLogoHeight}' alt='{$altText}' />";
				}

				if ($this->params->get('mobile_logo'))
				{
					$html .= '<img class="logo-image-phone d-inline-block d-lg-none" src="' .
						Uri::root() .$this->params->get('mobile_logo') . '" alt="' . $altText . '" />';
				}

				$html .= '</a>';

				$html .= '</div>';
			}
			else
			{
				$html .= '<div class="logo">';
				$html .= '<a href="' . Uri::base(true) . '/">';

				$html .= '<img class="logo-image' . $custom_logo_class .
					'" src="' . Uri::base() . 'templates/' .
					$template_name . '/images/presets/' . $preset . '/logo.svg" alt="' . $altText . '" />';

				if ($this->params->get('mobile_logo'))
				{
					$html .= '<img class="logo-image-phone d-inline-block d-lg-none" src="' . $this->params->get('mobile_logo') . '" alt="' . $altText . '" />';
				}

				$html .= '</a>';
				$html .= '</div>';
			}

			if ($logo_height = $this->params->get('logo_height', ''))
			{
				$logo_height = preg_match("@(px|em|rem|%)$@", $logo_height) ? $logo_height : $logo_height . 'px';

				$logoStyle = '.logo-image {height:' . $logo_height . ';}';
				$logoStyle .= '.logo-image-phone {height:' . $logo_height . ';}';

				$doc->addStyleDeclaration($logoStyle);
			}

			// Add sticky logo height if set
			if ($sticky_logo_height = $this->params->get('sticky_logo_height', ''))
			{
				$sticky_logo_height = preg_match("@(px|em|rem|%)$@", $sticky_logo_height) ? $sticky_logo_height : $sticky_logo_height . 'px';

				$stickyLogoStyle = '.header-sticky .logo-sticky {height:' . $sticky_logo_height . ';}';

				$doc->addStyleDeclaration($stickyLogoStyle);
			}

			/**
			 * If responsive logo height is provided then add the height
			 * to the media query.
			 */
			if ($logo_height_sm = $this->params->get('logo_height_sm', ''))
			{
				$logo_height_sm = preg_match("@(px|em|rem|%)$@", $logo_height_sm) ? $logo_height_sm : $logo_height_sm . 'px';

				$logoStyleSm = '@media(max-width: 992px) {';
				$logoStyleSm .= '.logo-image {height: ' . $logo_height_sm . ';}';
				$logoStyleSm .= '.logo-image-phone {height: ' . $logo_height_sm . ';}';
				$logoStyleSm .= '}';

				$doc->addStyleDeclaration($logoStyleSm);
			}
			
			if ($logo_height_xs = $this->params->get('logo_height_xs', ''))
			{
				$logo_height_xs = preg_match("@(px|em|rem|%)$@", $logo_height_xs) ? $logo_height_xs : $logo_height_xs . 'px';

				$logoStyleXs = '@media(max-width: 576px) {';
				$logoStyleXs .= '.logo-image {height: ' . $logo_height_xs . ';}';
				$logoStyleXs .= '.logo-image-phone {height: ' . $logo_height_xs . ';}';
				$logoStyleXs .= '}';

				$doc->addStyleDeclaration($logoStyleXs);
			}
			// Add responsive sticky logo height if sticky logo height is set
			if ($this->params->get('sticky_logo') && $this->params->get('sticky_logo_height'))
			{
				if ($logo_height_sm)
				{
					$logoStyleSm = '@media(max-width: 992px) {';
					$logoStyleSm .= '.header-sticky .logo-sticky {height: ' . $logo_height_sm . ';}';
					$logoStyleSm .= '}';

					$doc->addStyleDeclaration($logoStyleSm);
				}
				
				if ($logo_height_xs)
				{
					$logoStyleXs = '@media(max-width: 576px) {';
					$logoStyleXs .= '.header-sticky .logo-sticky {height: ' . $logo_height_xs . ';}';
					$logoStyleXs .= '}';

					$doc->addStyleDeclaration($logoStyleXs);
				}
			}
		}
		else
		{
			if ($this->params->get('logo_text'))
			{
				$html .= '<span class="logo"><a href="' . Uri::base(true) . '/">' . $this->params->get('logo_text') . '</a></span>';
			}
			else
			{
				$html .= '<span class="logo"><a href="' . Uri::base(true) . '/">' . $sitename . '</a></span>';
			}

			if ($this->params->get('logo_slogan'))
			{
				$html .= '<span class="logo-slogan">' . $this->params->get('logo_slogan') . '</span>';
			}
		}

		return $html;
	}
}
