<?php
namespace Medienreaktor\Products\Domain\Model;

/***
 *
 * This file is part of the "Products" Extension for TYPO3 CMS.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 *  (c) 2017 Daniel Kestler &lt;daniel.kestler@medienreaktor&gt;, medienreaktor GmbH
 *
 ***/

/**
 * Product
 */
class Product extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
    /**
     * sku
     *
     * @var string
     */
    protected $sku = '';

    /**
     * name
     *
     * @var string
     */
    protected $name = '';

    /**
     * keywords
     *
     * @var string
     */
    protected $keywords = '';

    /**
     * description
     *
     * @var string
     */
    protected $description = '';

    /**
     * descriptionFullWidth
     *
     * @var string
     */
    protected $descriptionFullWidth = '';

    /**
     * features
     *
     * @var string
     */
    protected $features = '';

    /**
     * delivery
     *
     * @var string
     */
    protected $delivery = '';

    /**
     * footnotes
     *
     * @var string
     */
    protected $footnotes = '';

    /**
     * productgroup
     *
     * @var \Medienreaktor\Products\Domain\Model\ProductGroup
     */
    protected $productgroup = NULL;

    /**
     * productclass
     *
     * @var \Medienreaktor\Products\Domain\Model\ProductClass
     */
    protected $productclass = NULL;

    /**
	 * images
	 *
	 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
	 */
	protected $images = NULL;

    /**
     * piFlexform
     *
     * @var string
     */
    protected $piFlexform = '';

    /**
     * documentation
     *
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
     */
    protected $documentation = NULL;

    /**
     * whitepaper
     *
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
     */
    protected $whitepaper = NULL;

    /**
     * flyer
     *
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
     */
    protected $flyer = NULL;

    /**
     * license
     *
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
     */
    protected $license = NULL;

    /**
     * software
     *
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
     */
    protected $software = NULL;

    /**
     * drivers
     *
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
     */
    protected $drivers = NULL;

    /**
     * planningaids
     *
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
     */
    protected $planningaids = NULL;

    /**
     * distributor
     *
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
     */
    protected $distributor = NULL;

    /**
     * __construct
     */
    public function __construct() {
        //Do not remove the next line: It would break the functionality
        $this->initStorageObjects();
    }

    /**
     * Initializes all ObjectStorage properties
     * Do not modify this method!
     * It will be rewritten on each save in the extension builder
     * You may modify the constructor of this class instead
     *
     * @return void
     */
    protected function initStorageObjects() {
        $this->images = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
        $this->documentation = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
        $this->whitepaper = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
        $this->flyer = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
        $this->license = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
        $this->software = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
        $this->drivers = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
        $this->planningaids = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
        $this->distributor = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
    }

    /**
     * Returns the sku
     *
     * @return string $sku
     */
    public function getSku()
    {
        return $this->sku;
    }

    /**
     * Sets the sku
     *
     * @param string $sku
     * @return void
     */
    public function setSku($sku): void
    {
        $this->sku = $sku;
    }

    /**
     * Returns the name
     *
     * @return string $name
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Sets the name
     *
     * @param string $name
     * @return void
     */
    public function setName($name): void
    {
        $this->name = $name;
    }

    /**
     * Returns the keywords
     *
     * @return string $keywords
     */
    public function getKeywords()
    {
        return $this->keywords;
    }

    /**
     * Sets the keywords
     *
     * @param string $keywords
     * @return void
     */
    public function setKeywords($keywords): void
    {
        $this->keywords = $keywords;
    }

    /**
     * Returns the description
     *
     * @return string $description
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Sets the description
     *
     * @param string $description
     * @return void
     */
    public function setDescription($description): void
    {
        $this->description = $description;
    }

    /**
     * Returns the descriptionFullWidth
     *
     * @return string $descriptionFullWidth
     */
    public function getDescriptionFullWidth()
    {
        return $this->descriptionFullWidth;
    }

    /**
     * Sets the descriptionFullWidth
     *
     * @param string $descriptionFullWidth
     * @return void
     */
    public function setDescriptionFullWidth($descriptionFullWidth): void
    {
        $this->descriptionFullWidth = $descriptionFullWidth;
    }

    /**
     * Returns the features
     *
     * @return string $features
     */
    public function getFeatures()
    {
        return explode(LF, $this->features);
    }

    /**
     * Sets the features
     *
     * @param string $features
     * @return void
     */
    public function setFeatures($features): void
    {
        $this->features = $features;
    }

    /**
     * Returns the delivery
     *
     * @return string $delivery
     */
    public function getDelivery()
    {
        return $this->delivery;
    }

    /**
     * Sets the delivery
     *
     * @param string $delivery
     * @return void
     */
    public function setDelivery($delivery): void
    {
        $this->delivery = $delivery;
    }

    /**
     * Returns the footnotes
     *
     * @return string $footnotes
     */
    public function getFootnotes()
    {
        return $this->footnotes;
    }

    /**
     * Sets the footnotes
     *
     * @param string $footnotes
     * @return void
     */
    public function setFootnotes($footnotes): void
    {
        $this->footnotes = $footnotes;
    }

    /**
     * Returns the productGroup
     *
     * @return \Medienreaktor\Products\Domain\Model\ProductGroup $productgroup
     */
    public function getProductGroup() {
        return $this->productgroup;
    }

    /**
     * Sets the productGroup
     *
     * @param \Medienreaktor\Products\Domain\Model\ProductGroup $productgroup
     * @return void
     */
    public function setProductGroup(\Medienreaktor\Products\Domain\Model\ProductGroup $productgroup): void {
        $this->productgroup = $productgroup;
    }

    /**
     * Returns the productclass
     *
     * @return \Medienreaktor\Products\Domain\Model\ProductClass $productclass
     */
    public function getProductClass() {
        return $this->productclass;
    }

    /**
     * Sets the productclass
     *
     * @param \Medienreaktor\Products\Domain\Model\ProductClass $productclass
     * @return void
     */
    public function setProductClass(ProductClass $productclass): void {
        $this->productclass = $productclass;
    }

    /**
     * Returns the images
     *
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage $images
     */
    public function getImages() {
        return $this->images;
    }

    /**
     * Sets the images
     *
     * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $images
     * @return void
     */
    public function setImages(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $images): void {
        $this->images = $images;
    }

    /**
     * Returns the documentation
     *
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage $documentation
     */
    public function getDocumentation() {
        return $this->documentation;
    }

    /**
     * Sets the documentation
     *
     * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $documentation
     * @return void
     */
    public function setDocumentation(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $documentation): void {
        $this->documentation = $documentation;
    }

    /**
     * Returns the whitepaper
     *
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage $whitepaper
     */
    public function getWhitepaper() {
        return $this->whitepaper;
    }

    /**
     * Sets the whitepaper
     *
     * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $whitepaper
     * @return void
     */
    public function setWhitepaper(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $whitepaper): void {
        $this->whitepaper = $whitepaper;
    }

    /**
     * Returns the flyer
     *
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage $flyer
     */
    public function getFlyer() {
        return $this->flyer;
    }

    /**
     * Sets the flyer
     *
     * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $flyer
     * @return void
     */
    public function setFlyer(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $flyer): void {
        $this->flyer = $flyer;
    }

    /**
     * Returns the license
     *
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage $license
     */
    public function getLicense() {
        return $this->license;
    }

    /**
     * Sets the license
     *
     * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $license
     * @return void
     */
    public function setLicense(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $license): void {
        $this->license = $license;
    }

    /**
     * Returns the software
     *
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage $software
     */
    public function getSoftware() {
        return $this->software;
    }

    /**
     * Sets the software
     *
     * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $software
     * @return void
     */
    public function setSoftware(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $software): void {
        $this->software = $software;
    }

    /**
     * Returns the drivers
     *
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage $drivers
     */
    public function getDrivers() {
        return $this->drivers;
    }

    /**
     * Sets the drivers
     *
     * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $drivers
     * @return void
     */
    public function setDrivers(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $drivers): void {
        $this->drivers = $drivers;
    }

    /**
     * Returns the planningaids
     *
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage $planningaids
     */
    public function getPlanningaids() {
        return $this->planningaids;
    }

    /**
     * Sets the planningaids
     *
     * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $planningaids
     * @return void
     */
    public function setPlanningaids(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $planningaids): void {
        $this->planningaids = $planningaids;
    }

    /**
     * Returns the distributor
     *
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage $distributor
     */
    public function getDistributor() {
        return $this->distributor;
    }

    /**
     * Sets the distributor
     *
     * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $distributor
     * @return void
     */
    public function setDistributor(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $distributor): void {
        $this->distributor = $distributor;
    }

    public function getDynamicProperties() {
        $flexFormContent = $this->piFlexform;
        $flexFormService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Service\\FlexFormService');
        $fields = $flexFormService->convertFlexFormContentToArray($flexFormContent);

        return $fields;
    }

    public function getDistinctDynamicProperties() {
        $productGroup = $this->getProductGroup();
        $dynamicProperties = $this->getDynamicProperties();

        if($productGroup) {
            $inheritedProperties = $productGroup->getDynamicProperties();

            $dynamicProperties = array_filter($dynamicProperties);
            $mergedArray = array_replace($inheritedProperties, $dynamicProperties);
            return $mergedArray;
        }

        return $dynamicProperties;
    }
}
