Ti.include('record_db.js');
Ti.include('navigation.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(record) {
	db.deleteOne(record);
	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;
		row.add(dateLabel);

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

updateRecord(records);

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

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);
});

Ti.App.addEventListener('delete_row', function(record) {
	Titanium.API.debug("delete_row");
	deleteCallback(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 addButton = Ti.UI.createButton({
	systemButton: Titanium.UI.iPhone.SystemButton.ADD
});
addButton.addEventListener(
'click', function () {
	var at = new Date();
	var recordWindow = Ti.UI.createWindow({
		url: 'record_window.js',
		record: {weight:'', at: at.toDateString()},
		func: 'insert_row',
		backgroundColor:'#fff'
	});
	Ti.UI.currentTab.open(recordWindow);
});
win.rightNavButton = addButton;

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;
*/


var addCallback = function(e) {
	var at = new Date();
	var recordWindow = Ti.UI.createWindow({
		url: 'record_window.js',
		record: {weight:'', at: at.toDateString()},
		func: 'insert_row',
		backgroundColor:'#fff'
	});
	Ti.UI.currentTab.open(recordWindow);	
};

var grpahCallback = function(e) {
	if (records.length > 0) {
		var weights = "[";
		var ticks = "[";
		for (i = records.length-1; i >= 0; i--) {
			var at = new Date(records[i].at);
			weights = weights + "[" + at.getTime() +","+records[i].weight+"],";
			ticks = ticks + at.getTime() + ",";
		} 
		weights = weights + "]"; ticks = ticks + "]";
		var graphWindow = Ti.UI.createWindow({
			url: 'plot_window.js',
			weights: weights,
			ticks: ticks
		}
		);
		Ti.UI.currentTab.open(graphWindow);		
	}	
};

var navi = new Navigation();
navi.setup({
	iPhone: {
		leftButton: {title: 'Graph', callback: grpahCallback},
		rightButton: {systemButton: Titanium.UI.iPhone.SystemButton.ADD, callback : addCallback}
		
	},
	android: {
		leftButton: {title: 'Graph', icon: 'dark_stats-bars.png', callback: grpahCallback},
		rightButton: {title: 'Add', icon: 'dark_add.png', callback : addCallback}		
	}
});

