# -*- coding: utf-8 -*-
from aldryn_forms.cms_plugins import FormPlugin as _FormPlugin
from cms.models.pluginmodel import CMSPlugin
from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from django.conf import settings
from djangocms_picture.cms_plugins import PicturePlugin as _PicturePlugin
from djangocms_text_ckeditor.cms_plugins import TextPlugin as _TextPlugin
from django.contrib import admin
from mailchimp3 import MailChimp

from project.forms import NewsletterSubscriptionForm
from project.models import Section, Quote, SliderItem, SectionText, Video, DownloadSection, DownloadSectionFolder, \
    TextSliderItem, HighlightListItem, ReferenceListItem, SocialMediaList, SocialMediaListItem, Timetable, \
    TimetableItem, Partner, HighlightList, Image, TitleListItem, TitleList, IntroImage, Gallery


@plugin_pool.register_plugin
class SectionPlugin(CMSPluginBase):
    model = Section
    module = 'Content'
    name = 'Section'
    render_template = 'project/plugins/content/section.html'
    allow_children = True
    child_classes = ['QuotePlugin', 'SliderPlugin', 'SpacerPlugin', 'SectionTitlePlugin', 'TitleListPlugin',
                     'SectionTextPlugin', 'VideoPlugin', 'DownloadSectionPlugin', 'TextSliderPlugin',
                     'HighlightListPlugin', 'ReferenceListPlugin', 'FormPlugin', 'PicturePlugin', 'SubPageListPlugin',
                     'PartnerPlugin', 'NewsletterSubscriptionPlugin', 'NewsletterArchivePlugin',
                     'SocialMediaListPlugin', 'GalleryPlugin']


@plugin_pool.register_plugin
class SectionTitlePlugin(CMSPluginBase):
    model = CMSPlugin
    module = 'Content'
    name = 'Section Title'
    render_template = 'project/plugins/content/section_title.html'


@plugin_pool.register_plugin
class IntroImagePlugin(CMSPluginBase):
    model = IntroImage
    module = 'Intro'
    name = 'Intro Image'
    render_template = 'project/plugins/content/intro_image.html'


plugin_pool.unregister_plugin(_TextPlugin)


@plugin_pool.register_plugin
class TextPlugin(_TextPlugin):
    child_classes = ['LinkPlugin']


@plugin_pool.register_plugin
class SectionTextPlugin(CMSPluginBase):
    model = SectionText
    module = 'Content'
    name = 'Section Text'
    render_template = 'project/plugins/content/section_text.html'
    allow_children = True
    child_classes = ['TextPlugin']


@plugin_pool.register_plugin
class VideoPlugin(CMSPluginBase):
    model = Video
    module = 'Content'
    name = 'Video'
    render_template = 'project/plugins/content/video.html'


@plugin_pool.register_plugin
class SliderPlugin(CMSPluginBase):
    model = CMSPlugin
    module = 'Content'
    name = 'Slider'
    render_template = 'project/plugins/content/slider.html'
    allow_children = True
    child_classes = ['SliderItemPlugin']


@plugin_pool.register_plugin
class SliderItemPlugin(CMSPluginBase):
    model = SliderItem
    module = 'Slider'
    name = 'Slider Item'
    render_template = 'project/plugins/content/slider_item.html'
    filter_horizontal = ['qualifications']


@plugin_pool.register_plugin
class QuotePlugin(CMSPluginBase):
    model = Quote
    module = 'Content'
    name = 'Quote'
    render_template = 'project/plugins/content/quote.html'


class DownloadSectionFolderInlineAdmin(admin.TabularInline):
    model = DownloadSectionFolder
    extra = 0


@plugin_pool.register_plugin
class DownloadSectionPlugin(CMSPluginBase):
    model = DownloadSection
    module = 'Content'
    name = 'Download Section'
    render_template = 'project/plugins/content/download_section.html'
    inlines = [DownloadSectionFolderInlineAdmin]


@plugin_pool.register_plugin
class SpacerPlugin(CMSPluginBase):
    model = CMSPlugin
    module = 'Content'
    name = 'Spacer'
    render_template = 'project/plugins/content/spacer.html'


@plugin_pool.register_plugin
class TextSliderPlugin(CMSPluginBase):
    model = CMSPlugin
    module = 'Content'
    name = 'Text Slider'
    render_template = 'project/plugins/content/text_slider.html'
    allow_children = True
    child_classes = ['TextSliderItemPlugin']


@plugin_pool.register_plugin
class TextSliderItemPlugin(CMSPluginBase):
    model = TextSliderItem
    module = 'Slider'
    name = 'Text Slider Item'
    render_template = 'project/plugins/content/text_slider_item.html'


@plugin_pool.register_plugin
class HighlightListPlugin(CMSPluginBase):
    model = HighlightList
    module = 'Content'
    name = 'Highlight List'
    render_template = 'project/plugins/content/highlight_list.html'
    allow_children = True
    child_classes = ['HighlightListItemPlugin', 'SectionTitlePlugin']


@plugin_pool.register_plugin
class HighlightListItemPlugin(CMSPluginBase):
    model = HighlightListItem
    module = 'Slider'
    name = 'Highlight List Item'
    render_template = 'project/plugins/content/highlight_list_item.html'


@plugin_pool.register_plugin
class ReferenceListPlugin(CMSPluginBase):
    model = CMSPlugin
    module = 'Content'
    name = 'Reference List'
    render_template = 'project/plugins/content/reference_list.html'
    allow_children = True
    child_classes = ['ReferenceListItemPlugin']


@plugin_pool.register_plugin
class ReferenceListItemPlugin(CMSPluginBase):
    model = ReferenceListItem
    module = 'Slider'
    name = 'Highlight List Item'
    render_template = 'project/plugins/content/reference_list_item.html'


plugin_pool.unregister_plugin(_PicturePlugin)


@plugin_pool.register_plugin
class PicturePlugin(_PicturePlugin):
    module = 'Content'
    name = 'Image'
    model = Image

    fieldsets = [
        (None, {
            'fields': (
                'picture',
                'cropping',
                ('width', 'height'),
                'caption_text',
            )
        }),
    ]


plugin_pool.unregister_plugin(_FormPlugin)


@plugin_pool.register_plugin
class FormPlugin(_FormPlugin):
    module = 'Content'
    name = 'Form'
    child_classes = ['TextField', 'TextAreaField', 'EmailField', 'RadioSelectField', 'MultipleSelectField',
                     'SubmitButton']


class SocialMediaListItemInlineAdmin(admin.TabularInline):
    model = SocialMediaListItem
    extra = 0


@plugin_pool.register_plugin
class SocialMediaListPlugin(CMSPluginBase):
    model = SocialMediaList
    module = 'Content'
    name = 'Social Media List'
    render_template = 'project/plugins/social/social_media_list.html'
    inlines = [SocialMediaListItemInlineAdmin]


@plugin_pool.register_plugin
class JourneyCalculatorPlugin(CMSPluginBase):
    module = 'Content'
    name = 'Anfahrtszeit Rechner'
    render_template = 'project/plugins/contact/journey_calculator.html'
    allow_children = True
    child_classes = ['TextPlugin']


@plugin_pool.register_plugin
class TimetablePlugin(CMSPluginBase):
    model = Timetable
    module = 'Timetable'
    name = 'Timetable'
    render_template = 'project/plugins/timetable/timetable.html'
    allow_children = True
    child_classes = ['TimetableItemPlugin']


@plugin_pool.register_plugin
class TimetableItemPlugin(CMSPluginBase):
    model = TimetableItem
    module = 'Timetable'
    name = 'Timetable Item'
    render_template = 'project/plugins/timetable/timetable_item.html'


@plugin_pool.register_plugin
class SubPageListPlugin(CMSPluginBase):
    model = CMSPlugin
    module = 'Content'
    name = 'Subpage List'
    render_template = 'project/plugins/content/subpage_list.html'


@plugin_pool.register_plugin
class ContactFormButtonPlugin(CMSPluginBase):
    model = CMSPlugin
    module = 'Content'
    name = 'Contact Form Button'
    render_template = 'project/plugins/content/contact_form_button.html'


@plugin_pool.register_plugin
class PartnerPlugin(CMSPluginBase):
    model = Partner
    module = 'Content'
    name = 'Partner'
    render_template = 'project/plugins/content/partner.html'


@plugin_pool.register_plugin
class NewsletterSubscriptionPlugin(CMSPluginBase):
    module = 'Content'
    name = 'Newsletter Subscription'
    render_template = 'project/newsletter/subscription_form.html'

    def render(self, context, instance, placeholder):
        context = super(NewsletterSubscriptionPlugin, self).render(context, instance, placeholder)
        context['form'] = NewsletterSubscriptionForm
        return context


@plugin_pool.register_plugin
class NewsletterArchivePlugin(CMSPluginBase):
    module = 'Content'
    name = 'Newsletter Archive'
    render_template = 'project/newsletter/archive.html'

    def render(self, context, instance, placeholder):
        context = super(NewsletterArchivePlugin, self).render(context, instance, placeholder)
        client = MailChimp(settings.MAILCHIMP_API_KEY, settings.MAILCHIMP_USERNAME)

        campaigns = []
        for raw_campaign in client.campaigns.all(get_all=True)['campaigns']:
            if raw_campaign['status'] == 'sent':
                campaign = {
                    'title': raw_campaign['settings']['title'],
                    'text': raw_campaign['settings']['subject_line'],
                    'url': raw_campaign['archive_url'],
                }
                campaigns.append(campaign)

        context['campaigns'] = campaigns
        return context


class TitleListItemInlineAdmin(admin.TabularInline):
    model = TitleListItem
    extra = 0


@plugin_pool.register_plugin
class TitleListPlugin(CMSPluginBase):
    model = TitleList
    module = 'Content'
    name = 'Title List'
    render_template = 'project/plugins/content/title_list.html'
    inlines = [TitleListItemInlineAdmin]


@plugin_pool.register_plugin
class GalleryPlugin(CMSPluginBase):
    model = Gallery
    module = 'Content'
    name = 'Gallery'
    render_template = 'project/plugins/content/gallery.html'
