sa.model = {};
sa.collection = {};

sa.model.Model = Backbone.Model.extend({
    idAttribute: '_id'
});

sa.model.DeepModel =  sa.model.Model.extend({
    types: {}

    , _createModels: function(attributes) {
        var types = this.types;
        var converted = _.extend({}, attributes);

        _.each(attributes, function(value, name) {
            var model, name, type, options;
            if(name in types) {
                type = types[name];
                //sa.L.log(name, value)
                if(_.isFunction(type)) model = new type(value);
                else model = type.constructor(value, type.options||{})
                converted[name] = model;
            }
        }, this);

        return converted;
    }

    // equals check support using underscore.js
    , isEqual: function(other) {
        if(this.constructor != (other && other.constructor)) return false;

        var attrs = this.attributes, otherAttrs = other.attributes;
        for(var name in this.attributes) {
            if(!_.isEqual(attrs[name], otherAttrs[name])) return false;
        }

        return true;
    }

    , initialize: function(attributes, options) {
        var modeled = this._createModels(attributes);
        sa.model.DeepModel.__super__.set.call(this, modeled, {silent: true});
        this._previousAttributes = _.clone(this.attributes);
    }

    , parse: function(resp, xhr) {
        console.log('resp:', resp);
        return this._createModels(resp);
    }

    // TODO override set (and get?) to intercept sets.

});

sa.model.Alert = sa.model.Model.extend({
    isUnread: function() {
        return this.get('timechange') > this.get('timeseen');
    }
    , markRead: function() {
         this.set({
             'timeseen': new Date()
         });
    }
});

sa.collection.Alerts = Backbone.Collection.extend({
    model: sa.model.Alert
    , url: function() {
        return '/api/self/alerts';
    }
});

sa.model.AlertType = sa.model.Model.extend({
    attrs: {
        icon: null
        , name: null
        , parameters: [{}]
        , type: String
        , version: null
    }
});

sa.collection.AlertTypes = Backbone.Collection.extend({
    model: sa.model.AlertType
});

sa.model.Post = sa.model.Model.extend({
});

sa.collection.Posts = Backbone.Collection.extend({
    model: sa.model.Post
});

sa.model.User = sa.model.DeepModel.extend({
    types: {
        account: sa.model.Model.extend({
        })
        , agents: Backbone.Collection.extend({
            model: sa.model.Model.extend({
                attrs: {
                    synckey: null,
                    type: null
                }
            })
        })
        , alerts: Backbone.Collection.extend({
            model: sa.model.Alert
        })
        , _verify: sa.model.Model.extend({
            attrs: {
                account: null,
                email: null,
                phone: null
            }
        })
    }
});

sa.model.SelfUser = sa.model.User.extend({
    url: function() {
        return '/api/self/user';
    }
});


sa.model.PublicUser = sa.model.Model.extend({
});

sa.collection.PublicUsers = Backbone.Collection.extend({
    model: sa.model.PublicUser

    , url: function() {
        return '/api/public/users';
    }
});





