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

var UpdatesDepot = Class.create({
	initialize: function() {
		Mojo.Log.info("UpdateDepot initialize");
		this.depot = new Mojo.Depot( { name: "ReleaseNotes", replace: false}, 
            function()
            {
                Mojo.Log.info("Depot connected successfully");
            }, 
            function()
            {
                Mojo.Log.info("Depot failed to connect" );
            }
		);
	},
	
	getDepotObject: function( name, callback )
	{
	    this.depot.simpleGet( name, callback);
	},

	saveDepotObject: function( name, obj )
	{
	    this.depot.simpleAdd( name, obj);
	},
	
	setCurrentVersion: function(version){
//		Mojo.Log.info("Depot: setCurrentVersion", version);
		if(version && version != "") {
			var versionlist = version.split(" ");
			this.currentVersion = versionlist[2].replace(/\./g, "-");
		} else {
			this.currentVersion = undefined;
		}
	},
	
	setNextVersion: function(version){
//		Mojo.Log.info("Depot: setNextVersion", version);
		if(version && version != "") {
			var versionlist = version.split(" ");
			this.nextVersion = versionlist[2].replace(/\./g, "-");
		} else {
			this.nextVersion = undefined;
		}
	},
	
	getInstalledUrl: function(version){
		return "http://help.palm.com/release_notes/webOS_{version}_installed.html".gsub("{version}", version);
		//return "http://help.palm.com/release_notes/webOS_1-3-5_installed.html";
	},
	
	getAvailableUrl: function(version){
		return "http://help.palm.com/release_notes/webOS_{version}_available.html".gsub("{version}", version);
		//return "http://help.palm.com/release_notes/webOS_1-3-5_available.html";
	},
	
	getCurrentVersionNotesUrl: function(){
		if (!this.currentVersion || this.currentVersion == "") {
			return "";
		}
		return this.getInstalledUrl(this.currentVersion);
	},
	
	getNewVersionNotesUrl: function(){
		if (!this.nextVersion || this.nextVersion == "") {
			return "";
		}
		return this.getInstalledUrl(this.nextVersion);		
	},
	
	getNextVersionNotesUrl: function(){
		if (!this.nextVersion || this.nextVersion == "") {
			return "";
		}
		return this.getAvailableUrl(this.nextVersion);
	},

	getCurrentReleaseNotes: function(url, callback, failureCallback){
		Mojo.Log.info("Depot: getCurrentReleaseNotes");				
		this.currentXmlHttp = new XMLHttpRequest();		
        this.currentXmlHttp.onreadystatechange = callback;
        this.currentXmlHttp.open("GET", url, true);

        try {
            this.currentXmlHttp.send();
        } catch(e) {
            Mojo.Log.info("Depot Failed to send xmLHttpRequest");
			failureCallback();
        }
	},

	getNewReleaseNotes: function(url, callback, failureCallback){
		Mojo.Log.info("Depot: getNewReleaseNotes");				
		this.newXmlHttp = new XMLHttpRequest();		
        this.newXmlHttp.onreadystatechange = callback;
        this.newXmlHttp.open("GET", url, true);

        try {
            this.newXmlHttp.send();
        } catch(e) {
            Mojo.Log.info("Depot Failed to send xmLHttpRequest");
			failureCallback();
        }
	},

	getNextReleaseNotes: function(url, callback, failureCallback){
		Mojo.Log.info("Depot: getNextReleaseNotes");
		this.nextXmlHttp = new XMLHttpRequest();		
        this.nextXmlHttp.onreadystatechange = callback;
        this.nextXmlHttp.open("GET", url, true);

        try {
            this.nextXmlHttp.send();
        } catch(e) {
            Mojo.Log.info("Depot Failed to send xmLHttpRequest");
			failureCallback();
        }
	},
	
	currentXmlHttpSuccessCallback: function(detailToken, xmlHttp) {		
//		Mojo.Log.info("Depot: currentXmlHttpSuccessCallback");	
        if (this.currentXmlHttp.readyState == 4) {
            if ( (this.currentXmlHttp.status==404) || (this.currentXmlHttp.status==401) || (this.currentXmlHttp.responseText.length <= 0)) {
				Mojo.Log.info("Depot currentXmlHttpSuccessCallback responseText failure");
            } else {
				Mojo.Log.info("Depot currentXmlHttpSuccessCallback responseText = " + this.currentXmlHttp.responseText);
				// Do not show the coming soon content.
				if (this.currentXmlHttp.responseText.search("<!-- Content Coming Soon! -->") == -1) {
					this.saveDepotObject(detailToken, this.currentXmlHttp.responseText);
					this.currentReleaseCallback(this.currentXmlHttp.responseText);
				} else {
					Mojo.Log.info("Depot currentXmlHttpSuccessCallback: block content coming soon");
				}
			}          
        }
	},
	newXmlHttpSuccessCallback: function(detailToken, xmlHttp) {		
//		Mojo.Log.info("Depot: newXmlHttpSuccessCallback");	
        if (this.newXmlHttp.readyState == 4) {
            if ( (this.newXmlHttp.status==404) || (this.newXmlHttp.status==401) || (this.newXmlHttp.responseText.length <= 0)) {
				Mojo.Log.info("Depot newXmlHttpSuccessCallback responseText failure");
            } else {
				Mojo.Log.info("Depot newXmlHttpSuccessCallback responseText = " + this.newXmlHttp.responseText);
				// Do not show the coming soon content.
				if (this.newXmlHttp.responseText.search("<!-- Content Coming Soon! -->") == -1) {
					this.saveDepotObject(detailToken, this.newXmlHttp.responseText);
					this.newReleaseCallback(this.newXmlHttp.responseText);
				}else {
					Mojo.Log.info("Depot newXmlHttpSuccessCallback: block content coming soon");
				}
			}          
        }
	},
	nextXmlHttpSuccessCallback: function(detailToken, xmlHttp) {	
//		Mojo.Log.info("Depot: nextXmlHttpSuccessCallback");	
        if (this.nextXmlHttp.readyState == 4) {
            if ( (this.nextXmlHttp.status==404) || (this.nextXmlHttp.status==401) || (this.nextXmlHttp.responseText.length <= 0)) {
				Mojo.Log.info("Depot nextXmlHttpSuccessCallback responseText failure");
            } else {
				Mojo.Log.info("Depot nextXmlHttpSuccessCallback responseText = " + this.nextXmlHttp.responseText);
				// Do not show the coming soon content.
				if (this.nextXmlHttp.responseText.search("<!-- Content Coming Soon! -->") == -1) {
					this.saveDepotObject(detailToken, this.nextXmlHttp.responseText);
					this.nextReleaseCallback(this.nextXmlHttp.responseText);
				}else {
					Mojo.Log.info("Depot nextXmlHttpSuccessCallback: block content coming soon");
				}
			}          
        }
	},
	
	currentXmlHttpFailureCallback: function(detailToken, xmlHttp){
		Mojo.Log.info(detailToken);
		Mojo.Log.info("Depot currentXmlHttpFailureCallback responseText failure2");
	},
	
	newXmlHttpFailureCallback: function(detailToken, xmlHttp){
		Mojo.Log.info(detailToken);
		Mojo.Log.info("Depot newXmlHttpFailureCallback responseText failure2");
	},

	nextXmlHttpFailureCallback: function(detailToken, xmlHttp){
		Mojo.Log.info(detailToken);
		Mojo.Log.info("Depot nextXmlHttpFailureCallback responseText failure2");
	},

	depotCallback: function(detailToken, obj){		
		if (obj && obj != "") {
			Mojo.Log.info("Depot HTML stored ", detailToken, obj);
			
			// Do not show the coming soon content.
			if (obj.search("<!-- Content Coming Soon! -->") == -1) {
				if (detailToken == "currentBuildDetails") {
					this.currentReleaseCallback(obj);
				} else if (detailToken == "newBuildDetails") {
					this.newReleaseCallback(obj);
				} else if (detailToken == "releaseDetails") {
					this.nextReleaseCallback(obj);
				}	
			} else {
				Mojo.Log.info("Depot depotCallback: block content coming soon ");
			}	
		} else{
			Mojo.Log.info("Depot: depotCallback not stored");
		} 
		
		this.checkServer(detailToken);
		
	},
	
	checkServer: function(detailToken){		
		Mojo.Log.info("Depot: checkServer", detailToken);

		// always request HTML just incase server text is updated.
		Mojo.Log.info("Depot: Request html from server");
		var url = "";
		if (detailToken == "currentBuildDetails") {
			url = this.getCurrentVersionNotesUrl();
		} else if (detailToken == "newBuildDetails") {
			url = this.getNewVersionNotesUrl();
		} else if (detailToken == "releaseDetails") {
			url = this.getNextVersionNotesUrl();
		}
				
		Mojo.Log.info("Depot: url", url);
		if(url != "") {
			if (detailToken == "currentBuildDetails") {
				this.getCurrentReleaseNotes(url, this.currentXmlHttpSuccessCallback.bind(this, detailToken), this.currentXmlHttpFailureCallback.bind(this, detailToken));
			} else if (detailToken == "newBuildDetails") {
				this.getNewReleaseNotes(url, this.newXmlHttpSuccessCallback.bind(this, detailToken), this.newXmlHttpFailureCallback.bind(this, detailToken));
			} else if (detailToken == "releaseDetails") {
				this.getNextReleaseNotes(url, this.nextXmlHttpSuccessCallback.bind(this, detailToken), this.nextXmlHttpFailureCallback.bind(this, detailToken));
			}
		}
	},

	getCurrentReleaseDetails: function(callback){
		Mojo.Log.info("Depot: getCurrentReleaseDetails");
		if (!callback || callback == null) {
			Mojo.Log.info("Depot callback not set.");
			return;
		}

		this.currentReleaseCallback = callback;
		this.getDepotObject("currentBuildDetails", this.depotCallback.bind(this, "currentBuildDetails"));
	},
	
	getNewReleaseDetails: function(callback){
		Mojo.Log.info("Depot: getNewReleaseDetails");
		if (!callback || callback == null) {
			Mojo.Log.info("Depot callback not set.");
			return;
		}
		this.newReleaseCallback = callback;
		this.getDepotObject("newBuildDetails", this.depotCallback.bind(this, "newBuildDetails"));
	},
	
	getNextReleaseDetails: function(callback){
		Mojo.Log.info("Depot: getNextReleaseDetails");
		if (!callback || callback == null) {
			Mojo.Log.info("Depot callback not set.");
			return;
		}
		this.nextReleaseCallback = callback;		
		this.getDepotObject("releaseDetails", this.depotCallback.bind(this, "releaseDetails"));
	},
	
});

UpdatesDepot.DownloadReleaseNotesToken = "releaseDetails";
UpdatesDepot.InstalledReleaseNotesToken = "newBuildDetails";
UpdatesDepot.CurrentReleaseNotesToken = "currentBuildDetails";

UpdatesDepot.depot = null;
UpdatesDepot.getDepot = function() {
	if (!UpdatesDepot.depot) {
		UpdatesDepot.depot = new UpdatesDepot();
	}
	return UpdatesDepot.depot;
};