﻿function PredictionHelper() {
    this.list = [];
}

PredictionHelper.prototype = {
    list: null,
    ready: false,
    initialized: false,

    teamPointsStandings: null,
    teamPoints: null,
    teamPointsUrl: '/Prediction/TeamPredictionPoints',

    init: function () {
        var self = this;

        // perform ajax to get the match details
        ajaxHelper.call(
            {
                context: self,
                url: self.teamPointsUrl,
                type: 'GET',
                cache: true,
                success: function (points) {
                    self.teamPoints = points;
                    self.ready = true;
                    self.execute();
                },
                error: function () {
                    alert("Tahmin detaylari getirilemedi. Daha sonra tekrar deneyiniz.");
                }
            });
    },

    execute: function () {
        var self = this;
        $.each(this.list, function () {
            this.call(self);
        });
    },

    using: function (f) {
        if (!this.ready) {
            if (!this.initialized) {
                this.initialized = true;
                this.init();
            }

            // Match details didn't load yet, adding to the inner list
            // to be executed later
            if ($.isFunction(f)) {
                this.list.push(f);
            }
        }
        else {
            // run immediately
            f.call(this);
        }
    },


    getPointsByTeams: function () {
        var standings, points = this.teamPoints, p, teamsHash;

        if (!this.teamPointsStandings) {
            standings = [];
            teamsHash = {};

            function addPoints(id, points) {
                var t = teamsHash[id];
                if (!t) {
                    t = { id: id, points: 0, count: 0 };
                    teamsHash[id] = t;
                    standings.push(t);
                }

                t.points += points;
                t.count += 1;
                t.average = Math.round((t.points * 10)/ t.count) / 10;
            }

            for (var i = 0; i < points.length; i++) {
                p = points[i];
                var result = new MatchScore(p.HomeScore, p.AwayScore).getResult();

                if (result >= 0) {
                    if (result === 1) {
                        addPoints(p.HomeTeam, p.HomeRate);
                        addPoints(p.AwayTeam, p.HomeRate);
                    }
                    else if (result === 2) {
                        addPoints(p.HomeTeam, p.AwayRate);
                        addPoints(p.AwayTeam, p.AwayRate);
                    }
                    else {
                        addPoints(p.HomeTeam, p.DrawRate);
                        addPoints(p.AwayTeam, p.DrawRate);
                    }
                }
            }

            standings.sort(function (p1, p2) {
                return p2.average - p1.average;
            });

            this.teamPointsStandings = standings;
        }
        return this.teamPointsStandings;
    }
}

function MatchScore(homeScore, awayScore) {
    this.homeScore = homeScore;
    this.awayScore = awayScore;
}

MatchScore.prototype = {
    homeScore: null,
    awayScore: null,

    getResult: function () {
        var home = this.getScoreValue(this.homeScore);
        var away = this.getScoreValue(this.awayScore);

        if (home >= 0 && away >= 0) {
            return home > away ? 1 : (home < away ? 2 : 0);
        }

        return -1;
    },

    getScoreValue: function (score) {
        var scores = [];
        if (score) {
            for (var i = 0; i < 8; i += 2) {
                if (score.length >= (i + 2)) {
                    scores.push(parseInt(score.substr(i, 2), 10));
                }
            }
        }
        return scores.length > 1 ? scores[scores.length - 1] : -1;
    }
}

var predictionHelper = new PredictionHelper();

TY.loaded("predictionHelper", predictionHelper);

