

from __future__ import unicode_literals

from flask import render_template, session

from fossir.core import signals
from fossir.core.db import db
from fossir.core.logger import Logger
from fossir.core.roles import ManagementRole
from fossir.modules.events import Event
from fossir.modules.events.features.base import EventFeature
from fossir.modules.events.layout.util import MenuEntryData
from fossir.modules.events.surveys.models.submissions import SurveySubmission
from fossir.modules.events.surveys.util import query_active_surveys
from fossir.util.i18n import _
from fossir.web.flask.templating import template_hook
from fossir.web.flask.util import url_for
from fossir.web.menu import SideMenuItem


logger = Logger.get('events.survey')


@signals.users.merged.connect
def _merge_users(target, source, **kwargs):
    from fossir.modules.events.surveys.models.submissions import SurveySubmission
    SurveySubmission.find(user_id=source.id).update({SurveySubmission.user_id: target.id})


@signals.menu.items.connect_via('event-management-sidemenu')
def _extend_event_management_menu(sender, event, **kwargs):
    if not event.has_feature('surveys') or not event.can_manage(session.user, 'surveys'):
        return
    return SideMenuItem('surveys', _('Surveys'), url_for('surveys.manage_survey_list', event), section='organization')


@signals.event.sidemenu.connect
def _extend_event_menu(sender, **kwargs):
    from fossir.modules.events.surveys.models.surveys import Survey

    def _visible(event):
        return event.has_feature('surveys') and query_active_surveys(event).has_rows()

    return MenuEntryData(_('Surveys'), 'surveys', 'surveys.display_survey_list', position=12, visible=_visible)


def _get_active_surveys(event):
    if not event.has_feature('surveys'):
        return []
    from fossir.modules.events.surveys.models.surveys import Survey
    return (query_active_surveys(event)
            .order_by(db.func.lower(Survey.title))
            .all())


@template_hook('event-header')
def _inject_event_header(event, **kwargs):
    from fossir.modules.events.surveys.util import was_survey_submitted
    surveys = _get_active_surveys(event)
    if surveys:
        return render_template('events/surveys/display/event_header.html', surveys=surveys,
                               was_survey_submitted=was_survey_submitted)


@template_hook('conference-home-info')
def _inject_survey_announcement(event, **kwargs):
    from fossir.modules.events.surveys.util import was_survey_submitted
    surveys = _get_active_surveys(event)
    if surveys:
        return render_template('events/surveys/display/conference_home.html', surveys=surveys, event=event,
                               was_survey_submitted=was_survey_submitted)


@signals.event.get_feature_definitions.connect
def _get_feature_definitions(sender, **kwargs):
    return SurveysFeature


@signals.acl.get_management_roles.connect_via(Event)
def _get_management_roles(sender, **kwargs):
    return SurveysRole


@signals.import_tasks.connect
def _import_tasks(sender, **kwargs):
    import fossir.modules.events.surveys.tasks


class SurveysFeature(EventFeature):
    name = 'surveys'
    friendly_name = _('Surveys')
    description = _('Gives event managers the opportunity to create surveys.')

    @classmethod
    def is_default_for_event(cls, event):
        return True


class SurveysRole(ManagementRole):
    name = 'surveys'
    friendly_name = _('Surveys')
    description = _('Grants management access to surveys.')


@signals.get_placeholders.connect_via('survey-link-email')
def _get_placeholders(sender, event, survey, **kwargs):
    from fossir.modules.events.surveys import placeholders
    yield placeholders.EventTitlePlaceholder
    yield placeholders.SurveyTitlePlaceholder
    yield placeholders.SurveyLinkPlaceholder
