<?php

use KupShop\AdminBundle\AdminRegister\AdminRegisterLocator;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;

class AdminBarMenu
{
    protected $customMenuItems = [];

    protected $menu = [
        [
            'name' => 'administration',
            'submenu' => [
                [
                    'name' => 'leadpage',
                    'left' => 'blank', 'right' => 's=board.php',
                ],
                [
                    'name' => 'shopStore',
                    'left' => 's=menu.php&type=shopStore', 'right' => 's=board.php&type=shopStore',
                ],
                [
                    'name' => 'newWindow',
                    'script' => 'newWindow();',
                ],
                [
                    'name' => 'logout',
                    'script' => 'logout(0)',
                ],
                [
                    'name' => 'adminEdit',
                    'script' => "nw('adminEdit')",
                ],
                [
                    'name' => 'htmlComponents',
                    /* 'left' => 's=menu.php&type=htmlComponents', */ 'right' => 's=board.php&type=htmlComponents',
                ],
                [
                    'name' => 'chooseLanguage',
                    'submenu' => [
                        [
                            'name' => 'czech',
                            'href' => 'admin-change-language/czech',
                        ],
                        [
                            'name' => 'english',
                            'href' => 'admin-change-language/english',
                        ],
                    ],
                ],
            ],
        ],
        [
            'name' => 'productsMenu',
            'left' => 's=menu.php&type=products', 'right' => 's=list.php&type=products',
            'submenu' => [
                [
                    'name' => 'products',
                    'left' => 's=menu.php&type=products', 'right' => 's=list.php&type=products',
                ],
                [
                    'name' => 'productsMassModification',
                    'left' => 'blank', 'right' => 's=list.php&type=productsMassModification',
                ],
                [
                    'name' => 'parameters',
                    'left' => 's=menu.php&type=parameters', 'right' => 's=list.php&type=parameters',
                ],
                [
                    'name' => 'productsVarLabels',
                    'left' => 's=menu.php&type=productsVarLabels', 'right' => 's=list.php&type=productsVarLabels',
                ],
                [
                    'name' => 'sections',
                    'left' => 's=menu.php&type=sections', 'right' => 's=list.php&type=sections',
                ],
                [
                    'name' => 'producers',
                    'left' => 's=menu.php&type=producers', 'right' => 's=list.php&type=producers',
                ],
                [
                    'name' => 'templatesMenu',
                    'submenu' => [
                        [
                            'name' => 'templates',
                            'left' => 's=menu.php&type=templates', 'right' => 's=list.php&type=templates',
                        ],
                        [
                            'name' => 'templatesCategories',
                            'left' => 's=menu.php&type=templatesCategories', 'right' => 's=list.php&type=templatesCategories',
                        ],
                    ],
                ],
                [
                    'name' => 'reviews',
                    'left' => 's=menu.php&type=reviews', 'right' => 's=list.php&type=reviews&type_list=ShowNotConfirmed',
                ],
                [
                    'name' => 'productsRelatedTypes',
                    'left' => 's=menu.php&type=productsRelatedTypes', 'right' => 's=list.php&type=productsRelatedTypes',
                ],
            ],
        ],
        [
            'name' => 'ordersMenu',
            'left' => 's=menu.php&type=orders', 'right' => 's=list.php&type=orders',
            'submenu' => [
                [
                    'name' => 'orders',
                    'left' => 's=menu.php&type=orders', 'right' => 's=list.php&type=orders',
                ],
                [
                    'name' => 'users',
                    'left' => 's=menu.php&type=users', 'right' => 's=list.php&type=users',
                ],
                [
                    'name' => 'usersGroups',
                    'left' => 's=menu.php&type=usersGroups', 'right' => 's=list.php&type=usersGroups',
                ],

                [
                    'name' => 'ordersMassProcess',
                    'left' => 's=menu.php&type=ordersMassProcess', 'right' => 's=list.php&type=ordersMassProcess',
                ],

                [
                    'name' => 'old_pos',
                    'script' => "nw('old_pos')",
                ],

                [
                    'name' => 'orderPayment',
                    'left' => 's=menu.php&type=orderPayment', 'right' => null,
                ],
            ],
        ],
        [
            'name' => 'stockMenu',
            'submenu' => [
                [
                    'name' => 'stockIn',
                    'left' => 's=menu.php&type=stockIn', 'right' => 's=list.php&type=stockIn',
                ],
                [
                    'name' => 'suppliers',
                    'left' => 's=menu.php&type=suppliers', 'right' => 's=list.php&type=suppliers',
                ],
                [
                    'name' => 'stockInMissing',
                    'left' => 's=menu.php&type=stockInMissing', 'right' => 's=list.php&type=stockInMissing&fromNav=1',
                ],
                [
                    'name' => 'productsOfSuppliers',
                    'left' => 's=menu.php&type=productsOfSuppliers', 'right' => 's=list.php&type=productsOfSuppliers',
                ],
                [
                    'name' => 'ordersOfSuppliers',
                    'left' => 's=menu.php&type=ordersOfSuppliers', 'right' => 's=list.php&type=ordersOfSuppliers',
                ],

                [
                    'name' => 'inventory',
                    'left' => 's=menu.php&type=inventory', 'right' => 's=list.php&type=inventory',
                ],

                [
                    'name' => 'productsPrices',
                    'left' => 's=menu.php&type=productsPrices', 'right' => 's=list.php&type=productsPrices',
                ],
            ],
        ],
        [
            'name' => 'contentMenu',
            'submenu' => [
                [
                    'name' => 'menu',
                    'left' => 's=menu.php&type=menu', 'right' => 's=list.php&type=menu',
                ],
                [
                    'name' => 'sliders',
                    'left' => 's=menu.php&type=sliders', 'right' => 's=list.php&type=sliders',
                ],
                [
                    'name' => 'articlesMenu',
                    'submenu' => [
                        [
                            'name' => 'articles',
                            'left' => 's=menu.php&type=articles', 'right' => 's=list.php&type=articles',
                        ],
                        [
                            'name' => 'artsections',
                            'left' => 's=menu.php&type=artsections', 'right' => 's=list.php&type=artsections',
                        ],
                        [
                            'name' => 'artauthors',
                            'left' => 's=menu.php&type=artauthors', 'right' => 's=list.php&type=artauthors',
                        ],
                        [
                            'name' => 'articlesTags',
                            'left' => 's=menu.php&type=articlesTags', 'right' => 's=list.php&type=articlesTags',
                        ],
                    ],
                ],
                [
                    'name' => 'sellers',
                    'left' => 's=menu.php&type=sellers', 'right' => 's=list.php&type=sellers',
                ],
                [
                    'name' => 'photos',
                    'left' => 's=menu.php&type=photos', 'right' => 's=list.php&type=photos',
                ],
                [
                    'name' => 'fileBrowser',
                    'script' => 'nw(\'kcfinder\', \'all\')',
                ],
                /*[
                    'name' => 'fileBrowser',
                    'submenu' => [
                        [
                            'name' => 'browseImages',
                            'script' => 'nw(\'kcfinder\')',
                        ],
                        [
                            'name' => 'browseFiles',
                            'script' => 'nw(\'kcfinder\', \'other\')',
                        ],
                    ],
                ],*/
            ],
        ],
        [
            'name' => 'toolsMenu',
            'submenu' => [
                [
                    'name' => 'dbbackup',
                    'left' => 's=menu.php&type=dbbackup', 'right' => 's=list.php&type=dbbackup',
                ],

                [
                    'name' => 'stats',
                    'left' => 'blank', 'right' => 's=board.php&type=stats',
                ],
                [
                    'name' => 'export',
                    'submenu' => [
                        [
                            'name' => 'export_products',
                            'left' => 's=menu.php&type=export', 'right' => 's=board.php&type=export_products',
                        ],
                        [
                            'name' => 'export_selling_products',
                            'left' => 's=menu.php&type=export', 'right' => 's=board.php&type=export_selling_products',
                        ],
                        [
                            'name' => 'export_orders',
                            'left' => 's=menu.php&type=export', 'right' => 's=board.php&type=export_orders',
                        ],
                        [
                            'name' => 'export_users',
                            'left' => 's=menu.php&type=export', 'right' => 's=board.php&type=export_users',
                        ],
                    ],
                ],
                [
                    'name' => 'import',
                    'submenu' => [
                        [
                            'name' => 'import-generic',
                            'right' => 's=import.generic.php',
                        ],
                        [
                            'name' => 'import_automatic',
                            'left' => 's=menu.php&type=automatic_import', 'right' => 's=list.php&type=automatic_import',
                        ],
                        [
                            'name' => 'import-xml_feed',
                            'script' => "nw('import.xml_feed')",
                        ],
                        [
                            'name' => 'import-xml_feed_new',
                            'script' => "nw('import.xml_feed_new')",
                        ],
                    ],
                ],
                [
                    'name' => 'cleaning',
                    'script' => "nw('cleaning')",
                ],
            ],
        ],
        [
            'name' => 'settingsMenu',
            'submenu' => [
                [
                    'name' => 'admins',
                    'left' => 's=menu.php&type=admins', 'right' => 's=list.php&type=admins',
                ],
                [
                    'name' => 'settings',
                    'script' => "nw('settings')",
                ],
                [
                    'name' => 'emails',
                    'script' => "nw('emails')",
                ],
                [
                    'name' => 'delivery',
                    'submenu' => [
                        [
                            'name' => 'delivery_type',
                            'left' => 's=menu.php&type=delivery', 'right' => 's=list.php&type=delivery',
                        ],
                        [
                            'name' => 'deliveryDelivery',
                            'left' => 's=menu.php&type=deliveryDelivery', 'right' => 's=list.php&type=deliveryDelivery',
                        ],
                        [
                            'name' => 'deliveryPayment',
                            'left' => 's=menu.php&type=deliveryPayment', 'right' => 's=list.php&type=deliveryPayment',
                        ],
                    ],
                ],
                [
                    'name' => 'vats',
                    'left' => 's=menu.php&type=vats', 'right' => 's=list.php&type=vats',
                ],
                [
                    'name' => 'priceLevels',
                    'left' => 's=menu.php&type=pricelevels', 'right' => 's=list.php&type=pricelevels',
                ],
                [
                    'name' => 'fulltext',
                    'script' => "nw('fulltext')",
                ],
            ],
        ],
    ];

    public function getMenu(): array
    {
        $userRights = new UserRights();
        /** @var \Symfony\Component\EventDispatcher\EventDispatcher $dispatcher */
        $dispatcher = ServiceContainer::getService('event_dispatcher');
        $event = new \KupShop\KupShopBundle\Event\CreateMenuEvent($this, $userRights);
        $dispatcher->dispatch($event, \KupShop\KupShopBundle\Event\CreateMenuEvent::COMPLETING_TREE);
        $adminRegisterLocator = ServiceContainer::getService(AdminRegisterLocator::class);
        foreach ($adminRegisterLocator->getMenu() as $menu => $items) {
            foreach ($items as $item) {
                $this->addItem($menu, $item);
            }
        }
        $this->sortMenu($this->menu);

        return $this->menu;
    }

    private function sortMenu(&$menu)
    {
        foreach ($menu as $pos => &$item) {
            if (is_array($item) && array_key_exists('submenu', $item)) {
                $this->sortMenu($item['submenu']);
            }

            if (is_array($item) && array_key_exists('position', $item)) {
                $menuItem = [$menu[$pos]];
                unset($menu[$pos]);
                array_splice($menu, $item['position'], 0, $menuItem);
            }
        }
    }

    public function addItem($menuName, $item, &$menu = null, $index = 0)
    {
        if (!$menu) {
            $menu = &$this->menu;
        }
        // / new
        $menuNames = $this->getMenuNames($menuName);
        $maxIndex = count($menuNames) - 1;
        foreach ($menu as &$menuItem) {
            if ($menuItem['name'] == $menuNames[$index]) {
                if ($index == $maxIndex) {
                    $menuItem['submenu'][] = $item;

                    return true;
                } else {
                    $result = $this->addItem($menuName, $item, $menuItem['submenu'], ++$index);
                    if ($result) {
                        return true;
                    }
                }
            }
        }

        // create root menu item if $menuItem was not found
        $menu[] = $item;

        return false;
    }

    public function getMenuNames($menuName)
    {
        $names = explode('/', $menuName);

        return $names;
    }

    public function setMenu(array $menu)
    {
        $this->menu = $menu;
    }
}
