<?php

namespace Lc\ShopBundle\Model;

use App\Entity\Hub;
use App\Entity\ProductFamily;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Lc\ShopBundle\Context\FilterMerchantInterface;
use Lc\ShopBundle\Context\TreeInterface;

/**
 * @ORM\MappedSuperclass()
 */
abstract class ProductCategory extends AbstractDocumentEntity implements TreeInterface, FilterMerchantInterface
{

        /**
         * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\MerchantInterface", inversedBy="productCategories")
         * @ORM\JoinColumn(nullable=false)
         */
        protected $merchant;

        /**
         * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\ProductCategoryInterface", inversedBy="childrens")
         */
        protected $parent;

        /**
         * @ORM\OneToMany(targetEntity="Lc\ShopBundle\Context\ProductCategoryInterface", mappedBy="parent" , fetch="EAGER"))
         * @ORM\OrderBy({"position" = "ASC"})
         */
        protected $childrens;

        /**
         * @ORM\ManyToMany(targetEntity="App\Entity\ProductFamily", mappedBy="productCategories")
         */
        protected $productFamilies;

        public function __construct()
        {
                $this->childrens = new ArrayCollection();
                $this->productFamilies = new ArrayCollection();
        }

        public function __toString()
        {
                // TODO: Implement __toString() method.
                return $this->getTitle();
        }

        public function getParent(): ?self
        {
                return $this->parent;
        }

        public function setParent(?self $productCategory): self
        {
                $this->parent = $productCategory;

                return $this;
        }

        public function getParentCategory()
        {
                if($this->getParent()) {
                        return $this->getParent() ;
                }

                return $this ;
        }

        /**
         * @return Collection|self[]
         */
        public function getChildrens(): Collection
        {
                return $this->childrens;
        }

        public function addChildren(self $productCategory): self
        {
                if (!$this->childrens->contains($productCategory)) {
                        $this->childrens[] = $productCategory;
                        $productCategory->setParent($this);
                }

                return $this;
        }

        public function removeChildren(self $productCategory): self
        {
                if ($this->childrens->contains($productCategory)) {
                        $this->childrens->removeElement($productCategory);
                        // set the owning side to null (unless already changed)
                        if ($productCategory->getParent() === $this) {
                                $productCategory->setParent(null);
                        }
                }

                return $this;
        }

        /**
         * @return Collection|ProductFamily[]
         */
        public function getProductFamilies(): Collection
        {
                return $this->productFamilies;
        }

        public function addProductFamily(ProductFamily $productFamily): self
        {
                if (!$this->productFamilies->contains($productFamily)) {
                        $this->productFamilies[] = $productFamily;
                        $productFamily->addProductCategory($this);
                }

                return $this;
        }

        public function removeProductFamily(ProductFamily $productFamily): self
        {
                if ($this->productFamilies->contains($productFamily)) {
                        $this->productFamilies->removeElement($productFamily);
                        $productFamily->removeProductCategory($this);
                }

                return $this;
        }

        public function countProductFamilies($status = null)
        {
                $count = 0 ;

                foreach($this->getProductFamilies() as $productFamily) {
                        if($status === null || ($status !== null && $productFamily->getStatus() == $status)) {
                                $count ++ ;
                        }
                }

                return $count ;
        }

        public function getMerchant(): ?Merchant
        {
                return $this->merchant;
        }

        public function setMerchant(?Merchant $merchant): self
        {
                $this->merchant = $merchant;

                return $this;
        }
}
