# coding=utf-8

from __future__ import absolute_import, print_function

from flask import current_app, g, request, url_for
from flask_login import current_user

from abilian.i18n import _l
from abilian.sbe.apps.communities.security import is_manager
from abilian.services.security import Admin, Manager
from abilian.web.action import Action, FAIcon, ModalActionMixin, actions


class ForumAction(Action):

    def is_current(self):
        return request.path == self.url()

    def url(self, context=None):
        if self._url or self.endpoint:
            return super(ForumAction, self).url(context=context)

        return url_for("." + self.name, community_id=g.community.slug)


class ThreadAction(ForumAction):

    def pre_condition(self, context):
        thread = actions.context.get('object')
        return not not thread


def is_admin(context):
    svc = current_app.services['security']
    return svc.has_role(current_user, Admin, object=context.get('object'))


def is_in_thread(context):
    thread = context.get('object')
    return not thread


def is_closed(context):
    thread = context.get('object')
    return thread.closed


def not_closed(context):
    return not is_closed(context)


class ForumModalAction(ModalActionMixin, ThreadAction):
    pass


_close_template_action = u'''
<form method="POST" action="{{ url }}" encoding="multipart/form-data">
  {{ csrf.field() }}
  <button type="submit" class="btn btn-link" name="action"
          value="{{ action.name}}">
    {%- if action.icon %}{{ action.icon }} {% endif %}
    {{ action.title }}
  </button>
</form>
'''

_actions = (
    ForumAction(
        'forum:global', 'index', _l(u'Recent conversations')),
    ForumAction(
        'forum:global', 'index', _l(u'Top'), url='#'),
    ForumAction('forum:global', 'archives', _l(u'Archives')),
    ForumAction(
        'forum:global',
        'attachments',
        _l(u'Attachments'),
        condition=is_in_thread),
    ForumAction(
        'forum:global', 'new_thread', _l(u'New conversation'), icon='plus'),
    ForumModalAction(
        'forum:thread',
        'delete',
        _l(u'Delete'),
        condition=lambda ctx: is_admin(ctx) and not_closed(ctx),
        url='#modal-delete',
        icon='trash'),
    #
    ThreadAction(
        'forum:thread',
        'close',
        _l('Close thread'),
        url='close',
        template_string=_close_template_action,
        condition=lambda ctx: is_admin(ctx) and not_closed(ctx),
        icon=FAIcon('lock')),
    ThreadAction(
        'forum:thread',
        'reopen',
        _l('Re-open thread'),
        url='close',
        template_string=_close_template_action,
        condition=lambda ctx: is_admin(ctx) and is_closed(ctx),
        icon=FAIcon('unlock')),
    ThreadAction(
        'forum:thread',
        'attachments',
        _l(u'Attachments'),
        url='attachments',
        icon='file'),)


def register_actions(state):
    if not actions.installed(state.app):
        return
    with state.app.app_context():
        actions.register(*_actions)

@@ -468,16 +468,331 @@
 .url()%0A%0A
+    def is_filtred(self):%0A        filter_keys = %5B%22today%22,%22month%22,%22year%22,%22week%22%5D%0A        current_url = request.path.split(%22/%22)%0A        filter = current_url%5B-1%5D.strip()%0A%0A        if filter not in filter_keys:%0A            return False%0A%0A        if self.url() == %22#filter%22:%0A            return True%0A%0A        return False%0A%0A
     def 
@@ -2040,32 +2040,48 @@
 :global', 'index
+/%3Cstring:filter%3E
 ', _l(u'Top'), u
@@ -2085,16 +2085,22 @@
 , url='#
+filter
 '),%0A    

add action is_filtred