from __future__ import absolute_import

import re
from collections import OrderedDict

from django.core.exceptions import ObjectDoesNotExist
from django.contrib.sites.models import Site
from django.utils.translation import override

from cms.utils.i18n import (get_public_languages, get_fallback_languages,
                            hide_untranslated)
from .config import get_config_redirectors


LANG_REDIRECTOR = 'x-default'


def get_alternate(page, protocol=None, enable_redirectors=None):
    # Optimize for repeated calls within the same site
    site = Site.objects.get_current()
    site_languages = get_public_languages(site.pk)
    sitemap_languages = page.get_languages()
    # TODO: build API for this default
    if enable_redirectors is None:
       enable_redirectors = get_config_redirectors()

    protocol = protocol if protocol is not None else 'http'

    # handle case when hide_untranslated = False
    if len(sitemap_languages) < len(site_languages):
        for lang in site_languages:
            if (lang not in sitemap_languages
                and not hide_untranslated(lang, site.pk)):
                for fb_lang in sitemap_languages:
                    if fb_lang in get_fallback_languages(lang, site.pk):
                        sitemap_languages.append(lang)
                        break

    # Append default redirector link
    if sitemap_languages and enable_redirectors:
        sitemap_languages.append(LANG_REDIRECTOR)

    alt_links = OrderedDict()

    for lang in sitemap_languages:
        if lang == LANG_REDIRECTOR:
            with override(sitemap_languages[0]):
                url = page.get_absolute_url()
            url = re.sub(r'^/{}'.format(sitemap_languages[0]), '', url)
            assert sitemap_languages[0] != LANG_REDIRECTOR
        else:
            with override(lang):
                try:
                    url = page.get_absolute_url(language=lang, fallback=False)
                except ObjectDoesNotExist:
                    url = page.get_absolute_url(language=lang)
        alt_links[lang] = {'language_code': lang,
                           'location': '%s://%s%s' % (protocol,
                                                      site.domain,
                                                      url),
                           'redirector': lang == LANG_REDIRECTOR,
                           'path': url}
    return alt_links
