'use strict';

var mongoose = require('mongoose'),
  errorHandler = require('../errors'),
  async = require('async'),
  Match = mongoose.model('Match'),
  Ticket = mongoose.model('Ticket');

var turnsNotSeen = function(match, thisPlayer, yourTurn, res){
  var i = thisPlayer.lastSeenTurn;
  var turns_not_seen = [];
  var loop = function(i){
    if(i < match.turns.length){
      turns_not_seen.push(match.turns[i]);
      loop(parseInt(i+1));
      
    } else {
    
      match.players[thisPlayer.playerIndex].lastSeenTurn = parseInt(match.turns.length-1);
      match.markModified('players');
      match.save(function(err){
        if (err)
            return res.status(500).send({
              message: 'Error ocurred while looking for turns'
            });
        if(yourTurn){
          return res.status(201).send({
                    message: 'It is your turn, submit turn',
                    turns: turns_not_seen
                  });      
        }else{
          return res.status(201).send({
                    message: 'Wait...',
                    turns: turns_not_seen
                  });    
        }
      }); 
    }
  };
  loop(i);
};


var inactivePlayers = function(match, thisPlayer, turn_player, res){
  var i = turn_player;
  var yourTurn = false;
  var loop = function(i){
    if(i < thisPlayer.playerIndex){
      yourTurn = (match.players[i].active) ? false : true;
      if(yourTurn === true){
        match.turns.push(null);
        match.save(function(err){
        if (err)
            return res.status(500).send({
              message: 'Error ocurred while looking for turns'
            });
            loop(i+1);
        }); 

      } else {
         loop(i+1);
      }

    }else{
       turnsNotSeen(match, thisPlayer, yourTurn, res);
    }
  };
  loop(i);
};


exports.wait = function(req, res) {
  var matchId = req.body.matchId;
  var nextTurn = req.body.nextTurn;
  var playerId = req.body.player; //ticketId del usuario
  var thisPlayer;
  Match.findById(matchId, function(err, match) {
    if (err)
      return res.status(500).send({
        message: 'Error ocurred while looking for match with id = ' + matchId
      });

    if (match) {
         var turn_player = (match.turns.length % 2 === 0) ? 0 : 1;
         var players = match.players;
        
          match.players.forEach(function(player){
            if(player.ticket === playerId){
              thisPlayer = player;
            }
          });
          if (match.players[turn_player].ticket !== playerId) { //If it's not player's turn
            inactivePlayers (match, thisPlayer, turn_player, res);
          }else{
             turnsNotSeen(match, thisPlayer, true, res);
          }
         

    } else {
      return res.status(400).send({
        message: 'ERROR: There is no match with id = ' + matchId
      });

    }

  });

};



exports.submit = function(req, res) {
  var matchId = req.body.matchId;
  var turn = req.body.turn;
  var player = req.body.player; //player = ticketId
  Match.findById(matchId, function(err, match) {

    if (err)
      return res.status(500).send({
        message: 'Error ocurred while looking for match with id = ' + matchId
      });
    if (match) {
      var turn_player = (match.turns.length % 2 === 0) ? 0 : 1;
      if (match.players[turn_player].ticket === player) {
        match.turns.push(turn);
        match.save(function(err) {
          if (err)
            return res.status(500).send({
              message: 'Error ocurred while submiting new turn'
            });
          res.status(201).send('Turn submited');
        });

      } else {
        return res.status(400).send({
          message: 'Error: It is not your turn'
        });
      }

    } else {
      return res.status(400).send({
        message: 'ERROR: There is no match with id = ' + matchId
      });
    }

  });

};
@@ -309,16 +309,44 @@
 n = %5B%5D;%0A
+  // console.log(yourTurn);%0A
   var lo
@@ -355,32 +355,32 @@
  = function(i)%7B%0A
-
     if(i %3C match
@@ -1028,32 +1028,66 @@
 tus(201).send(%7B%0A
+                    match: match,%0A
                 
@@ -1360,32 +1360,64 @@
  = function(i)%7B%0A
+    console.log(match.players);%0A
     if(i %3C thisP
@@ -1737,36 +1737,46 @@
            loop(
+parseInt(
 i+1)
+)
 ;%0A        %7D); %0A%0A
@@ -1822,18 +1822,324 @@
   %7D%0A
-%0A    %7Delse
+    %7D else if(i %3E thisPlayer.playerIndex)%7B%0A       if(i %3C match.turns.length - 1)%7B%0A         yourTurn = (match.players%5Bi%5D.active) ? false : true;%0A         if(!yourTurn) turnsNotSeen(match, thisPlayer, yourTurn, res);%0A         loop(parseInt(i+1));%0A       %7D else%7B%0A          i = 0;%0A          loop(i);%0A       %7D%0A    %7D else 
 %7B%0A  

fix in wait