text: // Copyright 2014 The Oppia Authors. 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.
  
  /**
   * @fileoverview Data and controllers for the Oppia profile page.
   */
  
  oppia.controller('Profile', [
    '$scope', '$http', '$rootScope', 'oppiaDatetimeFormatter',
    'UrlInterpolationService',
    function($scope, $http, $rootScope, oppiaDatetimeFormatter,
      UrlInterpolationService) {
      var profileDataUrl = '/profilehandler/data/' + GLOBALS.PROFILE_USERNAME;
      var DEFAULT_PROFILE_PICTURE_URL = UrlInterpolationService.getStaticImageUrl(
        '/general/no_profile_picture.png');
  
      $scope.getLocaleDateString = function(millisSinceEpoch) {
        return oppiaDatetimeFormatter.getLocaleDateString(millisSinceEpoch);
      };
  
      $rootScope.loadingMessage = 'Loading';
      $http.get(profileDataUrl).then(function(response) {
        var data = response.data;
        $rootScope.loadingMessage = '';
        $scope.username = {
          title: 'Username',
          value: data.profile_username,
          helpText: (data.profile_username)
        };
        $scope.usernameIsLong = data.profile_username.length > 16;
        $scope.userBio = data.user_bio;
        $scope.userDisplayedStatistics = [{
          title: 'Impact',
          value: data.user_impact_score,
          helpText: (
            'A rough measure of the impact of explorations created by this ' +
            'user. Better ratings and more playthroughs improve this score.')
        }, {
          title: 'Created',
          value: data.created_exp_summary_dicts.length
        }, {
          title: 'Edited',
          value: data.edited_exp_summary_dicts.length
        }];
  
        $scope.userEditedExplorations = data.edited_exp_summary_dicts.sort(
            function(exploration1, exploration2) {
          if (exploration1.ratings > exploration2.ratings) {
            return 1;
          } else if (exploration1.ratings === exploration2.ratings) {
            if (exploration1.playthroughs > exploration2.playthroughs) {
              return 1;
            } else if (exploration1.playthroughs > exploration2.playthroughs) {
              return 0;
            } else {
              return -1;
            }
          } else {
            return -1;
          }
        });
  
        $scope.currentPageNumber = 0;
        $scope.PAGE_SIZE = 6;
        $scope.startingExplorationNumber = 1;
        $scope.endingExplorationNumber = 6;
        $scope.Math = window.Math;
        $scope.profileIsOfCurrentUser = data.profile_is_of_current_user;
  
        $scope.goToPreviousPage = function() {
          if ($scope.currentPageNumber === 0) {
            console.log('Error: cannot decrement page');
          } else {
            $scope.currentPageNumber--;
          }
        };
        $scope.goToNextPage = function() {
          if ($scope.currentPageNumber * $scope.PAGE_SIZE >= (
              data.edited_exp_summary_dicts.length)) {
            console.log('Error: Cannot increment page');
          } else {
            $scope.currentPageNumber++;
          }
        };
  
        $scope.getExplorationsToDisplay = function() {
          $scope.explorationsOnPage = [];
          if ($scope.userEditedExplorations.length === 0) {
            return $scope.explorationsOnPage;
          }
          $scope.explorationIndexStart = (
            $scope.currentPageNumber * $scope.PAGE_SIZE);
          $scope.explorationIndexEnd = (
            $scope.explorationIndexStart + $scope.PAGE_SIZE - 1);
          for (var ind = $scope.explorationIndexStart;
              ind <= $scope.explorationIndexEnd; ind++) {
            $scope.explorationsOnPage.push($scope.userEditedExplorations[ind]);
            if (ind === $scope.userEditedExplorations.length - 1) {
              break;
            }
          }
          return $scope.explorationsOnPage;
        };
  
        $scope.numUserPortfolioExplorations = (
          data.edited_exp_summary_dicts.length);
        $scope.subjectInterests = data.subject_interests;
        $scope.firstContributionMsec = data.first_contribution_msec;
        $scope.profilePictureDataUrl = (
          data.profile_picture_data_url || DEFAULT_PROFILE_PICTURE_URL);
        $rootScope.loadingMessage = '';
      });
    }
  ]);
  