App.Models.Email = Backbone.Model.extend({

	defaults: {
		UID:null,
		flags:'',
		preview:'',
		basicHeaders:{},
		size:0,
		timeago:null,
		sentToday:false,
		YYYYMMDD:null,
		HHMM:null,
		date:null,
		acct:'',
		acctName:'',
		key:''
	},
	
	idAttribute:'key',


	initialize: function(attrs,options){
		this.set('key', this.get('acct') + ':' + this.get('UID'));

		try
		{
			var d = new Date(attrs.basicHeaders.date),
				nums = [ 
					(d.getMonth()+1),
					d.getDate(),
					d.getHours(),
					d.getMinutes()
				],
				formatted, isToday, today,
				time;

			this._padWithZero(nums);

			formatted = [d.getFullYear(),nums[0],nums[1]].join('-');
			time      = nums[2]+":"+nums[3];
			today     = new Date();
			sentToday = false;

			if(today.getFullYear()===d.getFullYear() && today.getMonth()===d.getMonth() && today.getDate()===d.getDate()){
				sentToday = true;
			}

			this.set('date', d);
			this.set('timeago', jQuery.timeago( this.get('date') ) );
			this.set('dateFormatted', (formatted+" "+time) );
			this.set('sentToday',sentToday);
			this.set('YYYYMMDD',formatted);
			this.set('HHMM',time);

		} catch(e) {
			console.log(e);
			console.log(attrs);
		}
	},
});

App.Models.Email.prototype._padWithZero = function(nums){
	_.each(nums,function(n,k){
		nums[k] = ( n < 10 )? '0'+n : n.toString();
	});
};

App.Models.Email.prototype.getReplyEnvelope = function(){

	var hdrs = this.get('basicHeaders'),
		from = hdrs.from.split(",");

	return {
		to: [from.pop()],
		account: hdrs.to_email,
		subject: ( /^\s*RE:/i.test(hdrs.subject)? hdrs.subject : 'Re: '+hdrs.subject),
		body: App.messages.get( this.get('UID') ).get('bodyPreparedHtml')
	}
}

App.Models.Email.prototype.isRead = function(){
	var flags = this.get('flags');
	return -1 !== flags.indexOf("Seen");
}

App.Models.Email.prototype.isFlagged = function(){
	var flags = this.get('flags');
	return -1 !== flags.indexOf("Flagged");
}

App.Models.Email.prototype.isRepliedTo = function(){
	var flags = this.get('flags');
	return -1 !== flags.indexOf("Answered");
}

App.Models.Email.prototype.setSeen = function(){
	this._setFlagAttribute('Seen');
}

App.Models.Email.prototype.setFlagged = function(){
	this._setFlagAttribute('Flagged');
}

App.Models.Email.prototype.setUnseen = function(){
	this._unsetFlagAttribute('Seen');
}

App.Models.Email.prototype.setUnflagged = function(){
	this._unsetFlagAttribute('Flagged');
}

App.Models.Email.prototype._setFlagAttribute = function(flag){
	var flags = this.get('flags');

	if(flags.indexOf(flag) === -1){
		if(flags.length){
			var tmp = flags.split(',');
			tmp.push(flag);
			this.set( 'flags', tmp.join(',') );
		} else {
			this.set( 'flags', flag );
		}
	}
}

App.Models.Email.prototype._unsetFlagAttribute = function(flag){
	var flags = this.get('flags');
	
	if(flags.indexOf(flag) !== -1){
		
		if(flags.indexOf(',') > -1){
			var tmp = _.reject(flags.split(','),function(f){ return f==flag });
			// console.log(tmp);
			this.set( 'flags', tmp.join(",") );
		} else {
			this.set( 'flags', '');
		}
	}
}

