<?php

class CatalyzCache {
    /**
     * CatalyzCache::create()
     *
     * @param int $node_id
     * @param string $culture
     */
    public static function create($node_id = null, $culture = null)
    {
        if (!is_null($node_id) && !is_null($culture)) {
            $content_tree = ContentTree::instance($culture);
            $content_tree->build();
            $content_tree->initializeCurrentNodeById($node_id);
            self::createChildren(array($content_tree->getCurrentNode()), $culture, false);
        } elseif (is_null($node_id) && is_null($culture)) {
            foreach (Catalyz::getAllTranslations() as $code => $translation_name) {
                $content_tree = ContentTree::instance($code);
                $content_tree->build();
                self::createChildren(array($content_tree->getRoot()), $code);
            }
        }
    }

    /**
     * CatalyzCache::clear()
     *
     * @param int $node_id
     * @param string $culture
     * @param boolean $regenerate
     */
    public static function clear($node_id = null, $culture = null, $regenerate = false)
    {
        if (!is_null($node_id) && !is_null($culture)) {
            $content_tree = ContentTree::instance($culture);
            $content_tree->initializeCurrentNodeById($node_id);
            $content_tree_node = $content_tree->getCurrentNode();

            if (!is_null($content_tree_node)) {
                $path_to_cache_file = $content_tree_node->getPathToCacheFile($culture);
                if (is_file($path_to_cache_file)) {
                    unlink($path_to_cache_file);
                    if ($regenerate) {
                        self::create($node_id, $culture);
                    }
                }
            }
        } elseif (is_null($node_id) && is_null($culture)) {
            sfToolkit::clearDirectory(sfConfig::get('sf_web_dir') . '/cache');
        }
    }

    public static function refresh()
    {
        $languages = Catalyz::getAllTranslations();
        foreach($languages as $culture => $langName) {
            $ContentTree = ContentTree::instance($culture);
        	$ContentTree->build();
            $iterator = new ContentTreeNodeIterator($ContentTree);
            while ($ContentTreeNode =/*(ContentTreeNode)*/ $iterator->next()) {
                $path_to_cache_file = $ContentTreeNode->getPathToCacheFile($culture);
                if (file_exists($path_to_cache_file)) {
                    unlink($path_to_cache_file);
                    self::create($ContentTreeNode->getId(), $culture);
                }
            }
        }
    }

    /**
     * CatalyzCache::clearAll()
     */
    public static function clearAll()
    {
        self::clear();
    }

    /**
     * CatalyzCache::regenerate()
     *
     * @param int $node_id
     * @param string $culture
     */
    public static function regenerate($node_id = null, $culture = null)
    {
        self::clear($node_id, $culture, true);
    }

    /**
     * CatalyzCache::regenerateAll()
     */
    public static function regenerateAll()
    {
        $configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', true);
        sfContext::createInstance($configuration);

        foreach (Catalyz::getAllTranslations() as $code => $translation_name) {
            $content_tree = ContentTree::instance($code);
            $content_tree->build();
            self::regenerateChildren(array($content_tree->getRoot()), $code);
        }
    }

    /**
     * CatalyzCache::createChildren()
     *
     * @param array $content_tree_nodes
     * @param string $culture
     * @param boolean $recursive
     */
    protected static function createChildren($content_tree_nodes, $culture, $recursive = true)
    {
        if (0 < count($content_tree_nodes)) {
            foreach ($content_tree_nodes as $content_tree_node) {
                if ($content_tree_node->isTranslationAvailable($culture)) {
                    $path_to_cache_file = $content_tree_node->getPathToCacheFile($culture);
                    if (!is_file($path_to_cache_file)) {
                        if (!is_dir(str_replace(basename($path_to_cache_file), '', $path_to_cache_file))) {
                            mkdir(str_replace(basename($path_to_cache_file), '', $path_to_cache_file), 0777, true);
                        }
                    } else {
                        unlink($path_to_cache_file);
                    }
                    file_put_contents($path_to_cache_file, file_get_contents(sfConfig::get('app_static_url') . $content_tree_node->getUrl($culture, false)));
                }
                if ($recursive) {
                    self::createChildren($content_tree_node->children, $culture);
                }
            }
        }
    }

    /**
     * CatalyzCache::regenerateChildren()
     *
     * @param array $content_tree_nodes
     * @param string $culture
     */
    protected static function regenerateChildren($content_tree_nodes, $culture)
    {
        if (0 < count($content_tree_nodes)) {
            foreach ($content_tree_nodes as $content_tree_node) {
                $path_to_cache_file = $content_tree_node->getPathToCacheFile($culture);
                if (is_file($path_to_cache_file)) {
                    unlink($path_to_cache_file);
                    file_put_contents($path_to_cache_file, file_get_contents(sfConfig::get('app_static_url') . $content_tree_node->getUrl($culture, false)));
                }
                self::regenerateChildren($content_tree_node->children, $culture);
            }
        }
    }
}