/*
	Licensed Material - Property of IBM

	(C) Copyright 2015,2016 IBM Corp.

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/


'use strict';

var strings = require('ibm-strings');
var inquirer = require("inquirer");
var q = require("q");
//private
var externalizedStrings = require('./externalizedStrings');

/**
 * Method to prompt users for information pertaining to their project if it could not be parsed
 * @param defaults - contains the default suggestions pulled from corresponding "platform file",
 *    Such as WMAppManifest.xml for WP8 projects
 * @returns - an array containing the answers provided by user
 */
function runInteractivePrompt(defaultPlatformValues, platform) {
	var inputPromise = q.defer();
  var dotNotatedIdName;

	if(platform === 'android' || platform === 'browser') {
		dotNotatedIdName = 'applicationId';
	} else if(platform === 'ios') {
		dotNotatedIdName = 'bundleId';
	} else {
		dotNotatedIdName = 'assemblyName';
	}

	//Need to pull strings out into file which has nls support
	var questions = [
	{
		when: function () {
			return defaultPlatformValues['app.dotId'] === '';
		},
		type: 'input',
		name: "app.dotId",
		message: strings.format(externalizedStrings.enterAppIdMessage, dotNotatedIdName),
		validate: function (input) {
			var isValid = input.match('^[a-zA-Z0-9.]*$');

			if (isValid) {
				return true;
			}
			else {
				return externalizedStrings.promptValidBundleId;
			}
		}
	},
	{
		when: function () {
			return defaultPlatformValues['app.version'] === '';
		},
		type: 'input',
		name: "app.version",
		message: externalizedStrings.enterVersionMessage,
		validate: function (input) {
			var isValid = input.match(/^([1-9]\d+|[0-7])(\.\d{1,3}){0,3}$/);
			if (isValid) {
				return true;
			}
			return externalizedStrings.validVersionNumber;
		}
	}];

	inquirer.prompt( questions, function( answers ) {
		if(!answers["app.dotId"]) {
			answers["app.dotId"] = defaultPlatformValues["app.dotId"];
		} else {
			//We need to display a message so users can prevent future prompting if platform === ios (since it is the most problamatic)
			if(platform === 'ios') {
				strings.warn(externalizedStrings.setIOSBundleId);
			}
		}
		if(!answers["app.version"]) {
			answers["app.version"] = defaultPlatformValues["app.version"];
		}

		answers['app.iosId'] = defaultPlatformValues['app.iosId'];
		answers['app.androidId'] = defaultPlatformValues['app.androidId'];

		inputPromise.resolve(answers);
	});

	return inputPromise.promise;
}

module.exports = runInteractivePrompt;
