
/*!
 * Picky Plugin Mobile
 * @license MIT
 */

/*
 * Module dependencies
 */
var path = require('path');
var util = require('picky-util');
// var Plugin = require('picky-webdriver').plugin;
var Device = require('./device');
var pkg = require('../package');

/*
 * Module variables
 */

var commands = util.loadAll(__dirname, 'cli', 'commands');

/*
 * Module
 */

// var plugin = Plugin({
//   name: 'mobile',
//   dirname: __dirname,
//   Device: Device
// });

/**
 * Parse and transform a given `options`.
 *
 * @param  {object} options - The configuration
 * @return {object} The parsed configuration
 */
function _parseOptions(options, done) {
	var pluginOptions = options.mobile || {};
	// Option `devices`
	options.mobile.devices = util.arrify(options.mobile.devices);

	done();
}

/**
 * The plugin method given a `suite`.
 *
 * @param  {object} suite - The suite
 * @return {object} The plugin instance
 */
function plugin(suite) {
	// Dependencies
	var wd = suite.usePlugin('picky-webdriver', require('picky-webdriver'));
	// Add the plugin's `root`
	suite.addRoot(path.join(__dirname, 'defaults'));
	// API
	suite.api.mobile = {}; // TODO: remove this

	// Instance
	var instance = {};

	// Instance's name
	instance.name = pkg.name;

	// Instance's CLI reference
	instance.cli = commands;

	// Options parser
	instance._parseOptions = _parseOptions;

	// Default configuration
	instance.defaults = {
		mobile: {
			availableDevices: [],
			allowDevices: [],
			devices: []
		}
	};

	// Listen for a new Session creation
	suite.on('create session', function (session) {
		// Register devices into webdriver
		suite.options.mobile.devices.forEach(function (device) {
			// console.log(device)
			wd.devices.push(new Device(session, device));
		});
	});

	return instance;
}

// Instance's CLI reference
plugin.cli = commands;

/*
 * Module exports
 */

module.exports = plugin;
