<?php
/**
 * @package    Joomla.Component.Builder
 *
 * @created    30th April, 2015
 * @author     Llewellyn van der Merwe <https://dev.vdm.io>
 * @git        Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
 * @copyright  Copyright (C) 2015 Vast Development Method. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */
namespace VDM\Component\Componentbuilder\Administrator\Model;

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\MVC\Model\ItemModel;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\User\User;
use Joomla\Input\Input;
use Joomla\Utilities\ArrayHelper;
use VDM\Component\Componentbuilder\Administrator\Helper\ComponentbuilderHelper;
use Joomla\CMS\Helper\TagsHelper;
use VDM\Joomla\Utilities\ArrayHelper as UtilitiesArrayHelper;
use VDM\Joomla\Componentbuilder\Package\Factory as PackageFactory;
use VDM\Joomla\Componentbuilder\Fieldtype\Factory as FieldtypeFactory;
use VDM\Joomla\Componentbuilder\JoomlaPower\Factory as JoomlaPowerFactory;
use VDM\Joomla\Componentbuilder\Power\Factory as PowerFactory;
use VDM\Joomla\Componentbuilder\Snippet\Factory as SnippetFactory;
use VDM\Joomla\Componentbuilder\Repository\Factory as RepositoryFactory;

// No direct access to this file
\defined('_JEXEC') or die;

/**
 * Componentbuilder Initialization_selection Item Model
 *
 * @since  1.6
 */
class Initialization_selectionModel extends ItemModel
{
	/**
	 * Model context string.
	 *
	 * @var     string
	 * @since   1.6
	 */
	protected $_context = 'com_componentbuilder.initialization_selection';

	/**
	 * Represents the current user object.
	 *
	 * @var   User  The user object representing the current user.
	 * @since 3.2.0
	 */
	protected User $user;

	/**
	 * The unique identifier of the current user.
	 *
	 * @var   int|null  The ID of the current user.
	 * @since 3.2.0
	 */
	protected ?int $userId;

	/**
	 * Flag indicating whether the current user is a guest.
	 *
	 * @var   int  1 if the user is a guest, 0 otherwise.
	 * @since 3.2.0
	 */
	protected int $guest;

	/**
	 * An array of groups that the current user belongs to.
	 *
	 * @var   array|null  An array of user group IDs.
	 * @since 3.2.0
	 */
	protected ?array $groups;

	/**
	 * An array of view access levels for the current user.
	 *
	 * @var   array|null  An array of access level IDs.
	 * @since 3.2.0
	 */
	protected ?array $levels;

	/**
	 * The application object.
	 *
	 * @var   CMSApplicationInterface  The application instance.
	 * @since 3.2.0
	 */
	protected CMSApplicationInterface $app;

	/**
	 * The input object, providing access to the request data.
	 *
	 * @var   Input  The input object.
	 * @since 3.2.0
	 */
	protected Input $input;

	/**
	 * The styles array.
	 *
	 * @var    array
	 * @since  4.3
	 */
	protected array $styles = [
		'administrator/components/com_componentbuilder/assets/css/admin.css',
		'administrator/components/com_componentbuilder/assets/css/initialization_selection.css'
 	];

	/**
	 * The scripts array.
	 *
	 * @var    array
	 * @since  4.3
	 */
	protected array $scripts = [
		'administrator/components/com_componentbuilder/assets/js/admin.js'
 	];

	/**
	 * A custom property for UI Kit components.
	 *
	 * @var   mixed  Property for storing UI Kit component-related data or objects.
	 * @since 3.2.0
	 */
	protected $uikitComp = [];

	/**
	 * @var     object item
	 * @since   1.6
	 */
	protected $item;

	/**
	 * Constructor
	 *
	 * @param   array                 $config   An array of configuration options (name, state, dbo, table_path, ignore_request).
	 * @param   ?MVCFactoryInterface  $factory  The factory.
	 *
	 * @since   3.0
	 * @throws  \Exception
	 */
	public function __construct($config = [], MVCFactoryInterface $factory = null)
	{
		parent::__construct($config, $factory);

		$this->app ??= Factory::getApplication();
		$this->input ??= $this->app->getInput();

		// Set the current user for authorisation checks (for those calling this model directly)
		$this->user ??= $this->getCurrentUser();
		$this->userId = $this->user->get('id');
		$this->guest = $this->user->get('guest');
		$this->groups = $this->user->get('groups');
		$this->authorisedGroups = $this->user->getAuthorisedGroups();
		$this->levels = $this->user->getAuthorisedViewLevels();

		// will be removed
		$this->initSet = true;
	}

	/**
	 * Method to auto-populate the model state.
	 *
	 * Note. Calling getState in this method will result in recursion.
	 *
	 * @return  void
	 * @since   1.6
	 */
	protected function populateState()
	{
		// Get the item main id
		$id = $this->input->getInt('id', null);
		$this->setState('initialization_selection.id', $id);

		// Load the parameters.
		parent::populateState();
	}

	/**
	 * Method to get article data.
	 *
	 * @param   integer  $pk  The id of the article.
	 *
	 * @return  mixed  Menu item data object on success, false on failure.
	 * @since   1.6
	 */
	public function getItem($pk = null)
	{
		// check if this user has permission to access item
		if (!$this->user->authorise('initialization_selection.access', 'com_componentbuilder'))
		{
			$this->app->enqueueMessage(Text::_('Not authorised!'), 'error');
			// redirect away if not a correct to cPanel/default view
			$this->app->redirect('index.php?option=com_componentbuilder');
			return false;
		}

		$pk = (!empty($pk)) ? $pk : (int) $this->getState('initialization_selection.id');

		if ($this->_item === null)
		{
			$this->_item = [];
		}

		if (!isset($this->_item[$pk]))
		{
			try
			{
				// Get a db connection.
				$db = $this->getDatabase();

				// Create a new query object.
				$query = $db->getQuery(true);

				// Get data
				$data = $this->getTagetAreaDetails();

				if (empty($data))
				{
					$app = Factory::getApplication();
					// If no data is found redirect to default page and show warning.
					$app->enqueueMessage(Text::_('COM_COMPONENTBUILDER_NOT_FOUND_OR_ACCESS_DENIED'), 'warning');
					$app->redirect('index.php?option=com_componentbuilder');
					return false;
				}

				// set data object to item.
				$this->_item[$pk] = $data;
			}
			catch (\Exception $e)
			{
				if ($e->getCode() == 404)
				{
					// Need to go thru the error handler to allow Redirect to work.
					throw $e;
				}
				else
				{
					$this->setError($e);
					$this->_item[$pk] = false;
				}
			}
		}

		return $this->_item[$pk];
	}

	/**
	 * Method to get the styles that have to be included on the view
	 *
	 * @return  array    styles files
	 * @since   4.3
	 */
	public function getStyles(): array
	{
		return $this->styles;
	}

	/**
	 * Method to set the styles that have to be included on the view
	 *
	 * @return  void
	 * @since   4.3
	 */
	public function setStyles(string $path): void
	{
		$this->styles[] = $path;
	}

	/**
	 * Method to get the script that have to be included on the view
	 *
	 * @return  array    script files
	 * @since   4.3
	 */
	public function getScripts(): array
	{
		return $this->scripts;
	}

	/**
	 * Method to set the script that have to be included on the view
	 *
	 * @return  void
	 * @since   4.3
	 */
	public function setScript(string $path): void
	{
		$this->scripts[] = $path;
	}

	/**
	 * Get the uikit needed components
	 *
	 * @return mixed  An array of objects on success.
	 *
	 */
	public function getUikitComp()
	{
		if (isset($this->uikitComp) && UtilitiesArrayHelper::check($this->uikitComp))
		{
			return $this->uikitComp;
		}
		return false;
	}

	/**
	 * Method to get the target power
	 *
	 * @return  array|null
	 *
	 * @since   5.1.1
	 */
	protected function getTargetAreaPower(): ?array
	{
		$power = $this->input->getString('power', null) ?? 'error';
		return $this->powers[$power] ? ['class' => $this->powers[$power], 'area' => $power] : null;
	}

	/**
	 * Method to get the target area details.
	 *
	 * @return  array|null
	 * @throws \Exception
	 * @since   5.1.1
	 */
	protected function getTagetAreaDetails(): ?array
	{
		if (($Power = $this->getTargetAreaPower()) !== null)
		{
			try
			{
				$class = $this->getPowerClass($Power['class'], "{$Power['area']}.Remote.Get");
				if ($class === null)
				{
					return null;
				}
				return ['repos' => $class->paths(), 'area_class' => $Power['area'], 'headers' => $class->getIndexHeader(), 'area_name' => $class->getArea(), 'list_view' => $class->getListViewCodeName()];
			}
			catch (\Exception $e)
			{
				if ($e->getCode() == 404)
				{
					// Need to go thru the error handler to allow Redirect to work.
					throw $e;
				}
				else
				{
					$this->setError($e);
				}
			}
		}
		return null;
	}

	/**
	 * The powers that we can initialize
	 *
	 * @var    array
	 * @since  5.1.1
	 */
	protected array $powers = [
		'AdminView' => 'PackageFactory',
		'Component' => 'PackageFactory',
		'CustomAdminView' => 'PackageFactory',
		'CustomCode' => 'PackageFactory',
		'DynamicGet' => 'PackageFactory',
		'Field' => 'PackageFactory',
		'Joomla.Fieldtype' => 'FieldtypeFactory',
		'Joomla.Power' => 'JoomlaPowerFactory',
		'Layout' => 'PackageFactory',
		'Library' => 'PackageFactory',
		'JoomlaModule' => 'PackageFactory',
		'JoomlaPlugin' => 'PackageFactory',
		'Power' => 'PowerFactory',
		'SiteView' => 'PackageFactory',
		'Snippet' => 'SnippetFactory',
		'Template' => 'PackageFactory',
		'ClassExtends' => 'PackageFactory',
		'ClassProperty' => 'PackageFactory',
		'ClassMethod' => 'PackageFactory',
		'Placeholder' => 'PackageFactory',
		'Repository' => 'RepositoryFactory'
	];

	/**
	 * Method to get the power get class
	 *
	 * @param   string  $factoryName  The factory name
	 * @param   string  $getClass          The remote power class name
	 *
	 * @return  mixed
	 * @since   5.1.1
	 */
	protected function getPowerClass(string $factoryName, string $getClass)
	{
		return match ($factoryName) {
			'PowerFactory' => PowerFactory::_($getClass),
			'JoomlaPowerFactory' => JoomlaPowerFactory::_($getClass),
			'FieldtypeFactory' => FieldtypeFactory::_($getClass),
			'SnippetFactory' => SnippetFactory::_($getClass),
			'PackageFactory' => PackageFactory::_($getClass),
			'RepositoryFactory' => RepositoryFactory::_($getClass),
			default => null,
		};
	}
}
