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

class LevelrankControl extends AdminController {

    public function levelrank() {
        $model = $this->getModel('levelrank');
        $view = $this->getView('LevelrankHTML', 'html');
        // load language for upload image
        $lang = Factory::getLanguage();
        $lang->load('com_media');

        // load levelrank
        $_levelrank = $model->_load_levelrank();

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

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

    public function editlevelrank() {
        $model = $this->getModel('levelrank');
        $view = $this->getView('LevelrankHTML', 'html');
        // load language for upload image
        $lang = Factory::getLanguage();
        $lang->load('com_media');
        $result = $model->_edit_levelrank();
        $this->viewdata['row'] = $result[0];
        $this->viewdata['lists'] = $result[1];
        // Display
        $view->edit_levelrank();
    }

    public function savelevelrank() {
        Session::checkToken() or jexit(Text::_('JINVALID_TOKEN'));
        $model = $this->getModel('levelrank');
        // save levelrank(s)
        $model->_save_levelrank();
    }

    public function deletelevelrank() {

        $model = $this->getModel('levelrank');
        // delete levelrank(s)
        $model->_delete_levelrank();
    }

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

    public function detailrank() {

        $model = $this->getModel('levelrank');

        $view = $this->getView('LevelrankHTML', 'html');

        // load levelrank
        $_detailrank = $model->_load_detailrank();

        $this->viewdata['detailrank'] = $_detailrank[0];
        $this->viewdata['total'] = $_detailrank[1];
        $this->viewdata['limit'] = $_detailrank[2];
        $this->viewdata['limitstart'] = $_detailrank[3];

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

    /**
     * Save the item(s) to the menu selected
     */
    public function saveorder() {
        $cid = array_map('\\intval', $this->app->input->get('cid', array(), 'array'));

        $model = $this->getModel('levelrank');
        if ($model->setOrder($cid)) {
            $msg = Text::_('AUP_NEW_ORDERING_SAVED');
        }

        $this->redirect('index.php?option=com_userpoints&task=levelrank', $msg);
    }

    /**
     * Save rank/medal(s) order
     */
    public function orderup() {
        $cid = array_map('\\intval', $this->app->input->get('cid', array(), 'array'));

        if (isset($cid[0]) && $cid[0]) {
            $id = $cid[0];
        } else {
            $this->redirect('index.php?option=com_userpoints&task=levelrank', Text::_('No Items Selected'));
            return false;
        }

        $model = $this->getModel('levelrank');
        if ($model->orderItem($id, -1)) {
            $msg = Text::_('AUP_ITEM_MOVED_UP');
        } else {
            $msg = $model->getError();
        }
        $this->redirect('index.php?option=com_userpoints&task=levelrank', $msg);
    }

    /**
     * Save rank/medal(s) order
     */
    public function orderdown() {
        $cid = array_map('\\intval', $this->app->input->get('cid', array(), 'array'));

        if (isset($cid[0]) && $cid[0]) {
            $id = $cid[0];
        } else {
            $this->redirect('index.php?option=com_userpoints&task=levelrank', Text::_('No Items Selected'));
            return false;
        }

        $model = $this->getModel('levelrank');
        if ($model->orderItem($id, 1)) {
            $msg = Text::_('AUP_ITEM_MOVED_DOWN');
        } else {
            $msg = $model->getError();
        }
        $this->redirect('index.php?option=com_userpoints&task=levelrank', $msg);
    }
}
