<?php

namespace Src\Modules\JsonObjects\Pages;

use Src\Common\Pages\Page;
use Src\Modules\JsonObjects\Interfaces\Pages\IItem;
use Src\Modules\JsonObjects\Interfaces\Infrastructure\IItemStorage;
use Src\Modules\JsonObjects\Interfaces\Dto\Item\IFactory as IItemDtoFactory;
use Src\Modules\JsonObjects\Interfaces\Dto\Item\IResourceItem;

class Item extends Page implements IItem
{

    protected IItemStorage $itemStorage;

    protected IItemDtoFactory $itemDtoFactory;

    protected IResourceItem $item;

    public function setItemStorage(IItemStorage $storage)
    {
        $this->itemStorage = $storage;
    }

    public function setItemDtoFactory(IItemDtoFactory $factory)
    {
        $this->itemDtoFactory = $factory;
    }

    public function init(string $itemId)
    {
        $this->item = $this->itemDtoFactory->createResource();
        $dsl = [
            '*',
            'dir' => ['*', 'path' => ['*'], 'parent' => ['*']],
        ];
        $itemData = $this->itemStorage->getById($itemId, $dsl);
        $this->item->load($itemData);
    }

    public function getItem(): IResourceItem
    {
        return $this->item;
    }

    public function getCssStack(): array
    {
        return [
            '<link rel="stylesheet" href="/admin-css/json-objects-item.css?' . self::CSS_VERSION . '">',
        ];
    }

    public function getJsStack(): array
    {
        return [
            '<script src="/admin-js/json-objects-item.js?' . self::JS_VERSION . '"></script>',
        ];
    }

    public function getJsSettings(): array
    {
        return [
            'fileInputSettings' => [
                'getDirUrl' => route('admin.common.filesBrowser.dir', ['dir' => 'files-browser'], false),
                'deleteFileUrl' => route('admin.common.filesBrowser.deleteFile', ['dir' => 'files-browser'], false),
                'renameFileUrl' => route('admin.common.filesBrowser.renameFile', ['dir' => 'files-browser'], false),
                'createDirUrl' => route('admin.common.filesBrowser.createDir', ['dir' => 'files-browser'], false),
                'uploadFileUrl' => route('admin.common.filesBrowser.uploadFile', ['dir' => 'files-browser'], false),
            ],
            'filesBrowser' => [
                'upload' => route('admin.common.filesBrowser.upload'),
                'create' => route('admin.common.filesBrowser.create'),
                'move' => route('admin.common.filesBrowser.move'),
                'remove' => route('admin.common.filesBrowser.remove'),
                'items' => route('admin.common.filesBrowser.items'),
                'tree' => route('admin.common.filesBrowser.tree'),
                'folder' => route('admin.common.filesBrowser.folder'),
                'ajax' => route('admin.common.filesBrowser.ajax'),
            ],
            // 'filesBrowser' => [
            //     'upload' => route('admin.jsonObjects.filesBrowser.upload'),
            //     'create' => route('admin.jsonObjects.filesBrowser.create'),
            //     'move' => route('admin.jsonObjects.filesBrowser.move'),
            //     'remove' => route('admin.jsonObjects.filesBrowser.remove'),
            //     'items' => route('admin.jsonObjects.filesBrowser.items'),
            //     'tree' => route('admin.jsonObjects.filesBrowser.tree'),
            //     'folder' => route('admin.jsonObjects.filesBrowser.folder'),
            //     'ajax' => route('admin.jsonObjects.filesBrowser.ajax'),
            // ],
            'item' => $this->item->toArray(),
            'editObjUrl' => route('admin.jsonObjects.editItem', [], false),
            'successUrl' => route('admin.jsonObjects.dir', ['dir-id' => $this->item->getDir()->getId()], false),
        ];
    }

    public function getTitle(): string
    {
        return '';
    }

    public function getBreadcrumbs(): array
    {
        $bc = [
            [
                'title' => 'jsonObjects',
                'href' => route('admin.jsonObjects.dir', [], false),
            ],
        ];
        foreach ($this->item->getDir()->getPath() as $pathItem) {
            $bc[] = [
                'title' => $pathItem->getName(),
                'href' => route('admin.jsonObjects.dir', ['dir-id' => $pathItem->getId()], false),
            ];
        }
        if ($this->item->getDir()->getParent()) {
            $bc[] = [
                'title' => $this->item->getDir()->getParent()->getName(),
                'href' => route('admin.jsonObjects.dir', ['dir-id' => $this->item->getDir()->getParent()->getId()], false),
            ];
        }
        $bc[] = [
            'title' => $this->item->getDir()->getName(),
            'href' => route('admin.jsonObjects.dir', ['dir-id' => $this->item->getDir()->getId()], false),
        ];
        $bc[] = [
            'title' => $this->item->getName(),
            'href' => route('admin.jsonObjects.editItem', ['item-id' => $this->item->getId()], false),
        ];
        return $bc;
    }
}
