var Config = {
	login_user_name: null,
	tags: null,
	initializers: []
};

var State = {
	active_bell: null,
	active_bell: null,
	timer: null
};

// 操作不可能なBell
var SimpleBell = Class.create();
SimpleBell.prototype = {
	started_at: null,	// Date
	stopped_at: null,	// Date
	initialize: function(json){
		this.started_at = Date.from_second(json.started_at);
		this.stopped_at = Date.from_second(json.stopped_at);
	},
	elapsed_time: function(){
		if(!this.started_at) return null;
		
		stopped_at = this.stopped_at || new Date();
		
		return new Date(stopped_at - this.started_at);
	},
	is_running: function(){
		return this.started_at != null && this.stopped_at == null;
	},
	is_finished: function(){
		return this.started_at != null && this.stopped_at != null;
	},
	/* dummy */
	create: function(callback){
	},
	update: function(callback){
	},
	destroy: function(callback){
	}
};


// 操作可能なBell
var OperatableBell = Class.create();
Object.extend(OperatableBell.prototype, SimpleBell.prototype);
Object.extend(OperatableBell.prototype, {
	initialize: function(json){
		SimpleBell.prototype.initialize.apply(this, arguments);
	},
	/* dummy */
	create: function(callback){
		callback && callback();
	},
	update: function(callback){
		callback && callback();
	},
	destroy: function(callback){
	}
});

// REST対応版のBell
var Bell = Class.create();
Object.extend(Bell.prototype, OperatableBell.prototype);
Object.extend(Bell.prototype, {
	tags: [],
	initialize: function(json){
		OperatableBell.prototype.initialize.apply(this, arguments);
		
		this.json = json;
		this.id = json.id;
		this.tags = json.tags;
		this.note = json.note;
		this.owner = json.owner || Config.login_user_name;
	},
	/* REST API */
	// create
	create: function(callback){
//	console.debug('create!');
		if(!this.owner) return false;
		if(this.id){
			this.update(callback);
			return true;
		}
		
		var url = ['/',this.owner,'/bells.json'].join('');
//		console.debug(url);
		var params = this.create_params();
		var ajax = new Ajax.Request(
			url,
			{
				method: "post",
				parameters: params,
				onComplete: callback,
				onFailure: function(request){
//					console.debug(request);
				},
				onException: function(request){
//					console.debug(request);
				}
			}
		);
		return true;
	},
	// update
	update: function(callback){
//	console.debug('update!');
		var url = ['/',this.owner,'/bells/',this.id,'.json'].join("");
		var params = this.create_params();
//		console.debug(params);
		var ajax = new Ajax.Request(
			url,
			{
				method: "put",
				parameters: params,
				onComplete: callback
			}
		);
		return true;
	},
	// destroy
	destroy: function(callback){
		var url = ['/',this.owner,'/bells/',this.id,'.json'].join("");
		var params = {};
		var ajax = new Ajax.Request(
			url,
			{
				method: "delete",
				parameters: params,
				onComplete: callback
			}
		);
		return true;
	},
	/* utilities */
	create_params: function(){
		var res = {};
		if(this.started_at != null && this.started_at.getTime() != 0) {
			res.started_at = this.started_at.getTimeBySecond();
		}
		if(this.stopped_at != null && this.stopped_at.getTime() != 0) {
			res.stopped_at = this.stopped_at.getTimeBySecond();
		}
//		console.debug(this.tags);
		res.tags = this.tags ? this.tags.join('+') : [];
		res.note = this.note;
		
		return res;
	}
});

var ScreenTimer = {
	timer_id: null,
	start: function(){
		// インターバルで呼び出される前に1度呼び出す。
		this.callback();
		this.timer_id && this.stop();
		this.timer_id = setInterval(this.callback, 1000);
	},
	stop: function(){
		this.timer_id && clearInterval(this.timer_id);
		this.timer_id = null;
	},
	// concrete
	callback: function(){
		ScreenElement.update();
	}
}

