/* Copyright 2009 Palm, Inc.  All rights reserved. */

var HistoryTable = Class.create({	

	initialize: function() {
		Mojo.Log.info("Youtube: History table: initialize");	
		this.db = null;		
	},
	
	// open the database and create history table
	openDatabase: function(callback) {
		try {
		    if (window.openDatabase) {
				this.db = openDatabase("Youtube", "1.0", "Youtube Database", 1000);
				this.db.transaction(function (tx) {
					// unique on id field insures that multiple records of the same video
					// can not be stored in the table
					tx.executeSql(
						"CREATE TABLE History(id varchar UNIQUE, title varchar, author varchar, rating varchar, views varchar, " +
						"duration varchar, desc varchar, thumbnail varchar, thumbnailBig varchar, " + 
						"link varchar, shareLink varchar, relatedUrl varchar)", [], 
						
						function(tx, result) {
							Mojo.Log.info("Youtube: History table created successfully");
							if (callback) callback();
						}, 
						function(tx, error) { 
							Mojo.Log.error("Youtube: did not create History table: %s", error.message); 
							// TODO if error is that table exists
							if (callback) callback();
						});
					});
			}
			else {
				Mojo.Log.error("Youtube: Couldn't open the database");
			}
		} 
		catch(err) { }
	},
	
	// inserts a record into history table
	insert: function(record) {	
		if (!this.db) {
			this.openDatabase(this.insert.bind(this, record));
		}
		else {
			this.db.transaction(function(tx){
				tx.executeSql("INSERT INTO History VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", 
					[record.id, record.title, record.author, record.rating, record.views, record.duration, record.desc, 
					record.thumbnail, record.thumbnail_240_320, record.link, record.shareLink, record.related_url], 
					function(tx, result) {
						Mojo.Log.info("Youtube: history-table insert OK");
					}, 
					function(tx, error) {
						Mojo.Log.error("Youtube: history-table insert failed: %s", error.message);
					});
			});
		}
	},
	
	// select "limit" records starting from "offset"
	selectByIndex: function(offset, limit, callback) {	
		if (!this.db) {
			this.openDatabase(this.selectByIndex.bind(this, startIndex, endIndex, callback));
		}
		else {
			this.db.transaction(function(tx) {
				tx.executeSql("SELECT * FROM History ORDER BY rowid DESC LIMIT ? OFFSET ?", 
					[limit, offset], 
					function(tx, result) {
						Mojo.Log.info("Youtube: history-table select OK");
						var items = [];
						for(var i = 0; i < result.rows.length; i++) {
							items[i] = {
								id: result.rows.item(i)['id'],
								title: result.rows.item(i)['title'],
								author: result.rows.item(i)['author'],
								rating: result.rows.item(i)['rating'],
								views: result.rows.item(i)['views'],
								duration: result.rows.item(i)['duration'],
								desc: result.rows.item(i)['desc'],
								thumbnail: result.rows.item(i)['thumbnail'],
								thumbnail_240_320: result.rows.item(i)['thumbnailBig'],
								link: result.rows.item(i)['link'],
								shareLink: result.rows.item(i)['shareLink'],
								related_url: result.rows.item(i)['relatedUrl'],
							};		
						}				
						callback(items);
					}, 
					function(tx, error) {
						Mojo.Log.error("Youtube: history-table select failed: %s", error.message);
						callback([]);
					});
			});
		}
	},
	
	// returns total count of records in the db
	totalCount: function(callback) {
		if (!this.db) {
			this.openDatabase(this.totalCount.bind(this, callback));
		}
		else {
			this.db.transaction(function(tx){
				tx.executeSql("SELECT COUNT(*) FROM History", [], 
					function(tx, result) {
						Mojo.Log.info("Youtube: History COUNT: %s", result.rows.item(0)['COUNT(*)']);
						callback( parseInt(result.rows.item(0)['COUNT(*)']) );
				    }, 
					function(tx, error) {
				       	Mojo.Log.info("Youtube: Failed to retrieve COUNT for History table - %s", error.message);
						callback(0);
					});
			});
		}
	},
	
	// deletes all records in the db
	deleteAll: function(callback) {
		if (!this.db) {
			this.openDatabase(this.deleteAll.bind(this, callback));
		}
		else {
			this.db.transaction(function(tx){
				tx.executeSql("DELETE FROM History", [], 
					function(tx, result) {
						if (callback) callback();
				    }, 
					function(tx, error) {
				       	Mojo.Log.info("Youtube: Failed to DELETE from History table - %s", error.message);
						if (callback) callback();
					});
			});
		}
	},
	
});