# -*- coding: utf-8 -*-
from cms.models.fields import PlaceholderField
import os
from django.contrib.auth.models import Group, User
from django.core.files.storage import default_storage
from django.conf import settings
from django.db import models
from django.core.urlresolvers import reverse_lazy
from django.utils.translation import ugettext_lazy as _
from filer.fields.file import FilerFileField
from image_cropping import ImageRatioField
from parler.models import TranslatableModel, TranslatedFields

from portal.storage import PrivateS3MediaStorage
from project.utils import CroppableFilerImageField


class Profile(models.Model):
    user = models.OneToOneField(User, related_name='profile', on_delete=models.CASCADE)
    first_name = models.CharField(verbose_name=_('Vorname'), max_length=255)
    last_name = models.CharField(verbose_name=_('Nachname'), max_length=255)
    street = models.CharField(verbose_name=_('Strasse'), max_length=255, null=True, blank=True)
    zip = models.IntegerField(verbose_name=_('PLZ'), null=True, blank=True)
    place = models.CharField(verbose_name=_('Ort'), max_length=255, null=True, blank=True)
    email = models.EmailField(verbose_name=_('E-Mail'), null=True, blank=True)

    class Meta:
        verbose_name = 'User Profil'
        verbose_name_plural = 'User Profile'

    def __str__(self):
        return self.full_name

    @property
    def full_name(self):
        return '{} {}'.format(self.first_name, self.last_name)


class Announcement(TranslatableModel):
    groups = models.ManyToManyField(Group, verbose_name='Mitglieder Gruppe', related_name='announcements')
    image = CroppableFilerImageField(verbose_name='Bild', blank=True, null=True)
    cropping = ImageRatioField('image', '1200x800', free_crop=True)

    placeholder = PlaceholderField('content')

    published = models.BooleanField(verbose_name='Veröffentlicht', default=False)
    updated = models.DateTimeField(verbose_name='Aktualisiert', auto_now=True)
    informed_users = models.ManyToManyField(User, verbose_name='Als gelesen markiert von:', null=True, blank=True)

    translations = TranslatedFields(
        title=models.CharField(max_length=100, verbose_name='Title'),
    )

    class Meta:
        verbose_name = 'Neuigkeit'
        verbose_name_plural = 'Aktuell'
        ordering = ['-updated']

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse_lazy('portal:announcement', args=[self.pk])


class InformationSection(TranslatableModel):
    translations = TranslatedFields(
        title=models.CharField(max_length=100, verbose_name='Title')
    )
    ordering = models.IntegerField(default=50, verbose_name='Sortierung')

    class Meta:
        verbose_name = 'Information Section'
        verbose_name_plural = 'Information Sections'
        ordering = ['ordering']

    def __str__(self):
        return self.title


class Information(TranslatableModel):
    section = models.ForeignKey(InformationSection, verbose_name='Information Section', related_name='informations',
                                null=True, blank=True)
    groups = models.ManyToManyField(Group, verbose_name='Mitglieder Gruppe', related_name='informations')
    image = CroppableFilerImageField(verbose_name='Bild', blank=True, null=True)
    cropping = ImageRatioField('image', '1200x800', free_crop=True)

    placeholder = PlaceholderField('content')
    published = models.BooleanField(verbose_name='Veröffentlicht', default=False)

    translations = TranslatedFields(
        title=models.CharField(max_length=100, verbose_name='Title'),
        description=models.TextField(verbose_name='Beschreibung'),
        tag_list=models.TextField(verbose_name='Tags', help_text='Mit Komma getrennt', null=True, blank=True)
    )

    class Meta:
        verbose_name = 'Information'
        verbose_name_plural = 'Informationen'

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse_lazy('portal:information', args=[self.pk])


class DownloadSection(TranslatableModel):
    translations = TranslatedFields(
        title=models.CharField(max_length=100, verbose_name='Title')
    )
    ordering = models.IntegerField(default=50, verbose_name='Sortierung')

    class Meta:
        verbose_name = 'Download Section'
        verbose_name_plural = 'Download Sections'
        ordering = ['ordering']

    def __str__(self):
        return self.title


class DownloadFile(TranslatableModel):
    section = models.ForeignKey(DownloadSection, verbose_name='Download Section', related_name='files')
    groups = models.ManyToManyField(Group, verbose_name='Mitgliedergruppen', related_name='files')

    translations = TranslatedFields(
        name=models.CharField(max_length=512, verbose_name='Name', blank=True, null=True),
        description=models.TextField(verbose_name='Beschreibung', blank=True, null=True),
        file=FilerFileField(verbose_name='File'),
        tag_list=models.TextField(verbose_name='Tags', help_text='Mit Komma getrennt', null=True, blank=True)
    )

    ordering = models.IntegerField(default=50, verbose_name='Sortierung')

    class Meta:
        verbose_name = 'Download File'
        verbose_name_plural = 'Download Files'
        ordering = ['ordering']

    def __str__(self):
        return self.label

    @property
    def url(self):
        return self.file.url

    @property
    def extension(self):
        return self.file.name.split('.')[-1]

    @property
    def label(self):
        if self.name:
            return self.name
        else:
            return os.path.basename(self.file.name)
