var request = require('request')
var chalk = require('chalk')
var inquirer = require('inquirer')
var path = require('path')
var yargs = require('yargs')
var Q = require('q')
var opn = require('opn')
var configFilePath = path.join(process.cwd(), yargs.argv.config)
var options = {}
try {
	options = require(configFilePath)
} catch (err) {}

function getViewerItemSlug(itemId, accessToken, productId, cb) {
	var url = 'https://launchpadapi.mediafly.com/2/items/' + itemId +
		'?accessToken=' + accessToken + '&productId=' + productId

	request.get({url: url, json: true}, function(err, response, item) {
			if (err) {
				console.log(chalk.red('Error getting viewer item slug.'))
				return
			}
			cb(item.response.slug)
		})
}

function getProduct(mcode, cb) {
	var url = 'https://accounts.mediafly.com/api/3.0/products/get?mcode=' + mcode + '&accessToken=__anonymous__'

	request({
		json: true,
		uri: url
	}, function(err, response, product) {
		cb(product)
	})
}

function getAccessToken(userId, password, cb) {
	var url = 'https://accounts.mediafly.com/api/3.3/authentication/authenticate?accessType=edit&username=' + 
			encodeURIComponent(userId) + '&password= ' + encodeURIComponent(password)

	request.get({url: url, json: true}, function(err, response, body) {
			if (err) {
				console.log(chalk.red('Error authenticating. Please check your credentials.'))
				return
			}
			cb(body.accessToken)
		})
}

function promptForAccessToken(mcode, authType) {
	if (authType === 'Url') {
		//this content source is SAML
		console.log('Log in with your credentials in the newly opened browser window.')
		console.log('Once you have logged in, copy the Access Token and paste it below.')
		return opn(`https://login.mediafly.com/${mcode}#/login?returnUrl=https%3A%2F%2Fmediafly-mfly-interactive.s3.amazonaws.com%2Faccess-token.html`, { wait: false }).then(function() {
			return inquirer.prompt([{
				name: 'accessToken',
				message: 'Enter Access Token'
			}]).then(function(answers) { return answers.accessToken })
		})
	} else {
		//this is a regular content source
		return inquirer.prompt([{
			name: 'userId',
			message: 'Enter Airship User Id'  
		}, {
			name: 'password',
			message: 'Enter Airship password',
			type: 'password'
		}]).then(function(answers) {
			return new Q.Promise(function(resolve) {
				getAccessToken(answers.userId, answers.password, function(accessToken) {
					resolve(accessToken)
				})
			})
		})
	}
}

function renewAccessToken(cb) {
	getProduct(options.mcode, function(product) {
		promptForAccessToken(options.mcode, product.authentication.authenticationType)
			.then(function(accessToken) {
				cb(accessToken)
			})
	})
}

module.exports = {
	getAccessToken: getAccessToken,
	getViewerItemSlug: getViewerItemSlug,
	getProduct: getProduct,
	promptForAccessToken: promptForAccessToken,
	renewAccessToken: renewAccessToken
}
@@ -195,148 +195,8 @@
 pn')
-%0Avar configFilePath = path.join(process.cwd(), yargs.argv.config)%0Avar options = %7B%7D%0Atry %7B%0A%09options = require(configFilePath)%0A%7D catch (err) %7B%7D
 %0A%0Afu
@@ -2331,16 +2331,161 @@
 n(cb) %7B%0A
+%09var configFilePath = path.join(process.cwd(), yargs.argv.config)%0A%09var options = %7B%7D%0A%09try %7B%0A%09%09options = require(configFilePath)%0A%09%7D catch (err) %7B%7D%0A
 %09getProd

read the accessToken from a given config file