<?php

namespace Src\Modules\JsonObjects\Dto\Dir;

use Src\Modules\JsonObjects\Interfaces\Dto\Dir\IPersist;
use Src\Modules\JsonObjects\Interfaces\Dto\IFactory as IDtoFactory;
use Src\Modules\JsonObjects\Interfaces\Dto\Dir\IAbstractCategory;

/**
 * @property IPersist $parent
 */
class Persist implements IPersist
{

    use CategoryTrait;

    /**
     * @var IDtoFactory
     */
    protected $dtoFactory;

    protected array $updatedAttrs = [];

    public function init(): void
    {
        $this->id = uniqid();
    }

    public function setDtoFactory(IDtoFactory $factory)
    {
        $this->dtoFactory = $factory;
    }

    public function loadPath(array $data)
    {
        $this->path = [];
        foreach ($data as $itemData) {
            $pathItem = $this->dtoFactory->getDirFactory()->createPersist();
            $pathItem->load($itemData);
            $this->path[] = $pathItem;
        }
    }

    public function getPath(): array
    {
        return $this->path;
    }

    public function loadParent(array $data)
    {
        $this->parent = $this->dtoFactory->getDirFactory()->createPersist();
        $this->parent->load($data);
    }

    public function update(array $data): void
    {
        if (isset($data['name']) && $data['name'] != $this->name) {
            $this->name = $data['name'];
            $this->updatedAttrs['name'] = $data['name'];
        }
    }

    public function getInsertAttributes(): array
    {
        $parentId = ($this->parent) ? $this->parent->getId() : '';
        $attrs = [
            'id' => $this->id,
            'parent_id' => $parentId,
            'name' => $this->name,
        ];
        $parentPath = ($this->parent) ? $this->parent->getPath() : [];
        $pathIds = array_map(function (IAbstractCategory $item) {
            return $item->getId();
        }, $parentPath);
        $pathIds[] = $parentId;
        $attrs['matherial_path'] = join('|', $pathIds);
        return $attrs;
    }

    public function getUpdatedAttrs(): array
    {
        return $this->updatedAttrs;
    }
}
