<?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 - Max Buhe, Daniel Seifert <info@shopmodule.com>
 * @link      https://www.oxidmodule.com
 */

declare(strict_types=1);

namespace D3\ManufacturerInformation\Application\Controller\Admin;

use D3\ManufacturerInformation\Application\Model\Constants;
use D3\ManufacturerInformation\Modules\Application\Model\D3ManufacturerLongDesc;
use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController;
use OxidEsales\Eshop\Application\Model\Manufacturer;
use OxidEsales\Eshop\Core\Registry;

class ManufacturerInfoLongdesc extends AdminDetailsController
{
    /**
     * @return string
     */
    public function render()
    {
        parent::render();

        $this->_aViewData['edit'] = $oManufacturer = oxNew(\OxidEsales\Eshop\Application\Model\Manufacturer::class);

        $sOXID = $this->_aViewData["oxid"] = $this->getEditObjectId();
        if (isset($sOXID) && $sOXID != "-1") {
            // load object
            $d3manufacturerlang = \OxidEsales\Eshop\Core\Registry::getConfig()->getRequestParameter("catlang");

            if (!isset($d3manufacturerlang)) {
                $d3manufacturerlang = $this->_iEditLang;
            }

            $this->_aViewData["catlang"] = $d3manufacturerlang;

            $oManufacturer->loadInLang($d3manufacturerlang, $sOXID);

            //Disable editing for derived items
            if ($oManufacturer->isDerived()) {
                $this->_aViewData['readonly'] = true;
            }

            foreach (\OxidEsales\Eshop\Core\Registry::getLang()->getLanguageNames() as $id => $language) {
                $oLang = new \stdClass();
                $oLang->sLangDesc = $language;
                $oLang->selected = ($id == $this->_iEditLang);
                $this->_aViewData["otherlang"][$id] = clone $oLang;
            }
        }

        $this->_aViewData["editor"] = $this->generateTextEditor("100%", 280, $oManufacturer, "oxmanufacturers__d3description", "list.tpl.css");

        return "d3manufacturerlongdesc.tpl";
    }

    /**
     * @return void
     * @throws \Exception
     */
    public function save()
    {
        parent::save();

        $iManufacturerLang = intval(\OxidEsales\Eshop\Core\Registry::getConfig()->getRequestParameter("catlang"));
        $sOXID = $this->getEditObjectId();
        $this->setEditObjectId($sOXID);

        if ($sOXID == "-1") {
            return;
        }

        /** @var Manufacturer|D3ManufacturerLongDesc $oManufacturer */
        $oManufacturer = oxNew(Manufacturer::class);
        $oManufacturer->loadInLang($iManufacturerLang, $sOXID);

        if (!$oManufacturer->load($sOXID)) {
            return;
        }

        $aParams = Registry::getRequest()->getRequestEscapedParameter("editval");
        $oManufacturer->assign($aParams);

        if (false === is_null($this->d3GetLongdescParamValue())) {
            $oManufacturer->d3SetLongdesc($this->d3GetLongdescParamValue());
            $oManufacturer->save();
        }
    }

    /**
     * Returns string which must be edited by editor
     *
     * @param \OxidEsales\Eshop\Core\Model\BaseModel $oObject object with field will be used for editing
     * @param string                                 $sField  name of editable field
     *
     * @return string
     * @deprecated underscore prefix violates PSR12, will be renamed to "getEditValue" in next major
     */
    protected function _getEditValue($oManufacturer, $sField) // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
    {
        $sEditObjectValue = '';

        if ($oManufacturer and $oManufacturer->getId()) {
            $sEditObjectValue = $this->_processEditValue($oManufacturer->getFieldData(Constants::D3_OXMANUFACTURERS_LONGDESC_FIELDNAME));
        }

        return $sEditObjectValue;
    }

    /**
     * @return string
     */
    public function d3GetLongdescParamValue(): string
    {
        return (string) $aParams = Registry::getRequest()->getRequestEscapedParameter("editval")['oxmanufacturers__d3description'];
    }

    /**
     * inhaltliche Uebernahme analog Article_Main
     * wird genutzt zum Abspeichern der Artikel-Langbeschreibung
     *
     * @param $sValue
     *
     * @return mixed
     */
    protected function _processLongDesc($sValue)
    {
        $aSearch  = ['&amp;nbsp;', '&amp;', '&quot;', '&lang=', '<p>&nbsp;</p>', '<p>&nbsp; </p>'];
        $aReplace = ['&nbsp;', '&', '"', '&amp;lang=', '', ''];

        return str_replace($aSearch, $aReplace, $sValue);
    }
}