<?php

final class SiteMenuHelper
{

    private static $_mainMenuItems;

    public static function getItems()
    {
        if (isset(self::$_mainMenuItems)) return self::$_mainMenuItems;
        $fixed = array(
            array(
                'url' => array('action/index'),
                'label' => Yii::t('site', 'Акции'),
                'active' => "\$controller->id == 'action'",
            ),
            array(
                'url' => array('service/index'),
                'label' => Yii::t('site', 'Сервис'),
                'active' => "\$controller->id == 'service'",
            ),
            array(
                'url' => array('site/contact'),
                'label' => Yii::t('site', 'Контакты'),
                'active' => "\$controller->id == 'site' && \$controller->action->id == 'contact'",
            ),
            array(
                'url' => array('stoFilter/index'),
                'label' => Yii::t('site', 'Расчет стоимости ТО'),
                'active' => "\$controller->id == 'stoFilter'",
            ),
        );
        $items = $fixed;
        /** @var $rootNodes Node[] */
        $rootNodes = Node::model()->with('i18n')->findAll('node_id is null');
        $maping = array(
            'aboutRoot' => 'about/index',

            'jobsSection' => '',
            'partnersSection' => '',

            'newsRoot' => 'news/index',
            'questionRoot' => 'question/index',
            'reviewsRoot' => 'reviews/index',
            'saleRoot' => 'sale/index',

            'sportRoot' => 'sport/index',
            'albumsSection' => '',
            'calendarSection' => '',

            'textRoot' => 'textRoot/index',
            'textSection' => '',
            'textPage' => '',

            'tuningRoot' => 'tuning/index',
            'warrantyRoot' => 'warranty/index',
            'workRoot' => 'work/index',
        );

        foreach ($rootNodes as $node) {
            $route = $maping[$node->data_type];
            if (!empty($route) && !$node->hidden) {
                $items[] = array(
                    'url' => array($route, 'node_id' => $node->id),
                    'label' => $node->i18n->label,
                    'active' => 'isset($_GET["node_id"]) && $_GET["node_id"]==' . $node->id,
                );
            }
        }
        $res = array();
        foreach ($items as $item) {
            $res[crc32(serialize($item['url']))] = $item;
        }
        self::$_mainMenuItems = $res;
        return $res;
    }


    public static function getList()
    {
        $res = array();
        foreach (self::getItems() as $k => $item) {
            $res[$k] = $item['label'];
        }
        return $res;
    }

    /**
     * @static
     * @param $selected
     * @return array
     */
    public static function getListWithSelected($selected)
    {
        $items = self::getList();
        if (empty($selected)) return $items;
        $res = array();
        foreach ($selected as $i)
            if (!empty($items[$i])) {
                $res[$i] = $items[$i];
                unset($items[$i]);
            } else {
                unset($items[$i]);
            }
        $res += $items;
        return $res;
    }
}
