<?php

/**
 * Copyright (c) D3 Data Development (Inh. Thomas Dartsch)
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 *
 * https://www.d3data.de
 *
 * @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
 * @author    D3 Data Development - Daniel Seifert <info@shopmodule.com>
 * @link      https://www.oxidmodule.com
 */

declare(strict_types=1);

namespace D3\Ordermanager\Application\Controller\Admin;

use D3\ModCfg\Application\Model\d3filesystem;
use D3\ModCfg\Application\Model\d3str;
use D3\ModCfg\Application\Model\Exception\wrongModIdException;
use D3\Ordermanager\Application\Model\Constants;
use D3\Ordermanager\Application\Model\d3ordermanager as Manager;
use D3\ModCfg\Application\Model\Configuration\d3_cfg_mod;
use D3\Ordermanager\Application\Model\d3ordermanager_vars as VariablesTrait;
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
use OxidEsales\Eshop\Application\Controller\Admin\AdminMall;  // required for non fallback case
use OxidEsales\Eshop\Core\Language;
use OxidEsales\Eshop\Core\Request;

class d3_cfg_ordermanageritem_mall extends d3AdminMall
{
    use VariablesTrait;

    private string $_sModId = 'd3_ordermanager';

    /**
     * DB table having oxshopincl and oxshopexcl fields we are going to deal with
     */
    protected $_sMallTable = "d3modprofile";

    protected $_blUseOwnOxid = true;

    protected $_sMenuItemTitle = 'd3mxordermanager';

    protected $_sMenuSubItemTitle = 'd3tbclordermanager_items_mall';

    protected $_sHelpLinkMLAdd;

    protected $_aNaviItems = [
        'new' => [
            'sScript' => 'top.oxid.admin.editThis( -1 );return false;',
            'sTranslationId' => 'D3_TOOLTIPS_NEWORDERMANAGER',
        ],
    ];

    /**
     * Class name of object to load
     */
    protected $_sObjectClassName = Manager::class;

    /**
     * constructor.
     */
    public function __construct()
    {
        d3GetOxidDIC()->getParameter($this->_DIC_Instance_Id . 'modcfgid') === $this->_sModId or
            throw oxNew(wrongModIdException::class, $this->_sModId);

        parent::__construct();
    }

    public function getProfile(): Manager
    {
        /** @var Manager $oManager */
        $oManager = d3GetOxidDIC()->get($this->_sObjectClassName);
        return $oManager;
    }

    public function render(): string
    {
        $oProfile = $this->getProfile();
        /** @var Request $request */
        $request = d3GetOxidDIC()->get('d3ox.ordermanager.'.Request::class);
        $soxId = $request->getRequestEscapedParameter("oxid");

        if ($this->_isSetOxid($soxId)) {
            // load object
            $oProfile->loadInLang($this->_iEditLang, $soxId);
            $oProfile = $this->_d3LoadInOtherLang($oProfile, $soxId);
        }

        $this->addTplParam('edit', $oProfile);

        return parent::render();
    }

    public function getUserMessages(): array
    {
        return [];
    }

    public function getLang(): Language
    {
        /** @var Language $lang */
        $lang = d3GetOxidDIC()->get('d3ox.ordermanager.'.Language::class);

        return $lang;
    }

    public function getHelpURL(): string
    {
        $sUrl = $this->d3GetSet()->getHelpURL();
        /** @var d3str $oD3Str */
        $oD3Str = d3GetOxidDIC()->get(d3str::class);

        if ($this->_sHelpLinkMLAdd) {
            $sUrl .= $oD3Str->unprefixedslashit($this->getLang()->translateString($this->_sHelpLinkMLAdd));
        }

        $oFS = d3GetOxidDIC()->get(d3filesystem::class);
        $aFileName = $oFS->splitFilename($sUrl);

        // has no extension
        if (false == $aFileName['ext']) {
            return $oD3Str->trailingslashit($sUrl);
        }

        return $sUrl;
    }

    /**
     * return type can't be defined, because of unmockable d3_cfg_mod class, use stdClass in test
     * @return d3_cfg_mod
     */
    public function d3GetSet()
    {
        /** @var d3_cfg_mod $modcfg */
        $modcfg = d3GetOxidDIC()->get('d3.ordermanager.modcfg');

        return $modcfg;
    }

    /**
     * exampleItem:     array('new' => array(
     *                      'sScript' => 'top.oxid.admin.editThis( -1 );return false;',
     *                      'sTranslationId' => 'foo',
     *                  )
     */
    public function getNaviItems(): array
    {
        return $this->_aNaviItems;
    }

    public function d3GetMenuItemTitle(): string
    {
        return $this->_sMenuItemTitle;
    }

    public function d3GetMenuSubItemTitle(): string
    {
        return $this->_sMenuSubItemTitle;
    }

    /**
     * @param $soxId
     */
    protected function _isSetOxid($soxId): bool
    {
        return isset($soxId) && $soxId && $soxId != "-1";
    }

    /**
     * @param               $soxId
     *
     */
    protected function _d3LoadInOtherLang(Manager $oProfile, $soxId): Manager
    {
        // load object in other languages
        $oOtherLang = $oProfile->getAvailableInLangs();

        if (false == isset($oOtherLang[$this->_iEditLang])) {
            $oProfile->loadInLang(key($oOtherLang), $soxId);
        }

        return $oProfile;
    }
}
