<?php

/*
 * @component UserPoints
 * @copyright Copyright (C) 2015-2022 Bernard Gilly - Adrien Roussel - Martin Brampton
 * @license : GNU/GPL
 * @Website : https://remository.com
 */

namespace BlackSheepResearch\Component\UserPoints\Site;

// no direct access
defined('_JEXEC') or die('Restricted access');

use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Controller\BaseController;

/**
 * @package UserPoints
 */
class Controller extends BaseController {

    static protected $myspace;
    static protected $suffix;

    protected $app = null;
    protected $viewdata = [];

    /**
     * Custom Constructor
     */
    public function __construct() {
        parent::__construct();
        $this->app = Factory::getApplication();
    }

    public function __get (string $name) {
        return $this->viewdata[$name] ?? '';
    }

    public function redirect($url = '', $message = '', $type = 'info') {
        $this->app->enqueueMessage($message, $type);
        $this->app->redirect($url);
    }

    public function getModel($name = '', $prefix = '', $config = []) {
        $classname = static::$myspace . '\\Models\\' . ucfirst($name);
        return new $classname();
    }

    public function getView($name = '', $type = '', $prefix = '', $config = []) {
	$classname = static::$myspace . '\\Viewers\\' . ucfirst($name) . static::$suffix;
        return new $classname($this);
    }

    protected function getSingleCID () : ?int {
        return Factory::getApplication()->input->get('cid', null, 'int');
    }

    protected function getFirstCID () : ?int {
        $cids = $this->getArrayCID();
        return count($cids) ? $cids[0] : null;
    }

    protected function getArrayCID () : array {
        return array_map('\\intval', Factory::getApplication()->input->get('cid', [], 'array'));
    }

}

// end class
