<?php

namespace AutoAdmin\Bundle\Tests\Functional;

use AutoAdmin\Bundle\Tests\Functional\Entity\Page;
use AutoAdmin\Bundle\Tests\Functional\Model\RegexModel;
use DateTime;

class BrickDuplicationTest extends AbstractDuplicationTest
{
    private const SIMPLE_ENTITY_CLASS = 'AutoAdmin\\Bundle\\Tests\\Functional\\Entity\\SimpleEntity';
    private const FILE_ENTITY_CLASS = 'AutoAdmin\\Bundle\\Tests\\Functional\\Entity\\File';

    private ?Page $pageBrick = null;
    private ?Page $pageBrickScalarCollection = null;
    private ?Page $pageBrickEntityDefault = null;
    private ?Page $pageBrickEntityKeep = null;
    private ?Page $pageBrickEntityDuplicate = null;
    private ?Page $pageBrickFile = null;
    private ?Page $pageBrickEntityCollection = null;

    public static function provideEntitiesForDuplication(): array
    {
        $helper = new self();
        $helper->loadEntities();

        return [
//            'brick datetime setting' => [], TODO
            'brick entity default setting' => [
                'originalEntity' => $helper->pageBrickEntityDefault,
                'expectedProperties' => $helper->baseExpectations([
                    'translations' => [
                        $helper->expectedTranslation($helper->localeFR, [
                            'brick' => $helper->expectedSingleItemBrick($helper->brickItem('brick.entity_default', 'entity-default-fr', ['entity' => null])),
                        ]),
                        $helper->expectedTranslation($helper->localeEN, [
                            'brick' => $helper->expectedSingleItemBrick($helper->brickItem('brick.entity_default', 'entity-default-en', ['entity' => null])),
                        ]),
                    ],
                ]),
            ],
            'brick entity keep setting' => [
                'originalEntity' => $helper->pageBrickEntityKeep,
                'expectedProperties' => $helper->baseExpectations([
                    'translations' => [
                        $helper->expectedTranslation($helper->localeFR, [
                            'brick' => $helper->expectedSingleItemBrick($helper->brickItem('brick.entity_keep', 'entity-keep-fr', ['entity' => self::SIMPLE_ENTITY_CLASS . '#123'])),
                        ]),
                        $helper->expectedTranslation($helper->localeEN, [
                            'brick' => $helper->expectedSingleItemBrick($helper->brickItem('brick.entity_keep', 'entity-keep-en', ['entity' => self::SIMPLE_ENTITY_CLASS . '#456'])),
                        ]),
                    ],
                ]),
            ],
            'brick entity duplicate setting' => [
                'originalEntity' => $helper->pageBrickEntityDuplicate,
                'expectedProperties' => $helper->baseExpectations([
                    'translations' => [
                        $helper->expectedTranslation($helper->localeFR, [
                            'brick' => $helper->expectedSingleItemBrick($helper->brickItem('brick.entity_duplicate', 'entity-duplicate-fr', [
                                'entity' => ['id' => null, 'createdAt' => new DateTime(), 'updatedAt' => new DateTime()],
                            ])),
                        ]),
                        $helper->expectedTranslation($helper->localeEN, [
                            'brick' => $helper->expectedSingleItemBrick($helper->brickItem('brick.entity_duplicate', 'entity-duplicate-en', [
                                'entity' => ['id' => null, 'createdAt' => new DateTime(), 'updatedAt' => new DateTime()],
                            ])),
                        ]),
                    ],
                ]),
            ],
            'brick file setting' => [
                'originalEntity' => $helper->pageBrickFile,
                'expectedProperties' => $helper->baseExpectations([
                    'translations' => [
                        $helper->expectedTranslation($helper->localeFR, [
                            'brick' => $helper->expectedSingleItemBrick($helper->brickItem('brick.file', 'file-fr', [
                                'file' => $helper->expectedDuplicatedFile(),
                            ])),
                        ]),
                        $helper->expectedTranslation($helper->localeEN, [
                            'brick' => $helper->expectedSingleItemBrick($helper->brickItem('brick.file', 'file-en', [
                                'file' => $helper->expectedDuplicatedFile(),
                            ])),
                        ]),
                    ],
                ]),
            ],
            'brick entity collection setting' => [
                'originalEntity' => $helper->pageBrickEntityCollection,
                'expectedProperties' => $helper->baseExpectations([
                    'translations' => [
                        $helper->expectedTranslation($helper->localeFR, [
                            'brick' => $helper->expectedSingleItemBrick($helper->brickItem('brick.entity_collection', 'entity-collection-fr')),
                            // 'entities' => [], // Not implemented
                        ]),
                        $helper->expectedTranslation($helper->localeEN, [
                            'brick' => $helper->expectedSingleItemBrick($helper->brickItem('brick.entity_collection', 'entity-collection-en')),
                            // 'entities' => [], // Not implemented
                        ]),
                    ],
                ]),
            ],
//            'brick sub entity collection setting' => [], TODO
            'brick scalar setting' => [
                'originalEntity' => $helper->pageBrick,
                'expectedProperties' => $helper->baseExpectations([
                    'translations' => [
                        $helper->expectedTranslation($helper->localeFR, [
                            'brick' => $helper->expectedSingleItemBrick($helper->brickItem('brick.text', 'text-fr', ['content' => 'Test FR'])),
                        ]),
                        $helper->expectedTranslation($helper->localeEN, [
                            'brick' => $helper->expectedSingleItemBrick($helper->brickItem('brick.text', 'text-en', ['content' => 'Test EN'])),
                        ]),
                    ],
                ]),
            ],
            'brick scalar collection setting' => [
                'originalEntity' => $helper->pageBrickScalarCollection,
                'expectedProperties' => $helper->baseExpectations([
                    'translations' => [
                        $helper->expectedTranslation($helper->localeFR, [
                            'brick' => $helper->expectedSingleItemBrick($helper->brickItem('brick.scalar_collection', 'scalar-collection-fr', [
                                'items' => [
                                    ['title' => 'Item 1 FR', 'description' => 'Item 1 FR description'],
                                    ['title' => 'Item 2 FR', 'description' => 'Item 2 FR description'],
                                ],
                            ])),
                        ]),
                        $helper->expectedTranslation($helper->localeEN, [
                            'brick' => $helper->expectedSingleItemBrick($helper->brickItem('brick.scalar_collection', 'scalar-collection-en', [
                                'items' => [
                                    ['title' => 'Item 1 EN', 'description' => 'Item 1 EN description'],
                                    ['title' => 'Item 2 EN', 'description' => 'Item 2 EN description'],
                                ],
                            ])),
                        ]),
                    ],
                ]),
            ],
        ];
    }

    protected function loadEntities(): void
    {
        parent::loadEntities();

        $this->pageBrick = $this->createBrickPage(
            $this->brickItem('brick.text', 'text-fr', ['content' => 'Test FR']),
            $this->brickItem('brick.text', 'text-en', ['content' => 'Test EN']),
        );

        $this->pageBrickScalarCollection = $this->createBrickPage(
            $this->brickItem('brick.scalar_collection', 'scalar-collection-fr', [
                'items' => [
                    ['title' => 'Item 1 FR', 'description' => 'Item 1 FR description'],
                    ['title' => 'Item 2 FR', 'description' => 'Item 2 FR description'],
                ],
            ]),
            $this->brickItem('brick.scalar_collection', 'scalar-collection-en', [
                'items' => [
                    ['title' => 'Item 1 EN', 'description' => 'Item 1 EN description'],
                    ['title' => 'Item 2 EN', 'description' => 'Item 2 EN description'],
                ],
            ]),
        );

        $this->pageBrickEntityDefault = $this->createBrickPage(
            $this->brickItem('brick.entity_default', 'entity-default-fr', ['entity' => self::SIMPLE_ENTITY_CLASS . '#123']),
            $this->brickItem('brick.entity_default', 'entity-default-en', ['entity' => self::SIMPLE_ENTITY_CLASS . '#456']),
        );

        $this->pageBrickEntityKeep = $this->createBrickPage(
            $this->brickItem('brick.entity_keep', 'entity-keep-fr', ['entity' => self::SIMPLE_ENTITY_CLASS . '#123']),
            $this->brickItem('brick.entity_keep', 'entity-keep-en', ['entity' => self::SIMPLE_ENTITY_CLASS . '#456']),
        );

        $this->pageBrickEntityDuplicate = $this->createBrickPage(
            $this->brickItem('brick.entity_duplicate', 'entity-duplicate-fr', ['entity' => self::SIMPLE_ENTITY_CLASS . '#123']),
            $this->brickItem('brick.entity_duplicate', 'entity-duplicate-en', ['entity' => self::SIMPLE_ENTITY_CLASS . '#456']),
        );

        $this->pageBrickFile = $this->createBrickPage(
            $this->brickItem('brick.file', 'file-fr', ['file' => self::FILE_ENTITY_CLASS . '#123']),
            $this->brickItem('brick.file', 'file-en', ['file' => self::FILE_ENTITY_CLASS . '#456']),
        );

        $this->pageBrickEntityCollection = $this->createBrickPage(
            $this->brickItem('brick.entity_collection', 'entity-collection-fr', [
                'entities' => [
                    self::SIMPLE_ENTITY_CLASS . '#123',
                    self::SIMPLE_ENTITY_CLASS . '#456',
                    self::SIMPLE_ENTITY_CLASS . '#789',
                ],
            ]),
            $this->brickItem('brick.entity_collection', 'entity-collection-en', [
                'entities' => [
                    self::SIMPLE_ENTITY_CLASS . '#159',
                    self::SIMPLE_ENTITY_CLASS . '#357',
                    self::SIMPLE_ENTITY_CLASS . '#486',
                ],
            ]),
        );
    }

    private function brickSettings(string $cssClass, array $extra = []): array
    {
        return array_merge(['center' => true, 'css_classes' => $cssClass, 'wrapper' => true], $extra);
    }

    private function brickItem(string $type, string $cssClass, array $extra = []): array
    {
        return ['type' => $type, 'settings' => $this->brickSettings($cssClass, $extra)];
    }

    private function expectedDuplicatedFile(): array
    {
        return $this->baseExpectations([
            'fileName' => new RegexModel(sprintf('/^[a-f0-9]{8}-%s$/', self::MOCKED_FILE_NAME)),
            'originalName' => new RegexModel(sprintf('/^[a-f0-9]{8}-%s$/', self::MOCKED_FILE_NAME)),
        ]);
    }
}
