﻿/// <reference name="MicrosoftAjax.js"/>

// ClientStateManager (Knsb.Live.Web)
// Product:  KNSB Live Web Framework
// Assembly: Knsb.Live.Web
//
// Copyright © 2008 - 2009 Emando Software.  All rights reserved.
// Contact: j.h.stokking@emando.nl

Type.registerNamespace("LiveWeb");

LiveWeb.ClientStateManager = function() {
    LiveWeb.ClientStateManager.initializeBase(this);
    var manager = this;
    this._delay = 4;
    this._raceStateManagers = [];
    this._raceStateManagers[LiveWeb.PairColors.WhiteRed] = $create(LiveWeb.RaceStateManager, { stateManager: this, colors: LiveWeb.PairColors.WhiteRed });
    this._raceStateManagers[LiveWeb.PairColors.YellowBlue] = $create(LiveWeb.RaceStateManager, { stateManager: this, colors: LiveWeb.PairColors.YellowBlue });
    this._matches = null;
    this._updateComparableSkatersTID = null;
    this._updateMatchResultsTID = null;
    this._updateMessageTID = null;
}

LiveWeb.ClientStateManager.prototype =
{
    get_delay: function() { return this._delay; },
    set_delay: function(value) {
        this._delay = value;
        this._raceStateManagers[LiveWeb.PairColors.WhiteRed].set_delay(value);
        this._raceStateManagers[LiveWeb.PairColors.YellowBlue].set_delay(value);
    },
    get_matches: function() { return this._matches; },
    get_raceStateManager: function(colors) { return this._raceStateManagers[colors]; },
    get_raceStateManagers: function() { return this._raceStateManagers; },

    add_matchesUpdate: function(handler) { this.get_events().addHandler("matchesUpdate", handler); },
    remove_matchesUpdate: function(handler) { this.get_events().removeHandler("matchesUpdate", handler); },
    add_matchResultsUpdate: function(handler) { this.get_events().addHandler("matchResultsUpdate", handler); },
    remove_matchResultsUpdate: function(handler) { this.get_events().removeHandler("matchResultsUpdate", handler); },
    add_messageUpdate: function(handler) { this.get_events().addHandler("messageUpdate", handler); },
    remove_messageUpdate: function(handler) { this.get_events().removeHandler("messageUpdate", handler); },
    add_videoUpdate: function(handler) { this.get_events().addHandler("videoUpdate", handler); },
    remove_videoUpdate: function(handler) { this.get_events().removeHandler("videoUpdate", handler); },
    add_comparableSkatersUpdate: function(handler) { this.get_events().addHandler("comparableSkatersUpdate", handler); },
    remove_comparableSkatersUpdate: function(handler) { this.get_events().removeHandler("comparableSkatersUpdate", handler); },

    onMatchesUpdate: function(matches) {
        var handler = this.get_events().getHandler("matchesUpdate");
        if (handler) {
            handler(this, { matches: matches });
        }
    },
    onMatchResultsUpdate: function(match, draw, matchResult, ranking) {
        var handler = this.get_events().getHandler("matchResultsUpdate");
        if (handler) {
            handler(this, { match: match, draw: draw, matchResult: matchResult, ranking: ranking });
        }
    },
    onVideoUpdate: function(enabled, html) {
        var handler = this.get_events().getHandler("videoUpdate");
        if (handler) {
            handler(this, { enabled: enabled, html: html });
        }
    },
    onMessageUpdate: function(text) {
        var handler = this.get_events().getHandler("messageUpdate");
        if (handler) {
            handler(this, { text: text });
        }
    },
    onComparableSkatersUpdate: function(fastestRaceSkater, raceSkaters, recordHolders) {
        var handler = this.get_events().getHandler("comparableSkatersUpdate");
        if (handler) {
            handler(this, { fastestRaceSkater: fastestRaceSkater, raceSkaters: raceSkaters, recordHolders: recordHolders });
        }
    },

    reset: function() {
        this._raceStateManagers[LiveWeb.PairColors.WhiteRed].reset();
        this._raceStateManagers[LiveWeb.PairColors.YellowBlue].reset();
        if (this._updateComparableSkatersTID != null) {
            clearTimeout(this._updateComparableSkatersTID);
            this._updateComparableSkatersTID = null;
        }
        if (this._updateMatchResultsTID != null) {
            clearTimeout(this._updateMatchResultsTID);
            this._updateMatchResultsTID = null;
        }
        if (this._updateMessageTID != null) {
            clearTimeout(this._updateMessageTID);
            this._updateMessageTID = null;
        }
        this._matches = null;
    },
    queueData: function(data, delayRaces, delayMatchResultsUpdate) {
        if (data.matches != null) {
            this._matches = data.matches;
            for (var i = 0; i < this._matches.length; i++) {
                this._matches[i].lapCount = LiveWeb.Utils.laps(this._matches[i].distance);
                this._matches[i].lapDistances = LiveWeb.Utils.lapDistances(this._matches[i].distance);
            }
            this.onMatchesUpdate(this._matches);
        }
        this._raceStateManagers[LiveWeb.PairColors.WhiteRed].queueData(data.activeMatchIndex, data.races[LiveWeb.PairColors.WhiteRed], delayRaces);
        this._raceStateManagers[LiveWeb.PairColors.YellowBlue].queueData(data.activeMatchIndex, data.races[LiveWeb.PairColors.YellowBlue], delayRaces);
        if (data.comparableSkaters != null) {
            this.updateComparableSkaters(data.comparableSkaters.fastestRaceSkater, data.comparableSkaters.raceSkaters, data.comparableSkaters.recordHolders, delayRaces);
        }
        if (data.matchResults != null && (data.matchResults.draw != null || data.matchResults.matchResult != null || data.matchResults.ranking != null)) {
            var match = null;
            for (var i = 0; i < this._matches.length; i++) {
                if (this._matches[i].index == data.matchResults.index) {
                    match = this._matches[i];
                    break;
                }
            }
            this.updateMatchResults(match, data.matchResults.draw, data.matchResults.matchResult, data.matchResults.ranking, delayMatchResultsUpdate);
        }
        if (data.video != null) {
            this.updateVideo(data.video.enabled, data.video.html);
        }
        if (data.message != null) {
            this.updateMessage(data.message.text, delayRaces);
        }
    },
    updateMatchResults: function(matchIndex, draw, matchResult, ranking, delayUpdate) {
        Sys.Debug.trace("Match results for match " + matchIndex + " updated.");
        if (delayUpdate) {
            var manager = this;
            this._updateMatchResultsTID = setTimeout(function() {
                manager._updateMatchResultsTID = null;
                manager.onMatchResultsUpdate(matchIndex, draw, matchResult, ranking);
            }, this._delay * 1000);
        }
        else {
            if (this._updateMatchResultsTID != null) {
                clearTimeout(this._updateMatchResultsTID);
                this._updateMatchResultsTID = null;
            }
            this.onMatchResultsUpdate(matchIndex, draw, matchResult, ranking);
        }
    },
    updateVideo: function(enabled, html) {
        Sys.Debug.trace("Video enabled set to " + enabled + ".");
        this.onVideoUpdate(enabled, html);
    },
    updateMessage: function(text, delayUpdate) {
        Sys.Debug.trace("Message changed to '" + text + "'.");
        if (delayUpdate) {
            var manager = this;
            this._updateMessageTID = setTimeout(function() {
                manager._updateMessageTID = null;
                manager.onMessageUpdate(text);
            }, this._delay * 1000);
        }
        else {
            this.onMessageUpdate(text);
        }
    },
    updateComparableSkaters: function(fastestRaceSkater, raceSkaters, recordHolders, delayUpdate) {
        if (delayUpdate) {
            var manager = this;
            this._updateComparableSkatersTID = setTimeout(function() {
                manager._updateComparableSkatersTID = null;
                manager.onComparableSkatersUpdate(fastestRaceSkater, raceSkaters, recordHolders);
            }, this._delay * 1000);
        }
        else {
            if (this._updateComparableSkatersTID != null) {
                clearTimeout(this._updateComparableSkatersTID);
                this._updateComparableSkatersTID = null;
            }
            this.onComparableSkatersUpdate(fastestRaceSkater, raceSkaters, recordHolders);
        }
    }
};

LiveWeb.ClientStateManager.registerClass("LiveWeb.ClientStateManager", Sys.Component);