<?php

namespace KupShop\ContentBundle\Entity;

use Doctrine\Common\Collections\Collection;
use KupShop\ContentBundle\Util\BlockCollection;

class Page
{
    private $id;

    private $block;

    private $childBlocksHierarchy;

    private $template = '';

    private int $type;

    private ?int $parentId = null;

    private $name = '';

    private string $nameShort = '';

    private ?string $link = null;

    private string $target = '';

    private $label = '';

    private $erasable = 'Y';

    private $selectable = 'Y';

    private $meta_title;

    private $meta_description;

    private $meta_keywords;

    private $old_id_page = '';

    private $figure = 'Y';

    private $show_in_search = 'Y';

    private $url;

    private $data;
    private array $photos = [];
    private array $children = [];

    public function getId()
    {
        return $this->id;
    }

    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    public function addPhoto(array $photo)
    {
        $this->photos[] = $photo;
    }

    public function getPhotos()
    {
        return $this->photos;
    }

    public function setTemplate($template)
    {
        $this->template = $template;

        return $this;
    }

    public function getTemplate()
    {
        return $this->template;
    }

    public function setType(int $type): self
    {
        $this->type = $type;

        return $this;
    }

    public function getType(): int
    {
        return $this->type;
    }

    public function setParentId(?int $parentId): self
    {
        $this->parentId = $parentId;

        return $this;
    }

    public function getParentId(): ?int
    {
        return $this->parentId;
    }

    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setNameShort(string $nameShort): self
    {
        $this->nameShort = $nameShort;

        return $this;
    }

    public function getNameShort(): string
    {
        return $this->nameShort;
    }

    public function setLink(?string $link): self
    {
        $this->link = $link;

        return $this;
    }

    public function getLink(): ?string
    {
        return $this->link;
    }

    public function setTarget(string $target): self
    {
        $this->target = $target;

        return $this;
    }

    public function getTarget(): ?string
    {
        return $this->target;
    }

    public function setLabel($label)
    {
        $this->label = $label;

        return $this;
    }

    public function getLabel()
    {
        return $this->label;
    }

    public function setOldIdPage($old_id_page)
    {
        $this->old_id_page = $old_id_page;

        return $this;
    }

    public function getOldIdPage()
    {
        return $this->old_id_page;
    }

    public function setErasable($erasable)
    {
        $this->erasable = $erasable;

        return $this;
    }

    public function getErasable()
    {
        return $this->erasable;
    }

    public function setSelectable($selectable)
    {
        $this->selectable = $selectable;

        return $this;
    }

    public function getSelectable()
    {
        return $this->selectable;
    }

    public function getChildBlocks(): Collection
    {
        if ($this->getBlock()) {
            return $this->getBlock()->getBlocks();
        }
        if (!getAdminUser()) {
            return new BlockCollection();
        }

        return new BlockCollection([new Block()]);
    }

    public function getMetaTitle()
    {
        if ($this->meta_title) {
            return $this->meta_title;
        }

        return $this->name;
    }

    public function setMetaTitle(string $meta_title)
    {
        $this->meta_title = $meta_title;

        return $this;
    }

    public function getMetaDescription()
    {
        if ($this->meta_description) {
            return $this->meta_description;
        }

        $dbcfg = \Settings::getDefault();

        return $this->name.' - '.$dbcfg->shop_description;
    }

    public function setMetaDescription(string $meta_description)
    {
        $this->meta_description = $meta_description;

        return $this;
    }

    public function getMetaKeywords()
    {
        if ($this->meta_keywords) {
            return $this->meta_keywords;
        }

        $dbcfg = \Settings::getDefault();

        return $dbcfg->shop_keywords;
    }

    public function setMetaKeywords(string $meta_keywords)
    {
        $this->meta_keywords = $meta_keywords;

        return $this;
    }

    public function setBlock(?Block $block = null)
    {
        $this->block = $block;

        return $this;
    }

    public function getBlock()
    {
        return $this->block;
    }

    /** Get blocks hierarchy
     * returns only root blocks with cached children.
     */
    public function getChildBlocksHierarchy(): BlockCollection
    {
        if (!$this->childBlocksHierarchy) {
            $this->childBlocksHierarchy = Block::buildBlocksHierarchy($this->getChildBlocks());
        }

        return $this->childBlocksHierarchy;
    }

    public function setChildBlocksHierarchy($childBlocksHierarchy)
    {
        $this->childBlocksHierarchy = $childBlocksHierarchy;

        return $this->childBlocksHierarchy;
    }

    public function getFigure(): string
    {
        return $this->figure;
    }

    public function setFigure(string $figure): Page
    {
        $this->figure = $figure;

        return $this;
    }

    public function getShowInSearch(): string
    {
        return $this->show_in_search;
    }

    public function setShowInSearch(string $show_in_search): Page
    {
        $this->show_in_search = $show_in_search;

        return $this;
    }

    public function getUrl(): ?string
    {
        return $this->url;
    }

    public function setUrl(?string $url)
    {
        $this->url = $url;

        return $this;
    }

    /**
     * @return array
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * @param array $data
     *
     * @return Page
     */
    public function setData($data)
    {
        $this->data = $data;

        return $this;
    }

    public function setChildren(array $children): self
    {
        $this->children = $children;

        return $this;
    }

    public function getChildren(): array
    {
        return $this->children;
    }

    public function appendChild(Page $child): void
    {
        $this->children[$child->getId()] = $child;
    }
}
