<?php
/**
 * NOTICE OF LICENSE.
 *
 * Copyright 2022 Kosmonaft.dev
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 *  @author    Alexandre DEBUSSCHÈRE <alexandre@kosmonaft.dev>
 *  @copyright 2022 - 2023 Kosmonaft.dev
 *  @license   MIT
 */

if (!defined('_PS_VERSION_')) {
    exit;
}

/**
 * Class MRMaxAdminController
 */
class MRMaxAdminController extends AdminController
{

    /** @var MRMax */
    protected $module;

    /** @var string */
    protected $basename;

    /** @var string */
    protected $current_index;

    /** @var bool */
    protected $debug;

    /**
     * MRMaxAdminController constructor.
     */
    public function __construct()
    {
        $this->bootstrap = true;
        $this->show_toolbar = false;
        $this->context = Context::getContext();

        parent::__construct();

        $this->module = Module::getInstanceByName('mrmax');

        $this->basename = static::class;
        $this->debug = Tools::getValue('debug');
    }

    /**
     * @param string $string
     * @param null $class
     * @param false $addslashes
     * @param bool $htmlentities
     * @return string
     */
    protected function l($string, $class = null, $addslashes = false, $htmlentities = true)
    {
        unset($class, $addslashes, $htmlentities);

        // On older PrestaShop version (~1.6.1.24) it seems that the parent::__construct() in the constructor throws
        // an exception because : Call to a member function l() on null.
        // Reset the module instance in case it is missing.
        if (!$this->module instanceof MRMax) {
            $this->module = Module::getInstanceByName('mrmax');
        }

        return $this->module->l($string, $this->basename);
    }

    /**
     * @param false $disable
     * @return bool
     */
    public function viewAccess($disable = false)
    {
        unset($disable);

        return true;
    }

    /**
     * @throws PrestaShopException
     */
    public function initContent()
    {
        $this->display = 'view';
        $this->page_header_toolbar_title = $this->toolbar_title = 'Mondial Relay';

        parent::initContent();

        $this->content .= $this->renderForm();

        $this->context->smarty->assign(['content' => $this->content]);
    }

    /**
     * @param false $isNewTheme
     */
    public function setMedia($isNewTheme = false)
    {
        parent::setMedia($isNewTheme);

        $this->addJS($this->module->getLocalPath().'views/js/riot+compiler.min.js');
    }

    /**
     * @throws PrestaShopException
     */
    protected function redirectToOrder()
    {
        $id_order = (int)Tools::getValue('id_order');

        if (version_compare(_PS_VERSION_, '1.7', '>=')) {
            Tools::redirectAdmin(Context::getContext()->link->getAdminLink(
                'AdminOrders',
                true,
                array(
                    'route' => 'admin_orders_view',
                    'action' => 'view',
                    'orderId'=> $id_order,
                    'id_order'=> $id_order,
                    'id'=> $id_order
                )
            ));
        } else {
            Tools::redirectAdmin(sprintf(
                'index.php?controller=AdminOrders&id_order=%s&vieworder&token=%s',
                $id_order,
                Tools::getAdminTokenLite('AdminOrders')
            ));
        }
    }
}
