
define(["underscore", "QServer", "PrairieRandom"], function(_, QServer, PrairieRandom) {

    function MTFServer() {
        QServer.call(this);
    }
    MTFServer.prototype = new QServer();

    MTFServer.prototype.getData = function(vid, options) {
        var rand = new PrairieRandom.RandomGenerator(vid);

        // Set some default values first.
        // Missing values should be caught by the schemas.
        options = _.defaults(options, {
            trueStatements: [],
            falseStatements: []
        });

        var numberTrue = options.trueStatements.length;
        var numberFalse = options.falseStatements.length;

        // TODO(tnip): This is kinda weird. We should be checking for
        // definedness on the full set. If empty, throw an error.
        //
        // Here, we want a list of all the statements so we can list them all.
        var allStatements = [];
        allStatements = allStatements.concat(options.trueStatements);
        allStatements = allStatements.concat(options.falseStatements);

        allStatements = _(allStatements).map(function(value, index) {
              return {key: 'q' + index.toString(), statement: value};
        });
        var perm = rand.shuffle(allStatements);

        var trueAnswer = [];

        // For every index we have, if it's less than the length of true list,
        // we deem it a true statement. Else, it's false.
        for(var i = 0; i < perm.length; i++) {
          trueAnswer.push(perm[i] < options.trueStatements.length);
        }

        var params = {
            statements: allStatements
        };

        var questionData = {
            params: params,
            trueAnswer: { 'correctAnswers' : trueAnswer }
        };

        return questionData;
    }

    MTFServer.prototype.gradeAnswer = function(vid, params, trueAnswer, submittedAnswer, options) {
        trueAnswer = trueAnswer.correctAnswers;
        var finalScore = 0.0;
        var numberCorrect = 0.0;

        // Assumptions:
        //  trueAnswer[i]: T, submittedAnswer[qi-true]: T -> Correct
        //  trueAnswer[i]: F, submittedAnswer[qi-false]: T -> Correct
        //  Else: Incorrect
        for (var i = 0; i < trueAnswer.length; i++) {
          var questionId = 'q' + i.toString();

          if ((trueAnswer[i] && submittedAnswer[questionId + '-true']) ||
            (!trueAnswer[i] && submittedAnswer[questionId + '-false'])) {
            numberCorrect++;
          }
        }

        finalScore = numberCorrect / (trueAnswer.length * 1.0);

        return { 'score' : finalScore };
    }

    return MTFServer;
});

@@ -510,16 +510,109 @@
 ents: %5B%5D
+,%0A            correctScore: 1,%0A            incorrectScore: 0,%0A            guessingPenalty: -5
 %0A       
@@ -2056,27 +2056,59 @@
 var 
-numberCorrect = 0.0
+maxScore = options.correctScore * trueAnswer.length
 ;%0A%0A 
@@ -2293,24 +2293,107 @@
 : Incorrect%0A
+        console.log('ta', trueAnswer);%0A        console.log('sa', submittedAnswer);%0A
         for 
@@ -2646,48 +2646,506 @@
     
-numberCorrect++;%0A          %7D%0A        %7D%0A%0A
+console.log(finalScore);%0A            finalScore += options.correctScore;%0A            console.log('correct');%0A            console.log(finalScore);%0A          %7D else if (submittedAnswer%5BquestionId + '-true'%5D %7C%7C submittedAnswer%5BquestionId + '-false'%5D) %7B%0A            console.log(finalScore);%0A            finalScore = finalScore + (options.incorrectScore + options.guessingPenalty);%0A            console.log('penalty', options.guessingPenalty);%0A            console.log(finalScore);%0A          %7D else %7B%0A    
     
@@ -3163,54 +3163,207 @@
 ore 
++
 = 
-numberCorrect / (trueAnswer.length * 1
+options.incorrectScore;%0A            console.log('nothing');%0A          %7D%0A        %7D%0A%0A        // We floor the questions's final score to 0.%0A        finalScore = Math.max(finalScore / maxScore, 0
 .0);%0A
-%0A
     

implement scoring ofr MTF