<?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 BlackSheepResearch\Component\UserPoints\Administrator\AdminController;

class UsersControl extends AdminController {

    /**
     * Show Control Panel
     */
    public function usersync() {
        $model = $this->getModel('users');
        $model->Synchro();
        $this->redirect("index.php?option=com_userpoints&task=cpanel", Text::_('AUP_ALLUSERSSYNC'));
    }

    public function activities() {
        $model = $this->getModel('users');
        $view = $this->getView('ActivitiesHTML', 'html');

        $_activities = $model->_load_activities();

        $this->viewdata['activities'] = $_activities[0];
        $this->viewdata['total'] = $_activities[1];
        $this->viewdata['limit'] = $_activities[2];
        $this->viewdata['limitstart'] = $_activities[3];
        $this->viewdata['lists'] = $_activities[4];

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

    public function purge() {
        $model = $this->getModel('users');
        $model->_purge_expires();
        $msg = Text::_('AUP_SUCCESSFULLYPURGE');
        $this->redirect("index.php?option=com_userpoints&task=cpanel", $msg);
    }

    public function recalculate() {
        $model = $this->getModel('users');
        $model->_recalculate_points();
        $model->Recalc();
        // $this->redirect("index.php?option=com_userpoints&task=cpanel&recalculate=start");
        $this->redirect("index.php?option=com_userpoints&task=cpanel", Text::_('AUP_SUCCESSFULLYRECALCULATE'));
    }

    public function resetpoints() {
        $model = $this->getModel('users');
        $model->_resetpoints();
        $msg = Text::_('AUP_SUCCESSFULLYRESETTOZERO');
        $this->redirect("index.php?option=com_userpoints&task=cpanel", $msg);
    }

    public function savemaxpoints() {
        $app = Factory::getApplication();
        $model = $this->getModel('users');
        $view = $this->getView('MaxpointsHTML', 'html');
        $_setmaxpoints = $model->_setmaxpoints();
        $msg = Text::_('AUP_NEWMAXPOINTS') . " " . $_setmaxpoints;
        $urlredirect = "index.php?option=com_userpoints&task=setmaxpoints&newmaxpoints=$_setmaxpoints";
        $this->redirect($urlredirect, $msg);
    }

    public function showdetails() {
        $_name = Factory::getApplication()->input->get('name', '', 'cmd');
        $_cid = Factory::getApplication()->input->get('cid', '', '', 'cmd');

        $model = $this->getModel('user');
        $view = $this->getView('UserHTML', 'html');

        // load user details
        $userDetails = $model->_load_details_user();

        $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['cid'] = $_cid;

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

    public function edituserdetails() {
        $_name = Factory::getApplication()->input->get('name', '', 'cmd');

        $model = $this->getModel('user');
        $view = $this->getView('UserHTML', 'html');

        $_row = $model->_edit_pointsDetails();
        $_rule_name = $model->_get_rule_name($_row->rule);

        $this->viewdata['row'] = $_row;
        $this->viewdata['name'] = $_name;
        $this->viewdata['rulename'] = $_rule_name;

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

    public function saveuserdetails() {
        $model = $this->getModel('user');
        // save user details
        $model->_save_user_details();
    }

    public function canceluserdetails() {
        $redirecturl = Factory::getApplication()->input->get('redirect', '', 'string');
        $this->redirect("index.php?option=com_userpoints&task=" . $redirecturl);
    }

    public function deleteuserdetails() {
        $model = $this->getModel('user');
        // delete
        $model->_delete_user_details();
    }

    public function deleteuserallactivities() {
        $model = $this->getModel('user');
        // delete
        $model->_delete_user_all_activities();
    }

}
