# Path: invenio_communities/views/communities.py
# @pass_community
# @pass_community_logo
# def communities_detail(community=None, logo=None, pid_value=None):
#     """Community detail page."""
#     return render_template(
#         "invenio_communities/details/index.html",
#         community=community.to_dict(), # TODO: use serializer
#         logo=logo.to_dict() if logo else None,
#         # TODO: inject this from a vocabulary in the community
#         types={
#             "organization": "Organization",
#             "event": "Event",
#             "topic": "Topic",
#             "project": "Project"
#         },
#         # Pass permissions so we can disable partially UI components
#         # e.g Settings tab
#         permissions=community.has_permissions_to(['update']),
#         active_menu_tab="search"
#     )
# 
# def communities_frontpage():
#     """Communities index page."""
#     return render_template(
#         "invenio_communities/frontpage.html",
#     )
# 
# @login_required
# def communities_new():
#     """Communities creation page."""
#     return render_template(
#         "invenio_communities/new.html",
#         form_config=dict(
#             access=dict(
#                 visibilty=[
#                     {
#                         'text': 'Public',
#                         'value': 'public',
#                         'icon': 'group',
#                         'helpText': 'Your community is publicly accessible ' \
#                                     'and shows up in search results.'
#                     },
#                     {
#                         'text': 'Restricted',
#                         'value': 'restricted',
#                         'icon': 'lock',
#                         'helpText': 'Your community is restricted to users ' \
#                                     'with access.'
#                     }
#                 ]
#             ),
#             SITE_UI_URL=current_app.config["SITE_UI_URL"]
#         ),
#     )
# 
# def communities_search():
#     """Communities search page."""
#     return render_template(
#         "invenio_communities/search.html",
#     )
# 
# @pass_community
# @pass_community_logo
# @require_community_owner
# def communities_settings(community=None, logo=None, pid_value=None):
#     """Community settings/profile page."""
#     return render_template(
#         "invenio_communities/details/settings/profile.html",
#         community=community.to_dict(), # TODO: use serializer,
#         logo=logo.to_dict() if logo else None,
#         # TODO: inject this from a vocabulary in the community
#         types={
#             "organization": "Organization",
#             "event": "Event",
#             "topic": "Topic",
#             "project": "Project"
#         },
#         # Pass permissions so we can disable partially UI components
#         # e.g Settings tab
#         permissions=community.has_permissions_to(['update']),
#         active_menu_tab="settings"
#     )
# 
# @require_community_owner
# @pass_community
# @pass_community_logo
# def communities_settings_privileges(community=None, logo=None, pid_value=None):
#     """Community settings/privileges page."""
#     return render_template(
#         "invenio_communities/details/settings/privileges.html",
#         community=community.to_dict(), # TODO: use serializer,
#         form_config=dict(
#             access=dict(
#                 visibilty=[
#                     {
#                         'text': 'Public',
#                         'value': 'public',
#                         'icon': 'group',
#                         'helpText': 'Your community is publicly accessible ' \
#                                     'and shows up in search results.'
#                     },
#                     {
#                         'text': 'Restricted',
#                         'value': 'restricted',
#                         'icon': 'lock',
#                         'helpText': 'Your community is restricted to users ' \
#                                     'with access.'
#                     }
#                 ]
#             ),
#         ),
#         # TODO: inject this from a vocabulary in the community
#         types={
#             "organization": "Organization",
#             "event": "Event",
#             "topic": "Topic",
#             "project": "Project"
#         },
#         # Pass permissions so we can disable partially UI components
#         # e.g Settings tab
#         permissions=community.has_permissions_to(['update']),
#         active_menu_tab="settings",
#         logo=logo.to_dict() if logo else None
#     )


from flask import Blueprint, current_app, render_template
from flask_login import current_user
from flask_menu import current_menu
from invenio_pidstore.errors import PIDDeletedError, PIDDoesNotExistError
from invenio_records_resources.services.errors import PermissionDeniedError
from .communities import communities_detail, communities_frontpage, \
    communities_new, communities_search, communities_settings, \
    communities_settings_privileges
    return render_template("invenio_communities/tombstone.html"), 410


def record_permission_denied_error(error):
    """Handle permission denier error on record views."""
    if not current_user.is_authenticated:
        # trigger the flask-login unauthorized handler
        return current_app.login_manager.unauthorized()
    return render_template(current_app.config['THEME_403_TEMPLATE']), 403


#
# Registration
#
def create_ui_blueprint(app):
    """Register blueprint routes on app."""
    routes = app.config.get("COMMUNITIES_ROUTES")

    blueprint = Blueprint(
        "invenio_communities",
        __name__,
        template_folder="../templates",
        static_folder='../static'
    )

    # control blueprint endpoints registration
    if app.config["COMMUNITIES_ENABLED"]:
        # Communities URL rules
        blueprint.add_url_rule(
            routes["frontpage"],

# Path: invenio_communities/views/communities.py
# @pass_community
# @pass_community_logo
# def communities_detail(community=None, logo=None, pid_value=None):
#     """Community detail page."""
#     return render_template(
#         "invenio_communities/details/index.html",
#         community=community.to_dict(), # TODO: use serializer
#         logo=logo.to_dict() if logo else None,
#         # TODO: inject this from a vocabulary in the community
#         types={
#             "organization": "Organization",
#             "event": "Event",
#             "topic": "Topic",
#             "project": "Project"
#         },
#         # Pass permissions so we can disable partially UI components
#         # e.g Settings tab
#         permissions=community.has_permissions_to(['update']),
#         active_menu_tab="search"
#     )
# 
# def communities_frontpage():
#     """Communities index page."""
#     return render_template(
#         "invenio_communities/frontpage.html",
#     )
# 
# @login_required
# def communities_new():
#     """Communities creation page."""
#     return render_template(
#         "invenio_communities/new.html",
#         form_config=dict(
#             access=dict(
#                 visibilty=[
#                     {
#                         'text': 'Public',
#                         'value': 'public',
#                         'icon': 'group',
#                         'helpText': 'Your community is publicly accessible ' \
#                                     'and shows up in search results.'
#                     },
#                     {
#                         'text': 'Restricted',
#                         'value': 'restricted',
#                         'icon': 'lock',
#                         'helpText': 'Your community is restricted to users ' \
#                                     'with access.'
#                     }
#                 ]
#             ),
#             SITE_UI_URL=current_app.config["SITE_UI_URL"]
#         ),
#     )
# 
# def communities_search():
#     """Communities search page."""
#     return render_template(
#         "invenio_communities/search.html",
#     )
# 
# @pass_community
# @pass_community_logo
# @require_community_owner
# def communities_settings(community=None, logo=None, pid_value=None):
#     """Community settings/profile page."""
#     return render_template(
#         "invenio_communities/details/settings/profile.html",
#         community=community.to_dict(), # TODO: use serializer,
#         logo=logo.to_dict() if logo else None,
#         # TODO: inject this from a vocabulary in the community
#         types={
#             "organization": "Organization",
#             "event": "Event",
#             "topic": "Topic",
#             "project": "Project"
#         },
#         # Pass permissions so we can disable partially UI components
#         # e.g Settings tab
#         permissions=community.has_permissions_to(['update']),
#         active_menu_tab="settings"
#     )
# 
# @require_community_owner
# @pass_community
# @pass_community_logo
# def communities_settings_privileges(community=None, logo=None, pid_value=None):
#     """Community settings/privileges page."""
#     return render_template(
#         "invenio_communities/details/settings/privileges.html",
#         community=community.to_dict(), # TODO: use serializer,
#         form_config=dict(
#             access=dict(
#                 visibilty=[
#                     {
#                         'text': 'Public',
#                         'value': 'public',
#                         'icon': 'group',
#                         'helpText': 'Your community is publicly accessible ' \
#                                     'and shows up in search results.'
#                     },
#                     {
#                         'text': 'Restricted',
#                         'value': 'restricted',
#                         'icon': 'lock',
#                         'helpText': 'Your community is restricted to users ' \
#                                     'with access.'
#                     }
#                 ]
#             ),
#         ),
#         # TODO: inject this from a vocabulary in the community
#         types={
#             "organization": "Organization",
#             "event": "Event",
#             "topic": "Topic",
#             "project": "Project"
#         },
#         # Pass permissions so we can disable partially UI components
#         # e.g Settings tab
#         permissions=community.has_permissions_to(['update']),
#         active_menu_tab="settings",
#         logo=logo.to_dict() if logo else None
#     )


# Path: invenio_communities/views/ui.py
from flask import Blueprint, current_app, render_template
from flask_login import current_user
from flask_menu import current_menu
from invenio_pidstore.errors import PIDDeletedError, PIDDoesNotExistError
from invenio_records_resources.services.errors import PermissionDeniedError
from .communities import communities_detail, communities_frontpage, \
    communities_new, communities_search, communities_settings, \
    communities_settings_privileges

    return render_template("invenio_communities/tombstone.html"), 410


def record_permission_denied_error(error):
    """Handle permission denier error on record views."""
    if not current_user.is_authenticated:
        # trigger the flask-login unauthorized handler
        return current_app.login_manager.unauthorized()
    return render_template(current_app.config['THEME_403_TEMPLATE']), 403


#
# Registration
#
def create_ui_blueprint(app):
    """Register blueprint routes on app."""
    routes = app.config.get("COMMUNITIES_ROUTES")

    blueprint = Blueprint(
        "invenio_communities",
        __name__,
        template_folder="../templates",
        static_folder='../static'
    )

    # control blueprint endpoints registration
    if app.config["COMMUNITIES_ENABLED"]:
        # Communities URL rules
        blueprint.add_url_rule(
            routes["frontpage"],

            view_func=communities_frontpage,