var Dispatcher = require('../dispatcher/Dispatcher');
var EventEmitter = require('events').EventEmitter;
var QuestionActions = require('../actions/QuestionActions');
var QuestionConstants = require('../constants/QuestionConstants');
var ServerActionCreators = require('../actions/ServerActionCreators');

var assign = require('object-assign');
var CHANGE_EVENT = 'change';

var currentKontestId = -1;
var currentLeaderBoardId = -1;
var presentQuestion = {};
var quizDetails  = {};
var currentQuestionIndex=0;
var isTimerCompleted, isWritingExam= false;
var answers = [];

var currentKontest = {name: '', id: -1};

function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

var QuestionStore = assign({}, EventEmitter.prototype,{
    getAllQuestions : function(){
      return quizDetails['questionTypeObjectsList'];
    },
    getQuizDetails: function(){
      return quizDetails;
    },
    getCurrentLeaderBoardId: function(){
      return currentLeaderBoardId;
    },
    getAllAnswers: function(){
      return JSON.stringify(answers);
    },
    getKontestId: function(){
      return currentKontestId;
    },
    getCurrentKontestDetails: function(){
      return currentKontest;
    },
    getPresentQuestion: function(){
        if(currentQuestionIndex<quizDetails['questionTypeObjectsList'].length){
          quizDetails['questionTypeObjectsList'][currentQuestionIndex]['optionsList'] =
          shuffle(quizDetails['questionTypeObjectsList'][currentQuestionIndex]['optionsList']);
          // console.log(quizDetails['questionTypeObjectsList'][currentQuestionIndex]['optionsList']);
          return quizDetails['questionTypeObjectsList'][currentQuestionIndex];
        }
        else{
          return null;
        }
    },
    getCurrentQuestionIndex: function(){
      return currentQuestionIndex;
    },
    incrementCurrentQuestionIndex : function(){
      if(currentQuestionIndex+1<quizDetails['questionTypeObjectsList'].length){
        ++currentQuestionIndex;
      }
      else{
        // alert('Quiz completed');
        isWritingExam = false;
        ServerActionCreators.quizCompleted(quizDetails['userSubLbStatsLogId'], JSON.stringify(answers),QuestionStore.getCurrentLeaderBoardId());
      }
    },
    isWritingExam: function(){
      return isWritingExam;
    },
    emitChange: function(){
        return this.emit(CHANGE_EVENT);
    },
    addChangeListener: function(callback) {
        this.on(CHANGE_EVENT, callback);
    },
    removeChangeListener: function(callback) {
        this.removeListener(CHANGE_EVENT, callback);
    },
    dispatcherIndex: Dispatcher.register(function(action){
      switch(action.type){
        case QuestionConstants.QUESTION_GETQUIZDETAILS:
          quizDetails = action.data;
          QuestionStore.emitChange();
          break;
        case QuestionConstants.QUESTION_SETISWRITINGEXAM:
          isWritingExam = action.isWritingExam;
          break;
        case QuestionConstants.QUESTION_GOTONEXTQUESTION:
          QuestionStore.incrementCurrentQuestionIndex();
          QuestionStore.emitChange();
          break;
        case QuestionConstants.QUESTION_STOREANSWER:
          console.log(action.answer!==undefined);
          if(action.answer!==undefined){
            answers.push(action.answer);
          }
          break;
        case QuestionConstants.STORE_CURRENT_LEADERBOARDID_AND_KONTEST:
          currentLeaderBoardId = action.currentLeaderBoardId;
          currentKontest = action.currentKontest;
          break;
        case QuestionConstants.STORE_CURRENT_KONTESTID_AND_KONTEST:
          currentKontestId = action.currentKontestId;
          break;
        case QuestionConstants.RESET_CURRENT_COUNT_INDEX:
          currentQuestionIndex = 0;
          quizDetails = {};
          answers=[];
          break;
        default:
          break;
      }
    })
});
module.exports = QuestionStore;
