'use strict';

var page = require('webpage').create(),
system = require('system'),

/* 
This script takes two input values -> 
The custom width and height of the viewport in dppx that we would be generating screenshots for;

NOTE the default user agent string is set to Google Chrome 41
The next URL indicates where we intend to navigate to and take a screenshot next;
TO-DO: Perhaps all of the configuration below can be moved into a configuration file?
*/
customWidth, customHeight, customViewPortSize, loadedPage,
userAgent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
baseURL = 'http://mymsd.mybluemix.net',
nextURL = baseURL;


//------------------------------------------------------------------
// All declarations above this section!
//------------------------------------------------------------------


// Initializing the argument list
customWidth = system.args[1];
customHeight = system.args[2];

// The page settings go here
// Our default useragent is set to Google Chrome 41 (WebKit layout engine again!)
page.settings.javascriptEnabled = true;
page.settings.loadImages = true;
page.settings.userAgent = userAgent;

// Set the default width and height to 480x800, if the user forgets to pass them as arguments
if (customWidth === undefined || customHeight === undefined){
	console.log('The width and/or height of the viewport appears to be undefined. Defaulting to "480x800"!');
	customWidth = 480;
	customHeight = 800;
} 

page.viewportSize = {
	width: customWidth, 
	height: customHeight
};

// This specifies the scaling factor for page.render
// The default is 1 (which corresponds to 100% zoom)
// 0.5 looks like it works best for PDF files!
page.zoomFactor = 0.5;

// customViewPortSize here is used to append to names of screenshots
customViewPortSize = customWidth + 'x' + customHeight;


//------------------------------------------------------------------
// All assignments above this section!
//------------------------------------------------------------------


// Logging all variables to the console
console.log('====================================================');
console.log('Viewport Size: ' + customViewPortSize);
console.log('JavaScript Enabled: true');
console.log('User Agent: ' + userAgent);
console.log('Load Images: true');
console.log('====================================================');


// Exit if we're unable to load the page or if there are errors!
function exitOnError(status) {
	if (status !== 'success') {
		console.log('Unable to load page. Network issues?');
		phantom.exit(1);
	}
};


//------------------------------------------------------------------
// All re-usable functions above this section.
// All the navigational madness below this section!
//------------------------------------------------------------------


// Take a screenshot of the landing page
// TO-DO: Get loadedPage to work with this script!
loadedPage = 'landing_page';
page.open(baseURL);
function takeScreenshot() {

	/* 
	For all of those weird cases where landing_page is undefined for some strange reason,
	we set loadedPage to the time-stamp to ensure it's unique!
	*/ 
	if (loadedPage === undefined) {
		loadedPage = Date.now();
	}

	// Do we even need nextURL if all we're doing is navigating from this point forward?
  	
  	page.render(loadedPage + '-' + customViewPortSize + '.pdf', {format: 'pdf', quality: '100'});
 	console.log('Screenshot for ' + loadedPage + ' saved!');
}

page.onLoadFinished = function(status) {
	exitOnError(status);
	console.log('Finished loading ' + nextURL);

	takeScreenshot();
	phantom.exit();
};
