<?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\HTML\HTMLHelper;
use Joomla\CMS\Session\Session;
use BlackSheepResearch\Component\UserPoints\Administrator\AdminController;
use BlackSheepResearch\Component\UserPoints\Site\Problems\Coupon;

class CouponControl extends AdminController {

    public function couponcodes() {

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

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

        // load coupons
        $_couponcodes = $model->_load_couponcodes();

        foreach ($_couponcodes[0] as $coupon) {
            $coupons[] = Coupon::makeOneProblem($coupon);
        }

        $this->viewdata['couponcodes'] = $coupons ?? [];
        $this->viewdata['total'] = $_couponcodes[1];
        $this->viewdata['limit'] = $_couponcodes[2];
        $this->viewdata['limitstart'] = $_couponcodes[3];
        $this->viewdata['lists'] = $_couponcodes[4];

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

    public function editcoupon() {

        $model = $this->getModel('couponcodes');
        $view = $this->getView('CouponcodesHTML', 'html');

        $result = $model->_edit_coupon();

        $this->viewdata['row'] = $result[0];
        $this->viewdata['lists'] = $result[1];

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

    public function savecoupon() {

        Session::checkToken() or jexit(Text::_('JINVALID_TOKEN'));
        $model = $this->getModel('couponcodes');
        // save coupon(s)
        $model->_save_coupon();
    }

    public function applycoupon() {

        Session::checkToken() or jexit(Text::_('JINVALID_TOKEN'));

        $model = $this->getModel('couponcodes');
        // save coupon(s)
        $apply = 1;
        $model->_save_coupon($apply);
    }

    public function deletecoupon() {

        $model = $this->getModel('couponcodes');
        // delete coupon(s)
        $model->_delete_coupon();
    }

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

    public function coupongenerator() {

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

        $lists = array();
        $lists['public'] = HTMLHelper::_('select.booleanlist', 'public', 'class="inputbox"', 1);
        $lists['printable'] = HTMLHelper::_('select.booleanlist', 'printable', 'class="inputbox"', 0);

        $this->viewdata['lists'] = $lists;

        $view->generate_coupon();
    }

    public function savecoupongenerator() {

        $model = $this->getModel('CouponcodesHTML');
        // save coupon(s)
        $model->_save_coupongenerator();
    }

    // show tracking QR code coupon
    public function qrcodestats() {

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

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

        $id = Factory::getApplication()->input->get('id', 0, 'int');

        // load coupons
        $_qrcodestats = $model->_load_qrcodestats($id);

        $this->viewdata['qrcodestats'] = $_qrcodestats[0];
        $this->viewdata['total'] = $_qrcodestats[1];
        $this->viewdata['limit'] = $_qrcodestats[2];
        $this->viewdata['limitstart'] = $_qrcodestats[3];
        $this->viewdata['id'] = $id;

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

}
