Ext.define("Portfolio.pd.util.Cache", {

	singleton: true,

	config: {
        stored: {}
    },

    constructor: function(o_config) {

		var i_self = this;

		i_self.initConfig(o_config);
	},

    set: function(s_key, value) {

        var i_self = this;

        var o_stored = i_self.getStored();
        o_stored[s_key] = value;
    },

    get: function(s_key) {

        var i_self = this;

        var o_stored = i_self.getStored();
        var value = o_stored[s_key];

        return value;
    },

    load: function(s_url, s_name) {

        var i_self = this;

        s_name = s_name || s_url;

        var a_return = Ext.Array.from(i_self.get(s_name));

        if (a_return.length === 0) {

            Ext.Ajax.request({
                url: s_url,
                async: false,
                success: function(i_response) {
    
                    a_return = Ext.decode(i_response.responseText, true) || [];
                }
            });

            Ext.Array.each(a_return, function(o_config) {

                if(o_config.type == "formCollection") {

                    var o_rule = o_config.rule || {};

                    Ext.apply(o_config, o_rule);

                    delete o_config.rule;
                }
            });

            i_self.set(s_name, a_return);
        }

        return a_return;
    },
    
    clear: function() {
        
        var i_self = this;
        i_self.setStored({});
    }
});