/* GameUpdate **CLIENT LIBRARY**
 * Wrapper for game update objects
 * does a little bit of reorganizing to provide what front end needs
 * returns DATA, not pretty formatted stuff, thats what prettyprint does
 */

function GameUpdate(lastgameupdate_data, gameupdate_data) {
    var gameUpdate = {}
    gameUpdate._gameupdate_data = gameupdate_data;
    gameUpdate._last = lastgameupdate_data;
    gameUpdate._startingLineups = null;
    
    gameUpdate.updateStartingLineups = function(startinglineupdata) {
        this._startingLineups = startinglineupdata;
    }
    
    gameUpdate.getEvent = function() {
        return this._gameupdate_data['apiResults'][0]['league']['season']['eventType'][0]['events'][0];
    }
    
    gameUpdate.getTeams = function() {
        return this.getEvent()["teams"];
    }
    
    gameUpdate.getBoxscores = function() {
        return this.getEvent()["boxscores"];
    }
    
    gameUpdate.getStatus = function() {
        return this.getEvent()["eventStatus"];
    }
    
    gameUpdate.getPBP = function() {
        return this.getEvent()["pbp"];
    }
    
    gameUpdate.getCurrentPBPIndex = function() {
        return this.getPBP().length - 1; 
    }
    
    gameUpdate.getRecentPlayTexts = function() {
        var playTexts = [];
        var numPlays = 3;
        var pbp = this.getPBP();
        if (pbp.length < 3) {
            numPlays = pbp.length;
        }
        for (var i = pbp.length - 1; i > pbp.length - numPlays - 1; i--) {
            var play = pbp[i];
            var text = play.time.minutes + ":" + play.time.seconds;
            text += " " + play.playText;
            playTexts.push(text);
        }
        return playTexts;
    }
    
    //return two lists of segments, each with an array of plays and a lineupchange object
    gameUpdate.getLineupSegments = function() {
        var pbp = this.getPBP();
        console.log(this._startingLineups);
        var segments = LineupSegmentList(pbp, {9: 0, 25: 1}, this._startingLineups);
        if (Object.keys(segments).length != 2) {
            return null;
        }
        return segments;
    }
    
    gameUpdate.getScoringPBP = function(index) {
        if (!index) index = 0;
        
        var pbp = this.getPBP();
        var scores = [0, 0];
        var scoringPBP = [[], []];
        
        for (var i = index; i < pbp.length; i++) {
            if (pbp[i]["pointsScored"]) {
                if (pbp[i]["homeScore"] > scores[0]) {
                    scores[0] = pbp[i]["homeScore"];
                    scoringPBP[0].push(pbp[i]);   
                }
                else if (pbp[i]["visitorScore"] > scores[1]) {
                    scores[1] = pbp[i]["visitorScore"];
                    scoringPBP[1].push(pbp[i]);
                }
            }
            
        }
        return scoringPBP;
    }
    
    //returns the index that indicates the end of the last pbp 
    gameUpdate.getLastPBPIndex = function() {
        if (!this._last) {
            return 0;
        }
        else {
            return this._last.getCurrentPBPIndex();
        }
    }
    
    gameUpdate.getOnCourtPlayers = function() {
        var boxscores = this.getBoxscores();
        if (!boxscores[0]) {
            console.log("no boxscore yet", boxscores);
            return;
        }
        var team1_players = []
	    for (var player_key in boxscores[0].playerstats) {
            var player = boxscores[0].playerstats[player_key]
        
            if (player.onCourt == "true") {
                team1_players.push(player.player)
            }
        }
        
        var team2_players = [];
	    for (var player_key in boxscores[1].playerstats) {
            var player = boxscores[1].playerstats[player_key]
        
            if (player.onCourt == "true") {
                team2_players.push(player.player)
            }
        }
        
        return [team1_players, team2_players];
    }
    
    return gameUpdate;
}