
StatusWidget = function(id, asyncRequestJson) {
    this.id = id;
    
    if (asyncRequestJson) {
        var self = this;
        asyncRequestJson('getStats.json', function(result) {
            self.__setText("name", result.name);
            self.__setText("hit_point_score", result.hit_points);
            self.__setText("kickboxing_score", result.kickboxing);
            self.__setText("linear_algebra_score", result.linear_algebra);
            self.__setText("cross_stitching_score", result.cross_stitching);
        });
    }
};

StatusWidget.prototype = {
    __getChild : function(className) {
        var xpath = "//div[@id='" + this.id + "']//*[@class='" + className + "']";
        var i = document.evaluate(
            xpath, document, null, XPathResult.ANY_TYPE, null
        );
        return i.iterateNext();
    },
    
    __setText : function(classname, value) {
        var c = this.__getChild(classname);
        c.innerHTML = value;
    },
    
    __getInt : function(className) {
        return parseInt(this.__getChild(className).innerHTML);
    },

    getName : function() {
        return this.__getChild("name").innerHTML;
    },

    getHitPoints : function() {
        return this.__getInt("hit_point_score");
    },

    getKickboxingScore : function() {
        return this.__getInt("kickboxing_score");
    },

    getLinearAlgebraScore : function() {
        return this.__getInt("linear_algebra_score");
    },

    getCrossStitchingScore : function() {
        return this.__getInt("cross_stitching_score");
    },

    setHitPoints : function(hp) {
        var xpath = "//div[@id='" + this.id + "']//*[@class='hit_point_score']";
        var i = document.evaluate(
            xpath, document, null, XPathResult.ANY_TYPE, null
        );
        var e = i.iterateNext();
        e.innerHTML = "" + hp;
    }
};

function asyncRequestJson(url, onComplete) {
    var req = new XMLHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            if (req.status == 200) {
                var result = jsonParse(req.responseText);
                onComplete(result);
            } else {
                throw new Error("Request failed: " + req.status);
            }
        }
    }

    req.open('GET', url, true);
    req.send(null);
}
