<?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 Exception;
use JLoader, EorisisFactory, EorisisRegistry, EorisisBaseDatabaseModel, EorisisContentHelper;
use Joomla\CMS\Cache\CacheControllerFactoryInterface;
use Joomla\Component\Content\Administrator\Helper\ContentHelper;
use com_eorisis_pixels\framework\lib\core\app\config as app_config;

if (\version_compare(JVERSION, '3.8', '>='))
{
	JLoader::registerAlias('EorisisFactory', '\\Joomla\\CMS\\Factory');
	JLoader::registerAlias('EorisisContentHelper', '\\Joomla\\CMS\\Helper\\ContentHelper');
	JLoader::registerAlias('EorisisBaseDatabaseModel', '\\Joomla\\CMS\\MVC\\Model\\BaseDatabaseModel');
}
else
{
	JLoader::registerAlias('EorisisFactory', 'JFactory');
	JLoader::registerAlias('EorisisContentHelper', 'JHelperContent');
	JLoader::registerAlias('EorisisBaseDatabaseModel', 'JModelLegacy');
}

if (\version_compare(JVERSION, '3.3', '>='))
{
	JLoader::registerAlias('EorisisRegistry', '\\Joomla\\Registry\\Registry');
}
else
{
	JLoader::registerAlias('EorisisRegistry', 'JRegistry');
}

class application
{
	public function __construct() {}

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

	public static function get()
	{
		return EorisisFactory::getApplication();
	}

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

	public static function is_backend() : bool
	{
		static $val = null;
		if ($val === null)
		{
			if (\version_compare(JVERSION, '3.7', '>='))
			{
				$val = (bool)self::get()->isClient('administrator');
			}
			else
			{
				$val = (bool)self::get()->isAdmin(); // < 3.7 Fallback
			}
		}

		return $val;
	}

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

	public static function get_template()
	{
		return self::get()->getTemplate();
	}

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

	public static function get_model(string $type, string $prefix, $config = [])
	{
		return EorisisBaseDatabaseModel::getInstance($type, $prefix, $config);
	}

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

	public static function register_class(string $class, string $path, bool $force = true) // void
	{
		JLoader::register($class, $path, $force);
	}

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

	public static function new_registry($data = null)
	{
		return new EorisisRegistry($data);
	}

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

	public static function cache(string $group = '', string $handler = 'callback', $storage = null)
	{
		// @param   string  $group    The cache group name
		// @param   string  $handler  The handler to use
		// @param   string  $storage  The storage method

		if (\version_compare(JVERSION, '4.3', '>='))
		{
			$options = ['defaultgroup' => $group];

			if ($storage)
			{
				$options['storage'] = $storage;
			}

			$class = CacheControllerFactoryInterface::class;
			return EorisisFactory::getContainer()->get($class)->createCacheController($handler, $options);
		}

		return EorisisFactory::getCache($group, $handler, $storage);
	}

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

	public static function dbo()
	{
		static $dbo = null;
		if ($dbo === null)
		{
			if (\version_compare(JVERSION, 4, '>='))
			{
				$dbo = EorisisFactory::getContainer()->get('DatabaseDriver');
			}
			else
			{
				$dbo = EorisisFactory::getDbo();
			}
		}

		return $dbo;
	}

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

	public static function content_actions(string $section = '', int $id = 0)
	{
		if (\version_compare(JVERSION, 4, '>='))
		{
			return ContentHelper::getActions(app_config::COMPONENT_ELEMENT, $section, $id);
		}

		return EorisisContentHelper::getActions(app_config::COMPONENT_ELEMENT, $section, $id);
	}

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

	public static function set_message(string $txt, string $type = 'notice')
	{
		// message, notice, warning, error
		self::get()->enqueueMessage($txt, $type);
	}

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

	public static function error_check($errors)
	{
		// Check for errors in the Model
		if (empty($errors))
		{
			return;
		}

		throw new Exception(\implode("\n", $errors), 500);
	}
}
