//-*- mode: Javascript; coding: utf8n; tab-width:4 -*-
var Results = Class.create(
    {
      initialize: function (result, push_url) {
          this.result = result;
          this.onetime_token = result.onetime_token;
          this.max = result.minors.length;
          this.start_minor = parseInt(result.current_minor);
          this.finish = result.finish==1?true:false;
          this._push_url = push_url;
          this._top_of_part = parseInt(result.top_of_part);
          this._max_in_part = parseInt(result.max_in_part);
          this._current_minor = result.current_minor;
          this.seek(this.current_minor());
          this.current_part = this.result.minors[this._current_minor].part;
          return true;
      },
      current_minor: function () {
          return this._current_minor - this._top_of_part;
      },
      seen: function () {
          this.result.minors[this._current_minor].seen = 1;
      },
      seek: function (minor) {
          if (window.console) { console.log( 'seek to :'+minor ) };
          minor = parseInt(minor);
          if (minor < 0) {
              minor = 0;
          }
          else if (this._max_in_part <= minor){
              minor = this._max_in_part - 1;
          }
          this._current_minor = this._top_of_part + minor;
          return this._current_minor;
      },
      prev: function () {
          if (0 < this._current_minor){
              this._current_minor --;
          }
          this.seen();
          return this._current_minor;
      },
      next: function () {
          if (this._current_minor == (this.max - 1)){
              this.finish = true;
          }
          else {
              this._current_minor ++;
              this.seen();
          }
          this.push();
          if (this.finish) {
              return 'finish';
          }
          if (this.current_part != this.result.minors[this._current_minor].part) {
              return 'next part';
          }
          return this._current_minor;
      },
      show_solution: function (flag) {
          if (flag) {
              this.result.minors[this._current_minor].saw_solution = 1;
          }
          else {
              this.result.minors[this._current_minor].saw_solution = 0;
          }
      },
      saw_solution: function () {
          return this.result.minors[this._current_minor].saw_solution==1?1:0;
      },
      check_answer: function (user_answer) {
          this.result.minors[this._current_minor].user_answer = user_answer;
          if (this.result.minors[this._current_minor].correct_answer === user_answer) {
              return true;
          }
          return false;
      },
      user_answer: function (user_answer) {
          if (user_answer != undefined) {
              this.result.minors[this._current_minor].user_answer = user_answer;
          }
          return this.result.minors[this._current_minor].user_answer;
      },
      elapsed_time: function (elapsed_time) {
          if (elapsed_time != undefined) {
              this.result.minors[this._current_minor].elapsed_time = elapsed_time;
          }
          return this.result.minors[this._current_minor].elapsed_time;
      },
      num_of_words: function (num_of_words) {
          if (num_of_words != undefined) {
              this.result.minors[this._current_minor].num_of_words = num_of_words;
          }
          return this.result.minors[this._current_minor].num_of_words;
      },
      done: function () {
          this.finish = true;
      },
      push: function () {
          this.result.current_minor = this._current_minor;
          if (this.finish){
              this.result.finish = 1;
          }
          var myJSONText = JSON.stringify(this.result); //.minors[this.result.current_minor]);
      },
      push_current: function () {
          this.result.current_minor = this._current_minor;
          if (this.finish){
              this.result.finish = 1;
          }
          var minor = this.result.minors[this._current_minor];
          var myJSONText = JSON.stringify( {
              "id": minor.id,
              "minor": minor.minor,
              "seen": minor.seen,
              "saw_solution": minor.saw_solution,
              "user_answer": minor.user_answer
          } );
          if (window.console) { console.log( 'myJSONText :'+myJSONText ) };
      }
    }
    );
