 

// ------------------------------------------------------------------------
// ProjectListAssistant
//   main scene handler for TimeTracker project lists
// ------------------------------------------------------------------------
function ProjectListAssistant() {
	this.appController = Mojo.Controller.getAppController();
	this.stageController = this.appController.getStageController(TimeTracker.MainStageName);
}


//
// setup()
//
ProjectListAssistant.prototype.setup = function() {
	Mojo.Log.info("ProjectListAssistant#setup()..");

	// Setup App Menu
	this.controller.setupWidget(Mojo.Menu.appMenu, TimeTracker.MenuAttr, TimeTracker.MenuModel);

	// Setup the project list..
	this.projectListModel = { items: TimeTracker.projectList.items() };
	this.projectListAttrs = {
		itemTemplate: "projectList/projectRowTemplate", 
		listTemplate: "projectList/projectListTemplate", 
		addItemLabel: $L("Add..."), 
		swipeToDelete: true, 
		renderLimit: 40,
		reorderable: true
	};
	this.controller.setupWidget("projectList", this.projectListAttrs, this.projectListModel);

	// Setup event handlers: list selection, add, delete and reorder project entry
	this.tapHandler = this.tapItem.bindAsEventListener(this);
	this.addHandler = this.addItem.bindAsEventListener(this);
	this.deleteHandler = this.deleteItem.bindAsEventListener(this);
	this.moveHandler = this.moveItem.bindAsEventListener(this);
	this.controller.listen("projectList", Mojo.Event.listTap, this.tapHandler);
	this.controller.listen("projectList", Mojo.Event.listAdd, this.addHandler);    
	this.controller.listen("projectList", Mojo.Event.listDelete, this.deleteHandler);
	this.controller.listen("projectList", Mojo.Event.listReorder, this.moveHandler);

	// Setup listeners for minimize/maximize events
	this.activateWindowHandler = this.activateWindow.bindAsEventListener(this);
	this.deactivateWindowHandler = this.deactivateWindow.bindAsEventListener(this);
	Mojo.Event.listen(this.controller.stageController.document, Mojo.Event.activate, this.activateWindowHandler);
	Mojo.Event.listen(this.controller.stageController.document, Mojo.Event.deactivate, this.deactivateWindowHandler);
};


//
// activate() - handle portrait/landscape orientation, 
//    feature project layout and rotation, and show the info 
//    dialog for the first launch after an app update.
//
ProjectListAssistant.prototype.activate = function() {
	Mojo.Log.info("ProjectListAssistant#activate()..");

	this.projectListModel.items = TimeTracker.projectList.items();
	this.controller.modelChanged(this.projectListModel);

	// Set Orientation to free to allow rotation
	if (this.controller.stageController.setWindowOrientation) {
		this.controller.stageController.setWindowOrientation("free");
	}

	if (TimeTracker.firstTime == true)  {
		TimeTracker.firstTime = false;
		this.controller.showDialog(
			{
				template: "firstTime/firstTime-dialog",
				assistant: new FirstTimeAssistant(this),
				title: $L("TimeTracker — v#{version}").interpolate({version: TimeTracker.versionString}),
				message: TimeTracker.whatsNew
			}
		);
	}   

};


//
// deactivate()
//
ProjectListAssistant.prototype.deactivate = function() {
	Mojo.Log.info("ProjectListAssistant#deactivate()..");
};


//
// cleanup() - save projectlist contents
//
ProjectListAssistant.prototype.cleanup = function() {
	Mojo.Log.info("ProjectListAssistant#cleanup()..");

	// Save the project list on close, just in case.. 
	TimeTracker.projectList.saveAll();

	// Remove event listeners
	this.controller.stopListening("projectList", Mojo.Event.listTap, this.tapHandler);
	this.controller.stopListening("projectList", Mojo.Event.listAdd, this.addHandler);    
	this.controller.stopListening("projectList", Mojo.Event.listDelete, this.deleteHandler);
	this.controller.stopListening("projectList", Mojo.Event.listReorder, this.moveHandler);
	Mojo.Event.stopListening(this.controller.stageController.document, Mojo.Event.activate, this.activateWindowHandler);
	Mojo.Event.stopListening(this.controller.stageController.document, Mojo.Event.deactivate, this.deactivateWindowHandler);
};


//
// activateWindow()
//
ProjectListAssistant.prototype.activateWindow = function() {
	Mojo.Log.info("ProjectListAssistant#activateWindow()..");
};


//
// deactivateWindow()
//
ProjectListAssistant.prototype.deactivateWindow = function() {
	Mojo.Log.info("ProjectListAssistant#deactivateWindow()..");
};


//
// deleteItem()
//
ProjectListAssistant.prototype.deleteItem = function(event) {
	Mojo.Log.info("ProjectListAssistant#deleteItem().. deleting item " + event.index + ", " + event.item.name + ".");

	TimeTracker.projectList.deleteItemAt(event.index);
	this.projectListModel.items = TimeTracker.projectList.items();
};


//
// moveItem()
//
ProjectListAssistant.prototype.moveItem = function(event) {
	Mojo.Log.info("ProjectListAssistant#moveItem().. moving " + event.item.name + " fromindex " + event.fromIndex + " toindex " + event.toIndex);

	TimeTracker.projectList.moveItemAt(event.fromIndex, event.toIndex);
	this.projectListModel.items = TimeTracker.projectList.items();
};


//
// addItem()
//
ProjectListAssistant.prototype.addItem = function() {
	Mojo.Log.info("ProjectListAssistant#addItem()..");
	this.stageController.pushScene("projectEdit");
};


//
// tapItem()
//
ProjectListAssistant.prototype.tapItem = function(event) {
	Mojo.Log.info("ProjectListAssistant#tapItem()..");

	var myEvent = event;
	var findPlace = myEvent.originalEvent.target;
	this.selectIndex = event.index;
	this.controller.popupSubmenu({
		onChoose:  this.popupHandler,
		placeNear: findPlace,
		items: [
			{label: $L("Record Time"), command: "record-time"},
			{label: $L("List Time"), command: "list-time"},
			{label: $L("Edit Project"), command: "edit-project"},
		]
	});
};


//
// popupHandler() - called by the popup from tapItem()..
//
ProjectListAssistant.prototype.popupHandler = function(command) {
	Mojo.Log.info("ProjectListAssistant#popupHandler().. command = " + command);
	var project = TimeTracker.projectList.getItemAt(this.selectIndex);		

	switch(command) {
		case "record-time":
		this.stageController.pushScene("timeEdit", project);
		break;
		
		case "list-time":
		var timeEntryList = new TimeEntryList();
		var pushSceneFn = (function() {
			Mojo.Log.info("ProjectListAssistant#popupHandler().. pushing TimeList scene..");
			this.stageController.pushScene("timeList", project, timeEntryList);
		}).bind(this);
		var logErrorFn = (function() { 
			Mojo.Log.error("ProjectListAssistant#popupHandler().. ERROR: " + $A(arguments).inspect());
		});
		timeEntryList.loadWhere("project_id = ?", [project.id], pushSceneFn, logErrorFn);
		break;
		
		case "edit-project":
		this.stageController.pushScene("projectEdit", this.selectIndex);
		break;

	}
};

