<?php

namespace Diamond\Modules\AdminArchivePages;

use ActiveTable\ColumnTable;
use ActiveTable\DataTableEngine;
use Core\Form\Control\MenuItemAction;
use Core\Form\Control\MenuItemEdit;
use Core\Form\Control\Switchery;
use Core\ModuleController;
use Diamond\Domain\Page\Contracts\PageEntityInterface;
use Diamond\Helpers\Form;
use Diamond\Helpers\Formatter;
use Core\Form\ControlBuilder;
use Diamond\Repositories\Page\PageArchiveRepository;
use Diamond\Repositories\Page\PageCriteria;
use Diamond\Repositories\Template;
use Diamond\Config\Main;
use DiamondTable\CommandFactory;
use DiamondTable\TableFactory;

/**
 * Description of AdminUsers
 *
 * @author Dmitriy
 */
class AdminArchivePages extends ModuleController
{

    public function __construct(
        private Main                        $mainConfig,
        private Template\TemplateRepository $templateRepo,
        private PageArchiveRepository       $pageRepo,
        private TableFactory                $tableFactory,
        private ControlBuilder              $controlBuilder)
    {
    }


    public function process(): string
    {

        $table = $this->tableFactory->buildAdminTable($this->pageRepo, PageCriteria::create());
        $table->removeControlAccess(DataTableEngine::CONTROL_ACCESS_ADD);

        $this->templates = $this->templateRepo->findByCriteria(Template\TemplateCriteria::create());

        if (!$this->templates->count()) {
            return Form::buildAnnouncementMessage('Редактирование страниц невозможно, необходимо создать шаблоны сайта');
        }

        $table
            ->addColumn(
                (new ColumnTable('id', '№'))->setWidth(40)
            )
            ->addColumn(
                (new ColumnTable('code', 'Идентификатор'))
            )
            ->addColumn(
                (new ColumnTable('title', 'Заголовок страницы'))
            )
            ->addColumn(
                (new ColumnTable('url', 'Адрес страницы'))
                    ->setFormat($this, 'formatUrl')

            )
            ->addColumn(
                (new ColumnTable('script', 'Twig страница'))
            )
            ->addColumn(
                (new ColumnTable('template', 'Шаблон'))
                    ->setFormat($this, 'formatTemplate')

            )
            ->addColumn(
                (new ColumnTable('content', 'Содержимое страницы'))
                    ->setFormat($this, 'formatContent')

            )
            ->addColumn(
                (new ColumnTable('active', 'Актив.'))
                    ->setFormat($this, 'formatActive')

            )
            ->addColumn(
                (new ColumnTable('system', 'Систем.'))
                    ->setFormat($this, 'formatSystem')

            )
            ->addColumn(
                (new ColumnTable('actions', ''))
                    ->setFormat($this, 'formatActions')
                    ->setWidth(120)
            )
            ->addFilter(
                $this->controlBuilder->buildInput('search_by_code')
            )
            ->addFilter(
                $this->controlBuilder->buildInput('search_by_title')
            )
            ->addFilter(
                $this->controlBuilder->buildInput('search_by_url')
            )
            ->addFilter(
                $this->controlBuilder->buildInput('search_by_script')
            )
            ->addFilter(
                $this->controlBuilder->buildDropdownYNUniform('filter_by_active', [
                    null => 'любой'
                ])
            )
            ->addFilter(
                $this->controlBuilder->buildDropdownYNUniform('filter_by_system', [
                    null => 'любой'
                ])
            )
            ->addFilter(
                $this->controlBuilder->buildDropdownUniform('filter_by_template', Form::prepareDropdown($this->templates))
            );


        $this->tableFactory->prepareAdminTableForDomain($table);

        $table
            ->addField(
                $this->controlBuilder->buildInput('title'), false, null, 'будет использоваться в title'
            )
            ->addField(
                $this->controlBuilder->buildInput('code'), false, null, 'техническое имя'
            )
            ->addField(
                $this->controlBuilder->buildInput('url'), false, null,
                $this->mainConfig->getUrl_suffix() ?
                    'Суффикс ' . $this->mainConfig->getUrl_suffix() . ' будет атоматически добавлен' : 'начинается с /'
            )
            ->addField(
                $this->controlBuilder->buildInput('script'), false, null, 'twig шаблон из папки pages'
            )
            ->addField(
                $this->controlBuilder->buildDropdownUniform('template', array_merge([
                    null => 'без шаблона'
                ], $this->templates->getDropdownList()))
            )
            ->addField(
                $this->controlBuilder
                    ->buildTextEditor('content')
                    ->setMini(true)
                    ->setHeight(300)
                    ->setWidthPercent(100)

            )
            ->addField(
                $this->controlBuilder->buildDropdownYNUniform('active')->setValue('Y'), true
            )
            ->addField(
                $this->controlBuilder->buildDropdownYNUniform('system'), true
            )
            ->addField(
                $this->controlBuilder->buildFormHeader(
                    'Мета теги', 'meta'
                )
            )
            ->addField(
                $this->controlBuilder->buildInput('mttitle', 2), false, 'Заголовок'
            )
            ->addField(
                $this->controlBuilder->buildTextArea('mtdescription', 2), false, 'Описание'
            )
            ->addField(
                $this->controlBuilder->buildTextArea('mtkeywords', 2), false, 'Ключевые слова', 'через запятую'
            )
            ->addRowActionCommand('restore-pages', new PageRestoreCommand($table), 'Восстановить');

        return $table->render();
    }


    public function formatTemplate(PageEntityInterface $page): string
    {
        $ar = $this->templates->getDropdownList();
        return $ar[$page->getTemplate()] ?? $page->getTemplate();
    }


    public function formatActions(PageEntityInterface $page): string
    {
        $items = [];

        $items [] = new MenuItemEdit($page->getId());

        $items [] = new MenuItemAction($page->getId(), 'restore-pages', 'Восстановить', 'Восстановить запись?', 'demo-pli-plus');

        if ($page->getSystem() === 'N') {
            $items [] = new MenuItemAction($page->getId(), CommandFactory::ROWS_DELETE, 'Удалить', 'Действительно удалить страницу?', 'demo-pli-remove');
        }

        return $this->controlBuilder->buildActionMenu($items)->render();
    }

    public function formatUrl(PageEntityInterface $page): string
    {
        return !$page->getUrl() ? '' : anchor($page->getUrl(), $page->getUrl(), [
            'target' => '_blank',
            'title' => 'Открыть страницу в отдельной вкладке'
        ]);
    }


    public function formatContent(PageEntityInterface $page): string
    {
        return Formatter::formatLongText($page->getContent());
    }


    public function formatSystem(PageEntityInterface $page): string
    {
        $data = [
            'class' => self::class,
            'id' => $page->getId(),
            'fn' => 'changeSystem'
        ];
        return (new Switchery($data, $page->getSystem() === 'Y'))->render();
    }


    public function formatActive(PageEntityInterface $page): string
    {
        $data = [
            'class' => self::class,
            'id' => $page->getId(),
            'fn' => 'changeActive'
        ];
        return (new Switchery($data, $page->getActive() === 'Y'))->render();
    }

    public function changeActive(int $id, bool $checked): void
    {
        $page = $this->pageRepo->findById($id);

        $page->setActive($checked === true ? 'Y' : 'N');
        $this->pageRepo->save($page);
    }


    public function changeSystem(int $id, bool $checked): void
    {
        $page = $this->pageRepo->findById($id);

        $page->setSystem($checked === true ? 'Y' : 'N');
        $this->pageRepo->save($page);
    }
}
