# Copyright 2012 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Courses module."""

__author__ = 'Pavel Simakov (psimakov@google.com)'

from common import resource
from controllers import assessments
from controllers import lessons
from controllers import utils
from models import content
from models import resources_display
from models import custom_modules
from models import roles
from tools import verify


All_LOCALES_PERMISSION = 'can_pick_all_locales'
All_LOCALES_DESCRIPTION = 'Can pick all locales, including unavailable ones.'

SEE_DRAFTS_PERMISSION = 'can_see_draft_content'
SEE_DRAFTS_DESCRIPTION = 'Can see lessons and assessments with draft status.'


custom_module = None


def can_pick_all_locales(app_context):
    return roles.Roles.is_user_allowed(
        app_context, custom_module, All_LOCALES_PERMISSION)


def can_see_drafts(app_context):
    return roles.Roles.is_user_allowed(
        app_context, custom_module, SEE_DRAFTS_PERMISSION)


def register_module():
    """Registers this module in the registry."""

    def on_module_enabled():
        roles.Roles.register_permissions(custom_module, permissions_callback)
        resource.Registry.register(resources_display.ResourceCourseSettings)
        resource.Registry.register(resources_display.ResourceUnit)
        resource.Registry.register(resources_display.ResourceAssessment)
        resource.Registry.register(resources_display.ResourceLink)
        resource.Registry.register(resources_display.ResourceLesson)
        resource.Registry.register(utils.ResourceHtmlHook)

    def permissions_callback(unused_app_context):
        return [
            roles.Permission(All_LOCALES_PERMISSION, All_LOCALES_DESCRIPTION),
            roles.Permission(SEE_DRAFTS_PERMISSION, SEE_DRAFTS_DESCRIPTION)
        ]

    # provide parser to verify
    verify.parse_content = content.parse_string_in_scope

    # setup routes
    courses_routes = [
        ('/', lessons.CourseHandler),
        ('/activity', lessons.UnitHandler),
        ('/answer', assessments.AnswerHandler),
        ('/assessment', lessons.AssessmentHandler),
        ('/course', lessons.CourseHandler),
        ('/forum', utils.ForumHandler),
        ('/preview', utils.PreviewHandler),
        ('/register', utils.RegisterHandler),
        ('/resources', utils.ResourcesHandler),
        ('/rest/locale', utils.StudentLocaleRESTHandler),
        ('/review', lessons.ReviewHandler),
        ('/reviewdashboard', lessons.ReviewDashboardHandler),
        ('/student/editstudent', utils.StudentEditStudentHandler),
        ('/student/settracks', utils.StudentSetTracksHandler),
        ('/student/home', utils.StudentProfileHandler),
        ('/student/unenroll', utils.StudentUnenrollHandler),
        ('/unit', lessons.UnitHandler)]

    global custom_module  # pylint: disable=global-statement
    custom_module = custom_modules.Module(
        'Course',
        'A set of pages for delivering an online course.',
        [], courses_routes,
        notify_module_enabled=on_module_enabled)
    return custom_module

[(41, 'models.roles.Roles.is_user_allowed', 'roles.Roles.is_user_allowed', False, 'from models import roles\n'), (46, 'models.roles.Roles.is_user_allowed', 'roles.Roles.is_user_allowed', False, 'from models import roles\n'), (92, 'models.custom_modules.Module', 'custom_modules.Module', False, 'from models import custom_modules\n'), (54, 'models.roles.Roles.register_permissions', 'roles.Roles.register_permissions', False, 'from models import roles\n'), (55, 'common.resource.Registry.register', 'resource.Registry.register', False, 'from common import resource\n'), (56, 'common.resource.Registry.register', 'resource.Registry.register', False, 'from common import resource\n'), (57, 'common.resource.Registry.register', 'resource.Registry.register', False, 'from common import resource\n'), (58, 'common.resource.Registry.register', 'resource.Registry.register', False, 'from common import resource\n'), (59, 'common.resource.Registry.register', 'resource.Registry.register', False, 'from common import resource\n'), (60, 'common.resource.Registry.register', 'resource.Registry.register', False, 'from common import resource\n'), (64, 'models.roles.Permission', 'roles.Permission', False, 'from models import roles\n'), (65, 'models.roles.Permission', 'roles.Permission', False, 'from models import roles\n')]