# -*- coding: utf-8 -*-
from cms.models.fields import PageField
from cms.models.pluginmodel import CMSPlugin
from django.db import models
from itertools import chain
from djangocms_picture.models import AbstractPicture
from djangocms_text_ckeditor.fields import HTMLField
from filer.fields.folder import FilerFolderField
from cms.extensions import PageExtension
from cms.extensions.extension_pool import extension_pool
from fontawesome.fields import IconField
from parler.models import TranslatableModel, TranslatedFields
from project.utils import CroppableFilerImageField
from image_cropping import ImageRatioField
from project.utils import AbstractClassWithoutFieldsNamed as without


class ImageExtension(PageExtension):
    image = CroppableFilerImageField(verbose_name='Bild', null=True, blank=True)
    cropping = ImageRatioField('image', '1000x1000', free_crop=True)


extension_pool.register(ImageExtension)


class Section(CMSPlugin):
    title = HTMLField(verbose_name='Titel', configuration='CKEDITOR_SETTINGS_SECTION_TITLE')

    class Meta(CMSPlugin.Meta):
        verbose_name = 'Section'
        verbose_name_plural = 'Sections'

    def __str__(self):
        return self.title


class SectionText(CMSPlugin):
    columns = models.BooleanField(verbose_name='Spalten', default=False)

    class Meta(CMSPlugin.Meta):
        verbose_name = 'Section Text'
        verbose_name_plural = 'Section Texts'


class IntroImage(CMSPlugin):
    image = CroppableFilerImageField(verbose_name='Bild')
    cropping = ImageRatioField('image', '1000x1000', free_crop=True)

    class Meta(CMSPlugin.Meta):
        verbose_name = 'Intro Bild'
        verbose_name_plural = 'intro Bilder'


class Video(CMSPlugin):
    vimeo_id = models.IntegerField(verbose_name='Vimeo ID', help_text='e.g. https://vimeo.com/<b>131766159</b>')
    thumbnail = CroppableFilerImageField(verbose_name='Vorschaubild')
    cropping = ImageRatioField('thumbnail', '1000x1000', free_crop=True)

    class Meta(CMSPlugin.Meta):
        verbose_name = 'Video'
        verbose_name_plural = 'Videos'

    def __str__(self):
        return str(self.vimeo_id)


class Notification(TranslatableModel):
    translations = TranslatedFields(
        title=models.CharField('Titel', max_length=256, null=True, blank=True),
        text=models.TextField('Text', null=True, blank=True),
        cta=models.CharField('CTA Text', max_length=256, null=True, blank=True)
    )
    page = PageField(verbose_name='Seite')
    published = models.BooleanField(verbose_name='Veröffentlicht', default=True)
    updated = models.DateTimeField(verbose_name='Aktualisiert', auto_now=True)

    class Meta:
        verbose_name = 'Benachrichtigung'
        verbose_name_plural = 'Benachrichtigungen'

    def __str__(self):
        return self.get_title

    @property
    def get_title(self):
        if self.title:
            return self.title
        else:
            return self.page.get_page_title()

    @property
    def get_text(self):
        if self.text:
            return self.text
        else:
            return self.page.get_meta_description()


class SliderItemQualification(TranslatableModel):
    translations = TranslatedFields(
        name=models.CharField('name', max_length=256)
    )
    ordering = models.IntegerField(verbose_name='Sortierung', default=5)

    class Meta:
        verbose_name = 'Slider Qualifikation'
        verbose_name_plural = 'Slider Qualifikationen'
        ordering = ['ordering']

    def __str__(self):
        return self.name


class SliderItem(CMSPlugin):
    image = CroppableFilerImageField(verbose_name='Bild')
    cropping = ImageRatioField('image', '1000x1000', free_crop=True)
    title = HTMLField(verbose_name='Titel', configuration='CKEDITOR_SETTINGS_SLIDER_TITLE')
    subline = HTMLField(verbose_name='Untertitel', configuration='CKEDITOR_SETTINGS_SLIDER_TEXT', blank=True, null=True)
    text = HTMLField(verbose_name='Text', configuration='CKEDITOR_SETTINGS_SLIDER_TEXT', blank=True, null=True)
    qualifications = models.ManyToManyField(SliderItemQualification, verbose_name='Qualifikationen', blank=True,
                                            null=True)
    email = models.EmailField(verbose_name='E-Mail', blank=True, null=True)
    next_slide_text = models.CharField(verbose_name='Nächster Slide Text', max_length=255, blank=True, null=True)

    class Meta(CMSPlugin.Meta):
        verbose_name = 'Slider Item'
        verbose_name_plural = 'Slider Items'

    def __str__(self):
        return self.title

    def copy_relations(self, oldinstance):
        self.qualifications = oldinstance.qualifications.all()


class TextSliderItem(CMSPlugin):
    text = HTMLField(verbose_name='Text', configuration='CKEDITOR_SETTINGS_TEXT_SLIDER_TEXT')

    class Meta(CMSPlugin.Meta):
        verbose_name = 'Text Slider Item'
        verbose_name_plural = 'Text Slider Items'

    def __str__(self):
        return self.text


class Quote(CMSPlugin):
    content = HTMLField(verbose_name='Inhalt', configuration='CKEDITOR_SETTINGS_QUOTE_CONTENT')
    source = HTMLField(verbose_name='Quelle', configuration='CKEDITOR_SETTINGS_QUOTE_SOURCE')
    image = CroppableFilerImageField(verbose_name='Bild', null=True, blank=True)
    cropping = ImageRatioField('image', '1000x1000', free_crop=True)

    class Meta(CMSPlugin.Meta):
        verbose_name = 'Quote'
        verbose_name_plural = 'Quotes'

    def __str__(self):
        return self.content


class DownloadSection(CMSPlugin):
    class Meta(CMSPlugin.Meta):
        verbose_name = 'Download Section'
        verbose_name_plural = 'Download Sections'

    def copy_relations(self, oldinstance):
        self.folders.all().delete()

        for folder in oldinstance.folders.all():
            folder.pk = None
            folder.download_section = self
            folder.save()

    @property
    def items(self):
        items = []
        for folder in self.folders.all():
            items = chain(items, folder.folder.files)
        return list(items)


class DownloadSectionFolder(models.Model):
    download_section = models.ForeignKey(DownloadSection, verbose_name='Download Section', related_name='folders')
    folder = FilerFolderField(verbose_name='Ordner')
    ordering = models.IntegerField(verbose_name='Sortierung', default=5)

    class Meta:
        ordering = ['ordering']


class HighlightList(CMSPlugin):
    full_width = models.BooleanField(verbose_name='Volle Breite', default=False)

    class Meta(CMSPlugin.Meta):
        verbose_name = 'Highlight List'
        verbose_name_plural = 'Highlight Lists'


ANIMATED_ICON_CHOICES = (
    ('strength', 'Stärken'),
    ('learning', 'Lernen'),
    ('creativity', 'Kreativität'),
    ('curiosity', 'Neugierde'),
    ('education', 'Bildung'),
    ('happiness', 'Glücklichkeit'),
)


class HighlightListItem(CMSPlugin):
    icon = IconField(verbose_name='Icon', null=True, blank=True)
    animated_icon = models.CharField(verbose_name='Animiertes Icon', max_length=256, choices=ANIMATED_ICON_CHOICES,
                                     null=True, blank=True)
    title = HTMLField(verbose_name='Titel', configuration='CKEDITOR_SETTINGS_HIGHLIGHT_LIST_ITEM_TITLE')
    text = HTMLField(verbose_name='Text', configuration='CKEDITOR_SETTINGS_HIGHLIGHT_LIST_ITEM_TEXT')

    class Meta(CMSPlugin.Meta):
        verbose_name = 'Highlight List Item'
        verbose_name_plural = 'Highlight List Items'

    def __str__(self):
        return self.title


class ReferenceListItem(CMSPlugin):
    vimeo_id = models.IntegerField(verbose_name='Vimeo ID', help_text='e.g. https://vimeo.com/<b>131766159</b>')
    thumbnail = CroppableFilerImageField(verbose_name='Vorschaubild')
    cropping = ImageRatioField('thumbnail', '1000x1000', free_crop=True)

    class Meta(CMSPlugin.Meta):
        verbose_name = 'Reference List Item'
        verbose_name_plural = 'Reference List Items'

    def __str__(self):
        return str(self.vimeo_id)


class SocialMediaList(CMSPlugin):
    class Meta(CMSPlugin.Meta):
        verbose_name = 'Social Media List'
        verbose_name_plural = 'Social Media Lists'

    def copy_relations(self, oldinstance):
        self.social_media_list_items.all().delete()

        for social_media_list_item in oldinstance.social_media_list_items.all():
            social_media_list_item.pk = None
            social_media_list_item.social_media_list = self
            social_media_list_item.save()


class SocialMediaListItem(models.Model):
    social_media_list = models.ForeignKey(SocialMediaList, verbose_name='Social Media List',
                                          related_name='social_media_list_items')
    icon = IconField(verbose_name='Icon')
    url = models.URLField(verbose_name='URL')
    ordering = models.IntegerField(verbose_name='Sortierung', default=5)

    class Meta:
        ordering = ['ordering']


class Timetable(CMSPlugin):
    start_image = CroppableFilerImageField(verbose_name='Startbild')
    cropping = ImageRatioField('start_image', '1000x1000', free_crop=True)
    introduction = HTMLField(verbose_name='Intro', configuration='CKEDITOR_SETTINGS_INPUT')
    outro = HTMLField(verbose_name='Outro', configuration='CKEDITOR_SETTINGS_INPUT', blank=True, null=True)
    cta = HTMLField(verbose_name='CTA', configuration='CKEDITOR_SETTINGS_INPUT', blank=True, null=True)
    video_vimeo_id = models.IntegerField(verbose_name='Video Vimeo ID', blank=True, null=True, help_text='e.g. https://vimeo.com/<b>131766159</b>')
    video_cta = HTMLField(verbose_name='Video CTA', configuration='CKEDITOR_SETTINGS_INPUT', blank=True, null=True)

    class Meta(CMSPlugin.Meta):
        verbose_name = 'Timetable'
        verbose_name_plural = 'Timetables'

    def __str__(self):
        return self.introduction


class TimetableItem(CMSPlugin):
    time = models.TimeField(verbose_name='Zeit')
    image = CroppableFilerImageField(verbose_name='Bild')
    cropping = ImageRatioField('image', '1000x1000', free_crop=True)
    title = HTMLField(verbose_name='Titel', configuration='CKEDITOR_SETTINGS_TIMETABLE_ITEM_TITLE')
    text = HTMLField(verbose_name='Text', configuration='CKEDITOR_SETTINGS_TIMETABLE_ITEM_TEXT')

    class Meta(CMSPlugin.Meta):
        verbose_name = 'Timetable Item'
        verbose_name_plural = 'Timetables Items'

    def __str__(self):
        return '%s | %s' % (self.time, self.title)


class Partner(CMSPlugin):
    logo = CroppableFilerImageField(verbose_name='Bild')
    cropping = ImageRatioField('logo', '1000x1000', free_crop=True)
    name = HTMLField(verbose_name='Titel', configuration='CKEDITOR_SETTINGS_SLIDER_TITLE')
    text = HTMLField(verbose_name='Text', configuration='CKEDITOR_SETTINGS_SLIDER_TEXT', blank=True, null=True)
    url = models.URLField(verbose_name='URL', blank=True, null=True)

    class Meta(CMSPlugin.Meta):
        verbose_name = 'Partner'
        verbose_name_plural = 'Partner'

    def __str__(self):
        return self.name


class Image(without(AbstractPicture, 'picture')):
    picture = CroppableFilerImageField(verbose_name='Bild', blank=True, null=True, on_delete=models.SET_NULL,
                                       related_name='+')
    cropping = ImageRatioField('picture', '1000x1000', free_crop=True)


class TitleList(CMSPlugin):
    class Meta(CMSPlugin.Meta):
        verbose_name = 'Title List'
        verbose_name_plural = 'Title Lists'

    def copy_relations(self, oldinstance):
        self.title_list_items.all().delete()

        for title_list_item in oldinstance.title_list_items.all():
            title_list_item.pk = None
            title_list_item.title_list = self
            title_list_item.save()


class TitleListItem(models.Model):
    title_list = models.ForeignKey(TitleList, verbose_name='Title List', related_name='title_list_items')
    title = models.CharField(verbose_name='Title', max_length=256, null=True, blank=True)
    text = models.TextField(verbose_name='Text')
    ordering = models.IntegerField(verbose_name='Sortierung', default=5)

    class Meta:
        ordering = ['ordering']


class Gallery(CMSPlugin):
    folder = FilerFolderField(verbose_name='Ordner')

    class Meta(CMSPlugin.Meta):
        verbose_name = 'Gallerie'
        verbose_name_plural = 'Gallerien'

    def __str__(self):
        return self.folder.name

    @property
    def files(self):
        return self.folder.files.all().order_by('name')
