'use strict';

angular.module('app')
    .controller('rgiAnswerRawListCtrl', function (
        _,
        $scope,
        rgiAnswerRawSrvc
    ) {
        $scope.sort_options = [
            {value: 'assessment_ID', text: 'Sort by assessment'},
            {value: 'answer_ID', text: 'Sort by answer id'},
            {value: 'status', text: 'Sort by Status'},
            {value: 'researcher_score_letter', text: 'Researcher letter score'},
            {value: 'researcher_score_text', text: 'Researcher score text'},
            {value: 'researcher_score_value', text: 'Researcher score value'},
            {value: 'reviewer_score_letter', text: 'Reviewer letter score'},
            {value: 'reviewer_score_text', text: 'Reviewer score text'},
            {value: 'reviewer_score_value', text: 'Reviewer score value'}
        ];

        $scope.raw_answer_header = [
            'assessment_id',
            'answer_id',
            'status',
            'question_text',
            'researcher_score_letter',
            'researcher_score_text',
            'researcher_score_value',
            'reviewer_score_letter',
            'reviewer_score_text',
            'reviewer_score_value'
        ];

        $scope.sort_order = $scope.sort_options[0].value;
        $scope.busy = false;
        $scope.answers = [];
        $scope.raw_answer_array = [];
        var limit = 50, currentPage = 0, allAnswersLoaded = false;

        rgiAnswerRawSrvc.query({skip: currentPage, limit: limit}, function (answers) {
            if(!answers.reason) {
                $scope.answers = answers;
                $scope.raw_answer_array = answers;
                currentPage++;
            }
        });

        $scope.loadMoreAnswers = function () {
            if ($scope.busy) {
                return;
            }

            $scope.busy = true;

            if(!allAnswersLoaded) {
                rgiAnswerRawSrvc.query({skip: currentPage, limit: limit}).$promise
                    .then(function (answers) {
                        if(!answers.reason) {
                            $scope.answers = $scope.answers.concat(answers);
                            $scope.raw_answer_array = $scope.raw_answer_array.concat(answers);
                            currentPage++;

                            if (answers.length < limit) {
                                allAnswersLoaded = true;
                            }
                        }
                    }).finally(function () {
                        $scope.busy = false;
                    });
            }
        };
    });

@@ -1353,24 +1353,25 @@
 array = %5B%5D;%0A
+%0A
         var 
@@ -1423,18 +1423,17 @@
  = false
-;%0A
+,
 %0A       
@@ -1437,65 +1437,24 @@
     
-rgiAnswerRawSrvc.query(%7Bskip: currentPage, limit: limit%7D,
+    addAnswers =
  fun
@@ -1450,33 +1450,32 @@
 swers = function
- 
 (answers) %7B%0A    
@@ -1462,32 +1462,36 @@
 tion(answers) %7B%0A
+    
             if(!
@@ -1516,32 +1516,36 @@
                 
+  
+  
 $scope.answers =
@@ -1545,25 +1545,52 @@
 swers = 
-answers;%0A
+$scope.answers.concat(answers);%0A    
         
@@ -1627,71 +1627,194 @@
 y = 
-answers;%0A                currentPage++;%0A            %7D%0A        %7D
+$scope.raw_answer_array.concat(answers);%0A                    currentPage++;%0A                %7D%0A            %7D;%0A%0A        rgiAnswerRawSrvc.query(%7Bskip: currentPage, limit: limit%7D, addAnswers
 );%0A%0A
@@ -2157,96 +2157,18 @@
     
-if(!a
+addA
 nswers
-.reason) %7B%0A                            $scope.answers = $scope.answers.concat
 (ans
@@ -2166,32 +2166,33 @@
 swers(answers);%0A
+%0A
                 
@@ -2203,153 +2203,30 @@
     
-    $scope.raw_answer_array = $scope.raw_answer_array.concat(answers);%0A                            currentPage++;%0A%0A                            if
+if (!answers.reason &&
  (an
@@ -2246,23 +2246,20 @@
 %3C limit)
+)
  %7B%0A
-    
         
@@ -2307,38 +2307,8 @@
 ue;%0A
-                            %7D%0A
     

refactor the 'Answer Raw List' front-end controller