var Class = require('../class/Class.js').Class;
var Memcache = require('./Memcache.js').Memcache;
var CFMemcache = require('./CFMemcache.js').CFMemcache;
var assertions = require('../common/Assertions.js');

var GCFMemcache = function () {
    this.Extends = CFMemcache;

    this.initialize = function (options) {
        this.parent(options);
    };

    this.get = function (key, callback) {
        var that = this;
        this.parent(key, function (data) {
            // dane sie generuja -> pytamy za chwile
            if (that._isGeneratingContent(data)) {
                console.log('MEM: data is generating...');
                setTimeout(function () {
                    console.log('MEM: checking data is generated...');
                    that.get(key, callback);
                }, 3);

            // nie ma danych     -> ustawiamy flage ze sie generuja
            } else if (!data) {
                console.log('MEM: there is no data! I need to generate content!');
                that._collapsedForwardMarkStart(key);
                callback(null);

            // mamy dane         -> wywolujemy oryginalny callback
            } else {
                console.log('MEM: data is in cache. Calling callback with data!');
                callback(data);
            }
        });
    };
    
    this._collapsedForwardMarkStart = function (key) {
        this.set(key, GCFMemcache.GENERATING);
    };

    this._collapsedForwardMarkClear = function (key) {
        this['delete'](key);
    };
    this._isGeneratingContent = function (data) {
        return data == GCFMemcache.GENERATING;
    };
};

GCFMemcache = new Class(new GCFMemcache());

GCFMemcache.GENERATING = "##GCFMemcache.GENERATING##";

exports.GCFMemcache = GCFMemcache;
