var AppAPI = require('utils/AppAPI');
var QuestionActions = require('./QuestionActions');
var LearningCardActions = require('./LearningCardActions');

import * as constants from 'constants/CourseConstants';

var ServerActionCreators = {
  quizCompleted: function(userSubLbStatsLogId,answers,leaderboardId,kontestId){
    AppAPI.networkCall(
     'kontests/submitUserResponsesAndGetBadges/v1/',
     {
    "kontestId":kontestId,
    "leaderboardId":leaderboardId,
    "gameStatus":"COMPLETED",
    "correctResponseCount":150,
    "groupId": -1,
    "isGroupGame": false,
    "isChallenge": false,
    "challengeInvitationId": -1,
    "history": answers,
    "userSubLbStatsLogId": userSubLbStatsLogId
    },
     function(msg){
      window.location.assign('#/home/'+leaderboardId);
      QuestionActions.resetCurrentCountIndex();
     }.bind(this),
     function(){
      window.location.assign('#/errorPage');
     }.bind(this)
    );
  },
  getLearningCards: function(learningCards, setId, offset){
    console.log(offset);
    AppAPI.networkCall(
      'kpropsnew/getLearningCardsAPI/v2/',
      {learningCardSetId: setId,offset:offset, length: 20},
      function(msg){
     if(msg['lcDetails'].length==0){
        LearningCardActions.updateCards(msg['lcDetails'], true);
      }
      else if(learningCards[learningCards.length-1].lcId!==msg['lcDetails'][msg['lcDetails'].length-1].lcId){
        if(msg['lcDetails'].length<20){
          LearningCardActions.updateCards(msg['lcDetails'], true);
        }
        else{
          LearningCardActions.updateCards(msg['lcDetails'], false);
        }
      }

      }.bind(this),
      function(msg){
        // console.log(msg);
      }
    );
  },
  receiveSets: function(sets){
    return {
      type: constants.RECEIVE_SETS,
      sets : sets
    };
  }
};

module.exports = ServerActionCreators;
