# coding=utf-8
"""
Forum views
"""
from __future__ import absolute_import, print_function, unicode_literals

from datetime import datetime

from flask import g, render_template

from abilian.i18n import _l
from abilian.web import url_for, views
from abilian.web.action import ButtonAction

from ..communities.blueprint import Blueprint
from ..communities.views import default_view_kw
from .forms import EventForm
from .models import Event

blueprint = Blueprint("calendar",
                      __name__,
                      url_prefix="/calendar",
                      template_folder="templates")
route = blueprint.route


@route('/')
def index():
    events = Event.query \
        .filter(Event.end > datetime.now()) \
        .order_by(Event.start) \
        .all()
    ctx = {'upcoming_events': events,}
    return render_template('calendar/index.html', **ctx)


class BaseEventView(object):
    Model = Event
    Form = EventForm
    pk = 'event_id'
    base_template = 'community/_base.html'

    def index_url(self):
        return url_for(".index", community_id=g.community.slug)

    def view_url(self):
        return url_for(self.obj)


class EventView(BaseEventView, views.ObjectView):
    methods = ['GET', 'HEAD']
    Form = EventForm
    template = 'calendar/event.html'

    @property
    def template_kwargs(self):
        kw = super(EventView, self).template_kwargs
        kw['event'] = self.obj
        return kw


event_view = EventView.as_view(b'event')
views.default_view(blueprint,
                   Event, 'event_id',
                   kw_func=default_view_kw)(event_view)
route('/<int:event_id>/')(event_view)


class EventCreateView(BaseEventView, views.ObjectCreate):
    POST_BUTTON = ButtonAction('form',
                               'create',
                               btn_class='primary',
                               title=_l('Post this event'))

    title = _l("New event")

    def init_object(self, args, kwargs):
        args, kwargs = super(EventCreateView, self).init_object(args, kwargs)
        self.event = self.obj
        return args, kwargs

    def after_populate_obj(self):
        if self.obj.community is None:
            self.obj.community = g.community._model

    @property
    def activity_target(self):
        return self.event.community

    def get_form_buttons(self, *args, **kwargs):
        return [self.POST_BUTTON, views.object.CANCEL_BUTTON]

    @property
    def activity_target(self):
        return self.obj.community


event_create_view = EventCreateView.as_view(b'new_event', view_endpoint='.event')
route('/new_event/')(event_create_view)


class EventEditView(BaseEventView, views.ObjectEdit):
    POST_BUTTON = ButtonAction('form',
                               'create',
                               btn_class='primary',
                               title=_l('Post this event'))

    title = _l("Edit event")


event_edit_view = EventEditView.as_view(b'event_edit', view_endpoint='.event')
route('/<int:event_id>/edit')(event_edit_view)
