<?php
namespace JExtstore\Component\JRealtimeAnalytics\Administrator\Controller;
/**
 *
 * @package JREALTIMEANALYTICS::CPANEL::administrator::components::com_jrealtimeanalytics
 * @subpackage controllers
 * @author Joomla! Extensions Store
 * @copyright (C) 2013 - Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
 */
defined ( '_JEXEC' ) or die ( 'Restricted access' );
use Joomla\CMS\Component\ComponentHelper;
use JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\Http;
use JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\Controller as JRealtimeController;

/**
 * CPanel controller
 *
 * @package JREALTIMEANALYTICS::CPANEL::administrator::components::com_jrealtimeanalytics
 * @subpackage controllers
 * @since 1.0
 */
class CpanelController extends JRealtimeController {
	/**
	 * Setta il model state a partire dallo userstate di sessione
	 * @access protected
	 * @return object
	 */
	protected function setModelState($scope = 'default', $ordering = true): object {
		$option = $this->option;
	
		// Filter by current month - week - day
		$cParams = ComponentHelper::getParams($option);
		if($cParams->get('cpanelstats_period_interval', 'week') == 'day') {
			$startPeriod = date ( "Y-m-d" );
			$endPeriod = date ( "Y-m-d" );
		} elseif($cParams->get('cpanelstats_period_interval', 'week') == 'week') {
			$dt = time();
			$startPeriod = date('Y-m-d', strtotime('-1 week', $dt));
			$endPeriod = date('Y-m-d', $dt);
		} elseif ($cParams->get('cpanelstats_period_interval', 'week') == 'month') {
			$dt = time();
			$startPeriod = date('Y-m-d', strtotime('-1 month', $dt));
			$endPeriod = date('Y-m-d', $dt);
		}
	
		// Set model state
		// Get default model
		$defaultModel = $this->getModel ();
		$defaultModel->setState('fromPeriod', $startPeriod);
		$defaultModel->setState('toPeriod', $endPeriod);
		$defaultModel->setState('task', $this->task);
		$defaultModel->setState('cparams', $cParams);
	
		return $defaultModel;
	}
	
	/**
	 * Show Control Panel
	 * 
	 * @access public
	 * @return void
	 */
	function display($cachable = false, $urlparams = false) {
		$this->setModelState('cpanel');
		
		$view = $this->getView('cpanel', 'html', '', array('base_path' => $this->basePath, 'layout' => 'default'));
		
		// Dependency injection setter on view/model
		$HTTPClient = new Http();
		$view->httpClient = $HTTPClient;
		
		// No operations
		parent::display ($cachable, $urlparams); 
	}
}