<?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\Session\Session;
use BlackSheepResearch\Component\UserPoints\Administrator\AdminController;
use BlackSheepResearch\Component\UserPoints\Site\UserPoints;
use BlackSheepResearch\Component\UserPoints\Site\Problems\Rule;
use BlackSheepResearch\Component\UserPoints\Site\Problems\User;
use BlackSheepResearch\Component\UserPoints\Site\Problems\PointsUser;

class RulesControl extends AdminController {

    public function rules() {
        $model = $this->getModel('rules');
        $view = $this->getView('RulesHTML', 'html');
        // load rules
        $_rules = $model->_load_rules();
        $this->viewdata['rules'] = $_rules[0];
        $this->viewdata['total'] = $_rules[1];
        $this->viewdata['limit'] = $_rules[2];
        $this->viewdata['limitstart'] = $_rules[3];
        $this->viewdata['lists'] = $_rules[4];
        $view->show();
    }

    public function applyrule() {
        Session::checkToken() or jexit(Text::_('JINVALID_TOKEN'));
        $model = $this->getModel('rules');
        // save rule(s)
        $apply = 1;
        $model->_save_rule($apply);
    }

    public function copyrule() {
        $model = $this->getModel('rules');
        // copy rule(s)
        $model->_copy_rule();
    }

    public function deleterule() {
        $model = $this->getModel('rules');
        // delete rule(s)
        $model->_delete_rule();
    }

    public function editrule() {
        $model = $this->getModel('rules');
        $view = $this->getView('RulesHTML', 'html');

        $_row = $model->_edit_rule();
        if (!$_row->id) {
            $_row->allow_referral = 1;
        }
        $_rowChainedRules = Rule::getChainedRules($_row->id);

        $_groups = User::getGroupsList(intval($_row->content_items));

        $this->viewdata['row'] = $_row;
        $this->viewdata['groups'] = $_groups;
        $this->viewdata['chainedrules'] = $_rowChainedRules;

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

    public function saverule() {
        Session::checkToken() or jexit(Text::_('JINVALID_TOKEN'));
        $model = $this->getModel('rules');
        // save rule(s)
        $model->_save_rule();
    }

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

    public function applycustomrule() { // several users
        $cid = Factory::getApplication()->input->get('cid', array(0), 'array');

        if (count($cid) < 1) {
            \JViewContent::displayError(Text::_('Select an item to') . ' ' . Text::_($action));
            return false;
        }
        $cid = implode(",", $cid);

        $view = $this->getView('RulesHTML', 'html');
        $this->viewdata['cid'] = $cid;
        $view->_displaycustomrulepoints();
    }

    public function savecustomrulepoints() { // save several users for custom rule points
        $cid = Factory::getApplication()->input->get('cid', '', 'string');
        $points = Factory::getApplication()->input->get('points', 0, 'float');
        $reason = Factory::getApplication()->input->get('reason', '', 'SAFE_HTML ');
        if ($cid) {
            foreach (PointsUser::getMany(explode(",", $cid)) as $pointsuser) {
                if ($pointsuser->referreid) {
                    UserPoints::userpoints('sysplgup_custom', $pointsuser->referreid, 0, '', $reason, $points);
                    $pointsuser->checkTotalAfterRecalculate('sysplgup_custom');
                }
            }
        }
        Factory::getApplication()->enqueueMessage(Text::_('AUP_RECALCULATION_MADE'));
        $this->redirect("index.php?option=com_userpoints&task=statistics");
    }
}
