Ti.include('record_db.js');

var db = new RecordDB();
var records = db.findAll();

function insertCallback(record) {
	db.insert(record);
	records = db.findAll();
	updateRecord(records);	
}

function updateCallback(record) {
	db.update(record);
	records = db.findAll();
	updateRecord(records);	
}

function deleteCallback(index) {
	db.deleteOne(records[index]);
	records = db.findAll();
	updateRecord(records);
}

var win = Ti.UI.currentWindow;
var data = [];
var tableView = Ti.UI.createTableView({
	data:data,
	editable: true
});
win.add(tableView);

function updateRecord (records) {
	var data = currentData = [];
	for (var i=0;i<records.length;i++) {
		var record = records[i];
		var row = Ti.UI.createTableViewRow({
			height: 'auto',
			layout: 'vertical',
			hasChild: true,
		});
		var weightLabel = Ti.UI.createLabel({
			width: 50,
			height: 'auto',
			left: 5,
			top: 5,
			fontSize: 8,
			fontWeight: 'bold',
			textAlign: 'right',
			color: '#2b4771'
		}
		);
		weightLabel.text = record.weight;
		row.add(weightLabel);
		var weightUnit = Ti.UI.createLabel({
			width: 50,
			height: 'auto',
			left: 60,
			top: -weightLabel.height,
			fontSize: 8,
			fontWeight: 'bold',
			text: 'Kg',
			color: '#2b4771'
		}
		);
		row.add(weightUnit);
		var dateLabel = Ti.UI.createLabel({
			width: 290,
			height: 'auto',
			left: 5,
			top: 5,
			fontSize: 6,
			textAlign: 'right'
		}
		);
		dateLabel.text = record.at.toDateString();
		row.add(dateLabel);

		currentData.push(row);
	}
	tableView.setData(currentData);
}

updateRecord(records);

tableView.addEventListener('delete', function(e){
	deleteCallback(e.index);
});

var addButton = Ti.UI.createButton({
	systemButton: Titanium.UI.iPhone.SystemButton.ADD
});
addButton.addEventListener(
'click', function () {
	var recordWindow = Ti.UI.createWindow({
		url: 'record_window.js',
		record: {weight:'', at: new Date()},
		func: 'insert_row',
		backgroundColor:'#fff'
	});
	Ti.UI.currentTab.open(recordWindow);
});
win.rightNavButton = addButton;

Ti.App.addEventListener('insert_row', function(record) {
	Titanium.API.debug("insert_row");
	insertCallback(record);
});

Ti.App.addEventListener('update_row', function(record) {
	Titanium.API.debug("update_row");
	updateCallback(record);
});

tableView.addEventListener(
'click', function(e) {
	var record = records[e.index];
	record.index = e.index;
	var recordWindow = Ti.UI.createWindow({
		url: 'record_window.js',
		record: record,
		func: 'update_row',
		backgroundColor:'#fff'
	}
	);
	Ti.UI.currentTab.open(recordWindow);
});


function select4weeks(to) {
	var from = new Date();
	from.setTime(to.getTime()-(4*7*24*3600*1000));
	var ret = {};
	var weights = "[";
	var ticks = "[";
	var recs = db.findBetween(from, to);
	if (recs.length > 0) {
		for (i = 0; i < recs.length; i++) {
			weights = weights + "[" + recs[i].at.getTime() +","+recs[i].weight+"],";
		}
		for (i = 0; i < 5; i++) {
			ticks = ticks + (from.getTime() + i*7*24*3600*1000) + ",";
		}
	}
	ret.weights = weights + "]";
	ret.ticks = ticks + "]";
	return ret;
}

var graphButton = Ti.UI.createButton({title: 'Graph'});
graphButton.addEventListener(
'click', function () {
	if (records.length > 0) {
		var weights = "[";
		var ticks = "[";
		for (i = records.length-1; i >= 0; i--) {
			weights = weights + "[" + records[i].at.getTime() +","+records[i].weight+"],";
			ticks = ticks + records[i].at.getTime() + ",";
		} 
		weights = weights + "]"; ticks = ticks + "]";
		var graphWindow = Ti.UI.createWindow({
			url: 'plot_window.js',
			weights: weights,
			ticks: ticks
		}
		);
		Ti.UI.currentTab.open(graphWindow);		
	}
}
);
win.leftNavButton = graphButton;
