# -*- coding: utf-8 -*-
from django.db import models
from django.utils.translation import ugettext_lazy as _
from mptt.models import TreeForeignKey, TreeManyToManyField, MPTTModel


class Navigation(models.Model):
    """
    Navigation menu
    """
    key = models.CharField(_(u'key'), max_length=32, help_text=_(u'This value is used in the code, do not touch it!'))
    name = models.CharField(_(u'name'), max_length=70)
    links = TreeManyToManyField('Link', verbose_name=_(u'links'), through='LinkMembership')

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = _(u'navigation menu')
        verbose_name_plural = _(u'navigation menus')


class Link(MPTTModel):
    """
    Navigation link
    """
    parent = TreeForeignKey('self', verbose_name=_(u'parent link'), null=True, blank=True)
    name = models.CharField(_(u'name'), max_length=70, help_text=_(u'Name of the link in the menu'))
    url = models.CharField(_(u'url'), max_length=255, help_text=_(u'Example: "/about/" or "/"'))

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return self.url

    class Meta:
        verbose_name = _(u'navigation link')
        verbose_name_plural = _(u'navigation links')


class LinkMembership(models.Model):
    """
    Link in navigation membership
    """
    navigation = models.ForeignKey('Navigation')
    link = TreeForeignKey('Link')
    position = models.PositiveIntegerField(_(u'position'), default=0, db_index=True)

    class Meta:
        ordering = ['position']
        verbose_name = _(u'link membership')
        verbose_name_plural = _(u'link memberships')