<?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\Administrator\Controllers;

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Session\Session;
use BlackSheepResearch\Component\UserPoints\Administrator\AdminController;

class StatisticsControl extends AdminController {

    public function statistics() {
        $model = $this->getModel('statistics');
        $view = $this->getView('StatisticsHTML', 'html');

        $_stats = $model->_load_users();

        $this->viewdata['usersStats'] = $_stats[0];
        $this->viewdata['total'] = $_stats[1];
        $this->viewdata['limit'] = $_stats[2];
        $this->viewdata['limitstart'] = $_stats[3];
        $this->viewdata['lists'] = $_stats[4];
        $this->viewdata['ranksexist'] = $_stats[5];
        $this->viewdata['medalsexist'] = $_stats[6];
        $view->show();
    }

    public function applyuser () {
        Session::checkToken() or jexit(Text::_('JINVALID_TOKEN'));
        $model = $this->getModel('statistics');
        // save general user stats
        $apply = 1;
        $model->_save_user($apply);
    }

    public function awardedmedal () {
        $model = $this->getModel('statistics');
        // save general medal
        $model->_save_medaluser();
    }

    public function removemedaluser () {
        $model = $this->getModel('statistics');
        // delete user medal
        $model->_delete_medaluser();
    }

    public function edituser () {
        $model = $this->getModel('statistics');
        $view = $this->getView('StatisticsHTML', 'html');

        $_row = $model->_edit_user();

        $this->viewdata['row'] = $_row[0];
        $this->viewdata['listrank'] = $_row[1];
        $this->viewdata['medalsexist'] = $_row[2];
        $this->viewdata['medalslistuser'] = $_row[3];
        $this->viewdata['listmedals'] = $_row[4];

        $model2 = $this->getModel('user');
        $cid = Factory::getApplication()->input->get('cid', array(), 'array');
        if (!is_array($cid)) {
            $cid = array(0);
        }
        $cid = $cid[0];

        // load user details
        $userDetails = $model2->_load_details_user_edit_user($cid);

        $this->viewdata['userDetails'] = $userDetails[0];
        $this->viewdata['total'] = $userDetails[1];
        $this->viewdata['limit'] = $userDetails[2];
        $this->viewdata['limitstart'] = $userDetails[3];
        $this->viewdata['lists'] = $userDetails[4];
        //$this->viewdata['name'] = $_name ;
        $this->viewdata['name'] = $_row[0]->name;
        $this->viewdata['cid'] = $cid;

        // Display
        $view->edit_user();
    }

    public function saveuser () {
        Session::checkToken() or jexit(Text::_('JINVALID_TOKEN'));
        $model = $this->getModel('statistics');
        // save general user stats
        $model->_save_user();
    }

    public function canceluser () {
        $this->redirect("index.php?option=com_userpoints&task=statistics");
    }

    public function stats() {

        $date_start = Factory::getApplication()->input->get('date_start', '', 'string');
        $date_end = Factory::getApplication()->input->get('date_end', '', 'string');
        $rule = Factory::getApplication()->input->get('rule', '', 'int');

        $model = $this->getModel('statistics');
        $view = $this->getView('StatsHTML', 'html');

        $result = $model->_pointsearned();
        $result2 = $model->_pointsspent();

        $average_points_earned_by_day = $model->_average_points_earned_by_day();
        $average_points_spent_by_day = $model->_average_points_spent_by_day();
        $numusers = $model->_get_num_users();
        $resultinactiveusers = $model->_get_inactive_members();
        $inactiveusers = $resultinactiveusers[0];
        $num_days_inactiveusers_rule = $resultinactiveusers[1];

        $communitypoints = $model->_totalcurrentcommunitypoints();

        $listRules = $model->_getListRules($rule);

        $this->viewdata['result'] = $result;
        $this->viewdata['result2'] = $result2;
        $this->viewdata['date_start'] = $date_start;
        $this->viewdata['date_end'] = $date_end;
        $this->viewdata['listrules'] = $listRules;
        $this->viewdata['communitypoints'] = $communitypoints;
        $this->viewdata['average_points_earned_by_day'] = $average_points_earned_by_day;
        $this->viewdata['average_points_spent_by_day'] = $average_points_spent_by_day;
        $this->viewdata['numusers'] = $numusers;
        $this->viewdata['inactiveusers'] = $inactiveusers;
        $this->viewdata['num_days_inactiveusers_rule'] = $num_days_inactiveusers_rule;

        // Display
        $view->display();
    }
}
