<?php

class CatalyzToolbar {
    protected static $instance;
    protected function __construct()
    {
    }
    /**
     *
     * @return CatalyzToolbar
     */
    static function instance()
    {
        if (empty(self::$instance)) {
            self::$instance = new CatalyzToolbar();
        }
        return self::$instance;
    }

    protected function getDefaultActions($page)
    {
        $canAdd = count($page->getValidSubClasses()) > 0;
        $canEdit = true;
        $ContentTree = ContentTree::instance(Catalyz::getPageCulture());
        $ContentTree->build();
        $ContentTree->initializeCurrentNodeById($page->getNodeId());
        $CurrentNode = $ContentTree->getCurrentNode();
        // var_dump(ContentTree::instance());exit;
        $canReorder = count($CurrentNode->children) > 1;
        $canDelete = !empty($CurrentNode->parent);
        $canTranslate = ($page instanceof ICatalyzTranslatable) && count(sfConfig::get('app_translations_available_languages')) > 1;

        if ($page instanceof ICatalyzToolbarActionManager) {
            $canAdd = $page->canAddChild($canAdd);
            $canEdit = $page->canEdit();
            $canReorder = $page->canReorderChildren($canReorder);
            $canDelete = $canDelete && $page->canDelete();
        }
        if ($page instanceof ICatalyzToolbarActionStringManager) {
            $addCaption = $page->getCatalyzToolbarAddCaption();
            $reorderCaption = $page->getCatalyzToolbarReorderCaption();
        } else {
            $addCaption = false;
            $reorderCaption = false;
        }
        if (false == $addCaption) {
            $addCaption = __('Add a page', null, 'catalyz');
        }
        if (false == $reorderCaption) {
            $reorderCaption = __('Reorder', null, 'catalyz');
        }

        $disabledButtons = sfConfig::get('app_catalyz_disabled_action', array());

        $actions = array();
        if ($canEdit && !in_array('edit', $disabledButtons)) {
            $actions[] = array(
                'key' => 'edit',
                'caption' => __('Edit', null, 'catalyz'),
                'url' => sfContext::getInstance()->getController()->genUrl('@node-translation-edit?node_id=' . $page->getNodeId() . '&culture=' . $CurrentNode->getCulture())
                );
        }
        if ($canAdd && !in_array('add', $disabledButtons)) {
            $actions[] = array(
                'key' => 'add',
                'caption' => $addCaption,
                'url' => sfContext::getInstance()->getController()->genUrl('@node-add?parent_id=' . $page->getNodeId())
                );
        }
        if ($canTranslate && !in_array('translations', $disabledButtons)) {
            $actions[] = array(
                'key' => 'translation',
                'caption' => __('Language versions', null, 'catalyz'),
                'url' => sfContext::getInstance()->getController()->genUrl('@node-translations?node_id=' . $page->getNodeId())
                );
        }
        if ($canReorder && !in_array('reorder', $disabledButtons)) {
            $actions[] = array(
                'key' => 'reorder',
                'caption' => $reorderCaption,
                'url' => sfContext::getInstance()->getController()->genUrl('@node-reorder?node_id=' . $page->getNodeId())
                );
        }
        if ($canDelete && !in_array('delete', $disabledButtons)) {
            $actions[] = array(
                'key' => 'delete',
                'caption' => __('Delete', null, 'catalyz'),
                'url' => sfContext::getInstance()->getController()->genUrl('@node-delete?node_id=' . $page->getNodeId())
                );
        }
        if (!in_array('admin', $disabledButtons)) {
            $actions[] = array(
                'key' => 'admin',
                'caption' => __('Admin', null, 'catalyz'),
                'url' => sfContext::getInstance()->getController()->genUrl('@admin?node_id=' . $page->getNodeId())
                );
        }
        // $actions[] = array(
        // 'key' => 'logout',
        // 'caption' => __('X'),
        // 'url' => '/catalyz/logout'
        // );
        return $actions;
    }

    protected $actions = array();
    protected $admin_actions = array();

    public function register($action, $is_admin = false)
    {
        if (!$is_admin) {
            $this->actions[] = $action;
        } else {
            $this->admin_actions[] = $action;
        }
    }

    public function getToolbarActions($page)
    {
		sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent($page, 'catalyz.initialize_toolbar_actions'));

        $actions = $this->getDefaultActions($page);
        $newActions = $this->actions;
        if ($page instanceof ICatalyzToolbarActionProvider) {
            $newActions = array_merge($newActions, $page->getToolbarActions());
        }
        if (count($newActions) > 0) {
            foreach($newActions as $newAction) {
                $tmp = array();
                $found = false;
                foreach($actions as $existingAction) {
                    if (isset($newAction['before']) && $newAction['before'] == $existingAction['key']) {
                        $tmp[] = $newAction;
                        $found = true;
                    }
                    $tmp[] = $existingAction;
                    if (isset($newAction['after']) && $newAction['after'] == $existingAction['key']) {
                        $tmp[] = $newAction;
                        $found = true;
                    }
                }
                if (!$found) {
                    if (!empty($newAction['after'])) {
                        $tmp[] = $newAction;
                    } else {
                        throw new Exception(sprintf('Unable to find action "%s" to add "%s" action near it in Catalyz Toolbar.', $newAction['before'] . $newAction['after'], $newAction['caption']));
                    }
                }
                $actions = $tmp;
            }
        }

        return $actions;
    }

    public function getAdminActions($page)
    {
        sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent($page, 'catalyz.initialize_admin_actions'));

        $return = $this->admin_actions;
        $disabled_actions = sfConfig::get('app_catalyz_disabled_action', array());
        foreach ($return as $k => $action) {
            if (in_array($action['key'], $disabled_actions)) {
                unset($return[$k]);
            }
        }
        return $return;
    }

    protected $enabled = true;

    public function disable()
    {
        $this->enabled = false;
    }
    public function enable()
    {
        $this->enabled = true;
    }
    public function enabled()
    {
        return $this->enabled;
    }
}

?>