# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd

"""A class to display the project summary page."""
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import

import logging

from businesslogic import work_env
from framework import permissions
from framework import servlet
from project import project_helpers
from project import project_views


class ProjectSummary(servlet.Servlet):
  """Page to show brief project description and process documentation."""

  _PAGE_TEMPLATE = 'project/project-summary-page.ezt'
  _MAIN_TAB_MODE = servlet.Servlet.MAIN_TAB_PROCESS

  def GatherPageData(self, mr):
    """Build up a dictionary of data values to use when rendering the page."""

    with mr.profiler.Phase('getting project star count'):
      num_stars = self.services.project_star.CountItemStars(
          mr.cnxn, mr.project_id)
      plural = '' if num_stars == 1 else 's'

    page_data = {
        'admin_tab_mode': self.PROCESS_TAB_SUMMARY,
        'formatted_project_description': mr.project.description,
        'access_level': project_views.ProjectAccessView(mr.project.access),
        'num_stars': num_stars,
        'plural': plural,
        'home_page': mr.project.home_page,
        'docs_url': mr.project.docs_url,
        'source_url': mr.project.source_url,
    }

    return page_data

  def GatherHelpData(self, mr, page_data):
    """Return a dict of values to drive on-page user help.

    Args:
      mr: common information parsed from the HTTP request.
      page_data: Dictionary of base and page template data.

    Returns:
      A dict of values to drive on-page user help, to be added to page_data.
    """
    help_data = super(ProjectSummary, self).GatherHelpData(mr, page_data)
    with work_env.WorkEnv(mr, self.services) as we:
      userprefs = we.GetUserPrefs(mr.auth.user_id)
    dismissed = [
        pv.name for pv in userprefs.prefs if pv.value == 'true']
    project = mr.project

    # Cue cards for project owners.
    if self.CheckPerm(mr, permissions.EDIT_PROJECT):
      if ('document_team_duties' not in dismissed and
          len(project_helpers.AllProjectMembers(project)) > 1 and
          not self.services.project.GetProjectCommitments(
              mr.cnxn, mr.project_id).commitments):
        help_data['cue'] = 'document_team_duties'

    return help_data
