<?php

namespace WapplerSystems\BookmarksLikesRatings\Controller;

use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Utility\DebugUtility;
use TYPO3\CMS\Extbase\Mvc\View\JsonView;
use WapplerSystems\BookmarksLikesRatings\Domain\Model\Bookmark;
use WapplerSystems\BookmarksLikesRatings\Domain\Model\Bookmarks;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * Plugin controller
 */
class BookmarkController extends AbstractController
{

    public function initializeStatusAction()
    {
        $this->defaultViewObjectName = JsonView::class;
    }

    /**
     * @param int $objectUid
     * @param string $tablename
     * @throws \TYPO3\CMS\Frontend\Exception
     */
    public function statusAction(int $objectUid,string $tablename) {

        $user = $this->getCurrentUser();

        $this->view->setVariablesToRender(['status']);
        $this->view->assignMultiple([
            'status' => ['status' => $this->bookmarkRepository->countByUserTablenameObjectUid($user['uid'],$tablename,$objectUid) === 0? 'false':'true']
        ]);

    }


    public function initializeToggleAction()
    {
        $this->defaultViewObjectName = JsonView::class;
    }

    /**
     * @param int $objectUid
     * @param string $tablename
     * @throws \TYPO3\CMS\Frontend\Exception
     */
    public function toggleAction(int $objectUid, string $tablename)
    {

        $user = $this->getCurrentUser();

        $this->view->setVariablesToRender(['status']);

        if ($this->bookmarkRepository->countByUserTablenameObjectUid($user['uid'], $tablename, $objectUid) === 0) {

            $bookmark = new Bookmark();
            $bookmark->setUser($user['uid']);
            $bookmark->setTablename($tablename);
            $bookmark->setObjectUid($objectUid);

            $this->bookmarkRepository->add($bookmark);

            $this->view->assignMultiple([
                'status' => ['status' => 'true']
            ]);
        } else {
            $this->bookmarkRepository->removeByUserTablenameObjectUid($user['uid'], $tablename, $objectUid);

            $this->view->assignMultiple([
                'status' => ['status' => 'false']
            ]);
        }

    }


    /**
     * @param int $objectUid
     * @param string $tablename
     * @throws \TYPO3\CMS\Frontend\Exception
     */
    public function deleteAction(int $objectUid, string $tablename)
    {

        $user = $this->getCurrentUser();
        $this->bookmarkRepository->removeByUserTablenameObjectUid($user['uid'], $tablename, $objectUid);

        $this->redirectToUri($this->uriBuilder->reset()->buildFrontendUri());

    }


    /**
     */
    public function personalListAction()
    {

        $user = $this->getCurrentUser();

        $bookmarks = [];

        $objs = $this->bookmarkRepository->findByUser($user['uid']);

        /** @var Bookmark $obj */
        foreach ($objs as $obj) {
            $bookmark = [];
            $bookmark['tablename'] = $obj->getTablename();
            $bookmark['object_uid'] = $obj->getObjectUid();

            if ($obj->getTablename() === 'pages') {

                $page = BackendUtility::getRecord('pages',$obj->getObjectUid(),'title');
                if ($page) {

                    $bookmark['title'] = $page['title'];
                    $bookmark['url'] = $this->uriBuilder->reset()->setTargetPageUid($obj->getObjectUid())->buildFrontendUri();
                }

                $bookmarks[] = $bookmark;
            }


        }

        $this->view->assignMultiple([
            'bookmarks' => $bookmarks
        ]);
    }


}