<?php

class Dispatcher extends DispatcherCore
{
    /**
     * Create an url from
     *
     * @@param string $route_id Name the route
     * @@param int $id_lang
     * @@param array $params
     * @@param bool $use_routes If false, don't use to create this url
     * @@param string $anchor Optional anchor to add at the end of this url
     */
    public function createUrl($route_id, $id_lang = null, array $params = array(), $force_routes = false, $anchor = '')
    {
        if (!$id_lang)
            $id_lang = Context::getContext()->language->id;
        
        if (!isset($this->routes[$id_lang][$route_id]))
        {
            $query = http_build_query($params, '', '&');
            $index_link = $this->use_routes ? '' : 'index.php';
            $url = ($route_id == 'index') ? $index_link.(($query) ? '?'.$query : '') : 'index.php?controller='.$route_id.(($query) ? '&'.$query : '').$anchor;
            return $this->getDesignerPreviewParam($url);
        }
        $route = $this->routes[$id_lang][$route_id];

        // Check required fields
        $query_params = isset($route['params']) ? $route['params'] : array();
        foreach ($route['keywords'] as $key => $data)
        {
            if (!$data['required'])
                continue;

            if (!array_key_exists($key, $params))
                die('Dispatcher::createUrl() miss required parameter "'.$key.'" for route "'.$route_id.'"');
            if (isset($this->default_routes[$route_id]))
                $query_params[$this->default_routes[$route_id]['keywords'][$key]['param']] = $params[$key];
        }

        // Build an url which match a route
        if ($this->use_routes || $force_routes)
        {
            $url = $route['rule'];
            $add_param = array();
            foreach ($params as $key => $value)
            {
                if (!isset($route['keywords'][$key]))
                {
                    if (!isset($this->default_routes[$route_id]['keywords'][$key]))
                        $add_param[$key] = $value;
                }
                else
                {
                    if ($params[$key])
                        $replace = $route['keywords'][$key]['prepend'].$params[$key].$route['keywords'][$key]['append'];
                    else
                        $replace = '';
                    $url = preg_replace('#\{([^{}]+:)?'.$key.'(:[^{}])?\}#', $replace, $url);
                }
            }
            $url = preg_replace('#\{([^{}]+:)?[a-z0-9_]+?(:[^{}])?\}#', '', $url);
            if (count($add_param))
                $url .= '?'.http_build_query($add_param, '', '&');
        }
        // Build a classic url index.php?controller=foo&...
        else
        {
            $add_params = array();
            foreach ($params as $key => $value)
                if (!isset($route['keywords'][$key]) && !isset($this->default_routes[$route_id]['keywords'][$key]))
                    $add_params[$key] = $value;

            if (!empty($route['controller']))
                $query_params['controller'] = $route['controller'];
            $query = http_build_query(array_merge($add_params, $query_params), '', '&');
            if ($this->multilang_activated)
                $query .= (!empty($query) ? '&' : '').'id_lang='.(int)$id_lang;
            $url = 'index.php?'.$query;
            
        }

        $url = $this->getDesignerPreviewParam($url);
        return $url.$anchor;
    }

    private function getDesignerPreviewParam($url)
    {
        $themeParam = 'theme_name'; // TODO: Designer preview parameter
        $themeName = Tools::getValue($themeParam);
        // removed (strpos ($url, $themeParam) !== FALSE). back parameter might contains it
        if (!$themeName) return $url;
        
        $domain = Tools::getValue('domain');
        $param = "$themeParam=$themeName" . ($domain ? '&domain=' . Tools::getValue('domain') : '');
        $t = explode('?', $url);
        $t[0] .= '?' . $param . (sizeof($t) > 1 ? '&' : '');
        return implode($t);
    }
}
