# -*- coding: utf8 -*-
from flask import url_for

ALL_ARTICLE_TYPES = [
    'article',
    'gallery',
    'podcast',
    'review',
    'video',
    'legacy-promotion'
]

ALL_TAG_TYPES = [
    'topic',
    'person',
    'brand',
    'author',
    'event',
    'location',
    'season',
    'genre',
    'series',
    'show_type'
]

def get_article_type(item):
    if 'type' in item:
        return item['type']
    if 'article_type' in item:
        return item['article_type']
    return None


def url_for_article(article):
    '''Generate a URL for an article'''

    article_type = get_article_type(article);

    if article_type == 'legacy-promotion':
        return url_for('promotions.promotion_by_slug', slug=article['slug'])

    if article_type == 'video':
        return url_for('video.video_by_slug', slug=article['slug'])

    article_variants = ['gallery']
    route = article_type if article_type in article_variants else 'article'
    return url_for('article.%s_by_slug' % route, slug=article['slug'])


def get_type(item):
    '''Get the type of a thing by poking it for various keys'''
    if 'type' in item:
        return item['type']

    if 'article_type' in item:
        return item['article_type']

    return None


def is_article(thing):
    '''Checks if a thing is an article'''

    return get_type(thing) in ALL_ARTICLE_TYPES


def is_tag(thing):
    '''Checks if a thing is a tag'''

    return get_type(thing) in ALL_TAG_TYPES


GLOBALS = {
    'is_article': is_article,
    'is_tag': is_tag,
    'url_for_article': url_for_article
}