<?php
/*
 * Software Name: Eorisis Pixels
 * Software URI: https://eorisis.com/shop/software/joomla/pixels
 * Author: Eorisis
 * Author URI: https://eorisis.com
 * Copyright: Copyright (C) 2011-2025 EORISIS. All Rights Reserved.
 * License: GNU General Public License version 2; See https://eorisis.com/gpl
 * Terms of Service: https://eorisis.com/tos
 */
namespace com_eorisis_pixels\framework\lib\core\cms;
\defined('_JEXEC') or die;

use EorisisToolbar, EorisisToolbarHelper, JLoader;
use com_eorisis_pixels\framework\lib\core\app\config as app_config;
use com_eorisis_pixels\framework\lib\core\cms\access;

if (\version_compare(JVERSION, '3.9.2', '>='))
{
	JLoader::registerAlias('EorisisToolbar', '\\Joomla\\CMS\\Toolbar\\Toolbar');
	JLoader::registerAlias('EorisisToolbarHelper', '\\Joomla\\CMS\\Toolbar\\ToolbarHelper');
}
else
{
	JLoader::registerAlias('EorisisToolbar', 'JToolbar');
	JLoader::registerAlias('EorisisToolbarHelper', 'JToolbarHelper');
}

class toolbar
{
	public function __construct() {}

	// --------------------------------------------------

	public static function instance()
	{
		static $obj = null;
		if ($obj === null)
		{
			$obj = EorisisToolbar::getInstance('toolbar');
		}

		return $obj;
	}

	// --------------------------------------------------

	public static function apply(string $view) // Must be "apply"
	{
		EorisisToolbarHelper::apply($view.'.apply');
	}

	// --------------------------------------------------

	public static function save(string $view) // Must be "save"
	{
		EorisisToolbarHelper::save($view.'.save');
	}

	// --------------------------------------------------

	// Joomla >= 4
	public static function save_group(array $buttons, string $button_class)
	{
		EorisisToolbarHelper::saveGroup($buttons, $button_class);
	}

	// --------------------------------------------------

	public static function save2new(string $view) // Must be "save2new"
	{
		EorisisToolbarHelper::save2new($view.'.save2new');
	}

	// --------------------------------------------------

	public static function save2copy(string $view) // Must be "save2copy"
	{
		EorisisToolbarHelper::save2copy($view.'.save2copy');
	}

	// --------------------------------------------------

	public static function add_close_button(string $view)
	{
		EorisisToolbarHelper::cancel($view.'.cancel', 'EO_CLOSE');
	}

	// --------------------------------------------------

	public static function add_options_button(
		string $view,
		string $text = 'JTOOLBAR_OPTIONS',
		int $height = 550,
		int $width = 875,
		string $path = '')
	{
		if (!access::auth('core.admin') and !access::auth('core.options'))
		{
			return;
		}

		$component_element = app_config::COMPONENT_ELEMENT;

		if (\version_compare(JVERSION, 4, '>='))
		{
			self::instance()->preferences($component_element, $text, $path);
			return;
		}

		EorisisToolbarHelper::preferences($component_element, $height, $width, $text, $path);
	}

	// --------------------------------------------------

	public static function set_title(string $title, string $icon = 'generic.png')
	{
		EorisisToolbarHelper::title($title, $icon);
	}

	// --------------------------------------------------

	// Joomla <= 3
	public static function add_button(
		string $icon,
		string $txt_key,
		string $uri,
		int $width = 550, // default
		int $height = 400) // default
	{
		self::instance()->appendButton('Popup', $icon, $txt_key, $uri);
	}
}
