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

var MainmenuAssistant = Class.create({
	initialize: function(query) {
		this.query = query;
	},
  
	setup: function() {
		AppAssistant.VideoLibrary.Util.addActiveScene(this);
		this.stateQuery = AppAssistant.VideoLibrary.VideoService.subscribeIndexerState(this.controller, this.handleServiceState.bind(this));
		this.subscribeForVideoCount();

		this.spinnerModel = {
			spinning: false
		};
		
		// spins while indexing
		this.controller.setupWidget('scan-spinner',
				{spinnerSize: 'large'}, this.spinnerModel);

		this.controller.get('option-list').observe(Mojo.Event.tap, this.handleSelection.bindAsEventListener(this));
		AppAssistant.VideoLibrary.Util.setupAppMenu(this);
	},
	
	cleanup: function() {
		AppAssistant.VideoLibrary.Util.removeActiveScene(this);
	},
	
	activate: function() {
		this.controller.stageController.setWindowOrientation("free");
	},
	
	showNoVideosDiv: function(){
		this.controller.get('empty-library-message').style.display = 'block';	
		this.controller.get('video-library-container').style.display = 'none';
	},
	
	showVideosDiv: function(){
		this.controller.get('empty-library-message').style.display = 'none';	
		this.controller.get('video-library-container').style.display = 'block';
	},
	
	handleServiceState: function(response) {
		if (!response || !response.state || !response.stateCode) {
			Mojo.Log.info("Error, unexpected state response!");
			return;
		}

		Mojo.Log.info ("indexer scanning state: " + response.state);

		if (this.indexerState !== undefined && this.indexerState == response.state) {
			Mojo.Log.info ("nothing to be done, returning");
			return;
		}

		this.indexerState = response.state;

		switch (response.state) {
			case "init":
				// fallthrough
			case "scanning":
				// fallthrough
			case "stopped":
				this.showIndexingDiv();
				break;
			default:
				//default includes case "idle"
				this.hideIndexingDiv();
			break;
		}
	},
	
	showIndexingDiv: function(){
		Mojo.Log.info ("show indexing div");
		if (!this.isIndexing){
			this.controller.get('indexing-div').style.display = 'block';	
			this.controller.get('video-library-container').style.display = 'block';
			this.controller.get('empty-library-message').style.display = 'none';	
			this.spinnerModel.spinning = true;
			this.controller.modelChanged(this.spinnerModel);
			this.isIndexing = true;
		}
	},
	
	hideIndexingDiv: function(){
		Mojo.Log.info ("hide indexing div");
		if (this.isIndexing){
			this.spinnerModel.spinning = false;
			this.controller.modelChanged(this.spinnerModel);
			this.isIndexing = false;
			this.subscribeForVideoCount();
		}
	},
	
	subscribeForVideoCount: function(){
		var params = {
			limit: 1,
			offset: 0,
			subscribe: true
		};
		this.videoCount = undefined;
		
		if (this.videoCountRequest){
			this.videoCountRequest.cancel();
		}
		
		this.videoCountRequest = AppAssistant.VideoLibrary.VideoService.listVideos(this.controller, params, function(response){
			var newCount = parseInt(response.videosTotal, 10);
			if (newCount !== this.videoCount) {
				this.videoCount = newCount;
				this.refreshMainMenu();
			}
		}.bind(this));
	},
	
	refreshMainMenu: function(){
		Mojo.Log.info ("calling refresh");
		if (AppAssistant.VideoLibrary.Util.transcoding){
			this.showIndexingDiv();
			
			
			// periodically retry until no longer transcoding
			this.controller.window.setTimeout(this.refreshMainMenu.bind(this), 1000);
		} else {
			// make sure the indexing div is not up
			this.controller.get('indexing-div').style.display = 'none'; 

			if (this.videoCount > 0) {
				this.showVideosDiv();
			} else {
				this.showNoVideosDiv();
			}

			this.populateAllThumbEntries();
		}
		
	
	},
	
	populateAllThumbEntries: function(){
		this.populateThumbEntries('videoroll', {captured: true, edited: false});	
		this.populateThumbEntries('loaded', {captured: false, edited: false});	
	},
	
	populateThumbEntries: function(baseId, params){
		var mainDiv = this.controller.get(baseId);
		var leftDiv = this.controller.get(baseId +'-thumb-left');
		var centerDiv = this.controller.get(baseId + '-thumb-center');
		var rightDiv = this.controller.get(baseId + '-thumb-right');
		
		AppAssistant.VideoLibrary.Util.displayRandomAlbumArt (this.controller, params, this.THUMB_WIDTH, this.THUMB_HEIGHT, leftDiv,  
			centerDiv,	rightDiv, function(count){
				if (count > 0){
					//purging the div of previous class assignments
					var classNameArray = mainDiv.className.split(" ");
					for(var i=0; i<classNameArray.length; i++){
						if(classNameArray[i] === "one" || classNameArray[i] === "two" || classNameArray[i] === "three"){
							delete classNameArray[i];
						}
					}
					mainDiv.style.display = "block";

					if (count === "1") { classNameArray.push("one"); }
					else if (count === "2") { classNameArray.push("two"); } 
					else { classNameArray.push("three"); }

					mainDiv.className = classNameArray.join(" ");
				} else {
					mainDiv.style.display = "none";
				}
			});
	},
	
	handleSelection: function(event){
		var targetRow = this.controller.get(event.target);
		if (!targetRow.hasClassName('palm-row')) {
		  targetRow = this.controller.get(event.target).up('div.palm-row');
		}
		if (targetRow) {
			var id = targetRow.id;
			var query = {};

			switch (id){
				case "loaded":
					query.captured = false;
					break;
				case "videoroll":
					query.captured = true;
					break;
				default:
					Mojo.Log.error ("unknown list item selected");
					break;
			}
			AppAssistant.VideoLibrary.Push(
				this.controller.stageController, AppAssistant.VideoLibrary.Browser, query);
		}
	},
	
	THUMB_WIDTH: 85,
	THUMB_HEIGHT: 65
});

