from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _

from olympia.constants.applications import (
    ANDROID, FIREFOX, SEAMONKEY, THUNDERBIRD)
from olympia.constants.base import (
    ADDON_DICT, ADDON_EXTENSION, ADDON_LPAPP, ADDON_PERSONA, ADDON_SEARCH,
    ADDON_SLUGS, ADDON_STATICTHEME, ADDON_THEME)


class StaticCategory(object):
    """Helper to populate `CATEGORIES` and provide some helpers.

    Note that any instance is immutable to avoid changing values
    on the globally unique instances during test runs which can lead
    to hard to debug sporadic test-failures.
    """

    def __init__(self, id=None, app=None, type=None, misc=False,
                 name=None, slug=None, weight=0, description=None):
        # Avoid triggering our own __setattr__ implementation
        # to keep immutability intact but set initial values.
        object.__setattr__(self, 'id', id)
        object.__setattr__(self, 'application', app)
        object.__setattr__(self, 'misc', misc)
        object.__setattr__(self, 'name', name)
        object.__setattr__(self, 'slug', slug)
        object.__setattr__(self, 'type', type)
        object.__setattr__(self, 'weight', weight)
        object.__setattr__(self, 'description', description)

    def __unicode__(self):
        return unicode(self.name)

    def __repr__(self):
        return u'<%s: %s (%s)>' % (
            self.__class__.__name__, self.__unicode__(), self.application)

    def get_url_path(self):
        try:
            type = ADDON_SLUGS[self.type]
        except KeyError:
            type = ADDON_SLUGS[ADDON_EXTENSION]
        return reverse('browse.%s' % type, args=[self.slug])

    def _immutable(self, *args):
        raise TypeError('%r instances are immutable' %
                        self.__class__.__name__)

    __setattr__ = __delattr__ = _immutable
    del _immutable


CATEGORIES = {
    FIREFOX.id: {
        ADDON_EXTENSION: {
            'alerts-updates': StaticCategory(
                id=72, name=_(u'Alerts & Updates')),
            'appearance': StaticCategory(id=14, name=_(u'Appearance')),
            'bookmarks': StaticCategory(id=22, name=_(u'Bookmarks')),
            'download-management': StaticCategor
y(
                id=5, name=_(u'Download Management')),
            'feeds-news-blogging': StaticCategory(
     
           id=1, name=_(u'Feeds, News & Blogging')),
            'games-entertainment': StaticCategory(
                id=142, name=_(u'Games & Entertainment')),
            'language-support': StaticCategory(
                id=37, name=_(u'Language Support')),
            'photos-music-videos': StaticCategory(
                id=38, name=_(u'Photos, Music & Videos')),
            'privacy-security': StaticCategory(
                id=12, name=_(u'Privacy & Security')),
            'search-tools': StaticCategory(id=13, name=_(u'Search Tools')),
            'shopping': StaticCategory(id=141, name=_(u'Shopping')),
            'social-communication': StaticCategory(
                id=71, name=_(u'Social & Communication')),
            'tabs': StaticCategory(id=93, name=_(u'Tabs')),
            'web-development': StaticCategory(
                id=4, name=_(u'Web Development')),
            'other': StaticCategory(id=73, name=_(u'Other'), weight=333)
        },
        ADDON_THEME: {
            'animals': StaticCategory(id=30, name=_(u'Animals')),
            'compact': StaticCategory(id=32, name=_(u'Compact')),
            'large': StaticCategory(id=67, name=_(u'Large')),
            'miscellaneous': StaticCategory(id=21, name=_(u'Miscellaneous')),
            'modern': StaticCategory(id=62, name=_(u'Modern')),
            'nature': StaticCategory(id=29, name=_(u'Nature')),
            'os-integration': StaticCategory(id=61, name=_(u'OS Integration')),
            'retro': StaticCategory(id=31, name=_(u'Retro')),
            'sports': StaticCategory(id=26, name=_(u'Sports'))
        },
        ADDON_STATICTHEME: {
            'abstract': StaticCategory(id=300, name=_(u'Abstract')),
            'causes': StaticCategory(id=320, name=_(u'Causes')),
            'fashion': StaticCategory(id=324, name=_(u'Fashion')),
            'film-and-tv': StaticCategory(id=326, name=_(u'Film and TV')),
            'firefox': StaticCategory(id=308, name=_(u'Firefox')),
            'foxkeh': StaticCategory(id=310, name=_(u'Foxkeh')),
            'holiday': StaticCategory(id=328, name=_(u'Holiday')),
            'music': StaticCategory(id=322, name=_(u'Music')),
            'nature': StaticCategory(id=302, name=_(u'Nature')),
            'other': StaticCategory(id=314, name=_(u'Other'), weight=333),
            'scenery': StaticCategory(id=306, name=_(u'Scenery')),
            'seasonal': StaticCategory(id=312, name=_(u'Seasonal')),
            'solid': StaticCategory(id=318, name=_(u'Solid')),
            'sports': StaticCategory(id=304, name=_(u'Sports')),
            'websites': StaticCategory(id=316, name=_(u'Websites'))
        },
        ADDON_DICT: {
            'general': StaticCategory(id=95, name=_(u'General'))
        },
        ADDON_SEARCH: {
            'bookmarks': StaticCategory(id=79, name=_(u'Bookmarks')),
            'business': StaticCategory(id=80, name=_(u'Business')),
            'dictionaries-encyclopedias': StaticCategory(
                id=81, name=_(u'Dictionaries & Encyclopedias')),
            'general': StaticCategory(id=82, name=_(u'General')),
            'kids': StaticCategory(id=83, name=_(u'Kids')),
            'multiple-search': StaticCategory(
                id=84, name=_(u'Multiple Search')),
            'music': StaticCategory(id=85, name=_(u'Music')),
            'news-blogs': StaticCategory(id=86, name=_(u'News & Blogs')),
            'photos-images': StaticCategory(id=87, name=_(u'Photos & Images')),
            'shopping-e-commerce': StaticCategory(
                id=88, name=_(u'Shopping & E-Commerce')),
            'social-people': StaticCategory(id=89, name=_(u'Social & People')),
            'sports': StaticCategory(id=90, name=_(u'Sports')),
            'travel': StaticCategory(id=91, name=_(u'Travel')),
            'video': StaticCategory(id=78, name=_(u'Video'))
        },
        ADDON_LPAPP: {
            'general': StaticCategory(id=98, name=_(u'General'))
        },
        ADDON_PERSONA: {
            'abstract': StaticCategory(id=100, name=_(u'Abstract')),
            'causes': StaticCategory(id=120, name=_(u'Causes')),
            'fashion': StaticCategory(id=124, name=_(u'Fashion')),
            'film-and-tv': StaticCategory(id=126, name=_(u'Film and TV')),
            'firefox': StaticCategory(id=108, name=_(u'Firefox')),
            'foxkeh': StaticCategory(id=110, name=_(u'Foxkeh')),
            'holiday': StaticCategory(id=128, name=_(u'Holiday')),
            'music': StaticCategory(id=122, name=_(u'Music')),
            'nature': StaticCategory(id=102, name=_(u'Nature')),
            'other': StaticCategory(id=114, name=_(u'Other')),
            'scenery': StaticCategory(id=106, name=_(u'Scenery')),
            'seasonal': StaticCategory(id=112, name=_(u'Seasonal')),
            'solid': StaticCategory(id=118, name=_(u'Solid')),
            'sports': StaticCategory(id=104, name=_(u'Sports')),
            'websites': StaticCategory(id=116, name=_(u'Websites'))
        }
    },
    ANDROID.id: {
        ADDON_EXTENSION: {
            'device-features-location': StaticCategory(
                id=145, name=_(u'Device Features & Location')),
            'experimental': StaticCategory(id=151, name=_(u'Experimental')),
            'feeds-news-blogging': StaticCategory(
                id=147, name=_(u'Feeds, News, & Blogging')),
            'performance': StaticCategory(id=144, name=_(u'Performance')),
            'photos-media': StaticCategory(id=143, name=_(u'Photos & Media')),
            'security-privacy': StaticCategory(
                id=149, name=_(u'Security & Privacy')),
            'shopping': StaticCategory(id=150, name=_(u'Shopping')),
            'social-networking': StaticCategory(
                id=148, name=_(u'Social Networking')),
    