/*
	Licensed Material - Property of IBM

	(C) Copyright 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.
*/


var externalizedStrings = require('../externalizedStrings');
var Q = require('q');
var profileIO = require('mdo-server-support').profileIO;
var consts = require('./consts');
var log = require('npmlog');

function logVerbose(message) { log.verbose(consts.MODULE, message); }

var keyPrompts = {
  'server': {
    type: 'input',
    name: 'server',
    message: externalizedStrings.serverPrompt,
    description: externalizedStrings.serverDescription,
    readable: externalizedStrings.serverReadable,
    // NOTE: validate function not defined since we create it on the fly,
    // as we need to get the valid server profiles
  },
  'runtime': {
    type: 'input',
    name: 'runtime',
    message: externalizedStrings.runtimePrompt,
    description: externalizedStrings.runtimeDescription,
    readable: externalizedStrings.runtimeReadable,
    default: 'mfp',
    validate: function(input) { // input param if needed
      return (/[^a-zA-Z0-9]/).test(input) ?
             externalizedStrings.enterAlphanumeric : true;
    }
  },
  'direct_update_authenticity_public_key': {
    type: 'input',
    name: 'direct_update_authenticity_public_key',
    message: externalizedStrings.directUpdateAuthenticityPublicKeyPrompt,
    description: externalizedStrings.directUpdateAuthenticityPublicKeyDescription,
    readable: externalizedStrings.directUpdateAuthenticityPublicKeyReadable,
    default: 'mfp_public_key',
    validate: function() {
      return true;
    }
  },

  'language_preferences': {
    type: 'input',
    name: 'language_preferences',
    message: externalizedStrings.languagePreferencesPrompt,
    description: externalizedStrings.languagePreferencesDescription,
    readable: externalizedStrings.languagePreferencesReadable,
    default: 'en',
  },

  'android_security_test_web_resources_checksum': {
    type: 'list',
    name: 'android_security_test_web_resources_checksum',
    message: externalizedStrings.androidTestWebResourcesChecksumPrompt,
    description: externalizedStrings.androidTestWebResourcesChecksumDescription,
    readable: externalizedStrings.androidTestWebResourcesChecksumReadable,
    default: 'false',
    choices: ['false', 'true'],
    validate: function(input) {
      return input === 'true' || input === 'false';
    }
  },
  'android_security_ignore_file_extensions': {
    type: 'input',
    name: 'android_security_ignore_file_extensions',
    message: externalizedStrings.androidIgnoreFileExtensionsPrompt,
    description: externalizedStrings.androidIgnoreFileExtensionsDescription,
    readable: externalizedStrings.androidIgnoreFileExtensionsReadable,
    default: 'png, mp3'
  },

  'ios_security_test_web_resources_checksum': {
    type: 'list',
    name: 'ios_security_test_web_resources_checksum',
    message: externalizedStrings.iosTestWebResourcesChecksumPrompt,
    description: externalizedStrings.iosTestWebResourcesChecksumDescription,
    readable: externalizedStrings.iosTestWebResourcesChecksumReadable,
    default: 'false',
    choices: ['false', 'true'],
    validate: function(input) {
      return input === 'true' || input === 'false';
    }
  },
  'ios_security_ignore_file_extensions': {
    type: 'input',
    name: 'ios_security_ignore_file_extensions',
    message: externalizedStrings.iosIgnoreFileExtensionsPrompt,
    description: externalizedStrings.iosIgnoreFileExtensionsDescription,
    readable: externalizedStrings.iosIgnoreFileExtensionsReadable,
    default: 'png, mp3',
  },

  'windows10_security_test_web_resources_checksum': {
    type: 'list',
    name: 'windows10_security_test_web_resources_checksum',
    message: externalizedStrings.windows10TestWebResourcesChecksumPrompt,
    description: externalizedStrings.windows10TestWebResourcesChecksumDescription,
    readable: externalizedStrings.windows10TestWebResourcesChecksumReadable,
    default: 'false',
    choices: ['false', 'true'],
    validate: function(input) {
      return input === 'true' || input === 'false';
    }
  },
  'windows10_security_ignore_file_extensions': {
    type: 'input',
    name: 'windows10_security_ignore_file_extensions',
    message: externalizedStrings.windows10IgnoreFileExtensionsPrompt,
    description: externalizedStrings.windows10IgnoreFileExtensionsDescription,
    readable: externalizedStrings.windows10IgnoreFileExtensionsReadable,
    default: 'png, mp3',
  },

  'windows8_security_test_web_resources_checksum': {
    type: 'list',
    name: 'windows8_security_test_web_resources_checksum',
    message: externalizedStrings.windows8TestWebResourcesChecksumPrompt,
    description: externalizedStrings.windows8TestWebResourcesChecksumDescription,
    readable: externalizedStrings.windows8TestWebResourcesChecksumReadable,
    default: 'false',
    choices: ['false', 'true'],
    validate: function(input) {
      return input === 'true' || input === 'false';
    }
  },
  'windows8_security_ignore_file_extensions': {
    type: 'input',
    name: 'windows8_security_ignore_file_extensions',
    message: externalizedStrings.windows8IgnoreFileExtensionsPrompt,
    description: externalizedStrings.windows8IgnoreFileExtensionsDescription,
    readable: externalizedStrings.windows8IgnoreFileExtensionsReadable,
    default: 'png, mp3',
  },

  'windowsphone8_security_test_web_resources_checksum': {
    type: 'list',
    name: 'windowsphone8_security_test_web_resources_checksum',
    message: externalizedStrings.windowsphone8TestWebResourcesChecksumPrompt,
    description: externalizedStrings.windowsphone8TestWebResourcesChecksumDescription,
    readable: externalizedStrings.windowsphone8TestWebResourcesChecksumReadable,
    default: 'false',
    choices: ['false', 'true'],
    validate: function(input) {
      return input === 'true' || input === 'false';
    }
  },
  'windowsphone8_security_ignore_file_extensions': {
    type: 'input',
    name: 'windowsphone8_security_ignore_file_extensions',
    message: externalizedStrings.windowsphone8IgnoreFileExtensionsPrompt,
    description: externalizedStrings.windowsphone8IgnoreFileExtensionsDescription,
    readable: externalizedStrings.windowsphone8IgnoreFileExtensionsReadable,
    default: 'png, mp3',
  },
};

/*
 * Given a profileInfo object, return an array of the form:
 * [
 *   {
 *     name: <server name>,
 *     value: {
 *       name: <server name>
 *       url: <server url>
 *     }
 *   ...
 * ]
 * to be used for the inquirer prompt.
 */
function getServerChoices(profileInfo) {
  return Object.keys(profileInfo.servers).map(function(serverName) {
    return serverName;
  });
}

function getServerURL(serverName) {
  var d = Q.defer();
  // Get the server profile details
  profileIO.getProfileDetails(serverName)
  .then(function(profileDetail) {
    // Return the url
    var host = profileDetail.ipAddress ? profileDetail.ipAddress :
      profileDetail.host;
    var url = profileDetail.protocol + '://' + host + ':' + profileDetail.port;
    logVerbose(profileDetail);
    logVerbose(url);
    d.resolve(url);
  })
  .catch(function() {
    d.reject();
  });
  return d.promise;
}

module.exports.getPromptForKey = function(key, configSettings) {
  var d = Q.defer();
  var keyPrompt = keyPrompts[key];

  if (key === 'server') {
    // Setting 'server' key, get list of servers first
    profileIO.getServerProfiles().then(function(profileInfo) {
      logVerbose(profileInfo);
      keyPrompt.choices = getServerChoices(profileInfo);
      keyPrompt.choices.push({
        name: externalizedStrings.enterServerURL,
        value: consts.ENTER_SERVER_URL
      });
      keyPrompt.type = 'list';

      d.resolve(keyPrompt);
    }).fail(d.reject);
  } else {
    // Setting a key other than 'server'
    keyPrompt.default = configSettings[key] || keyPrompt.default;
    d.resolve(keyPrompt);
  }
  return d.promise;
};

module.exports.getHumanReadableForKey = function(key) {
  return keyPrompts[key].readable;
};

module.exports.getMessageForKey = function(key) {
  return keyPrompts[key].message;
};

module.exports.getDescriptionForKey = function(key) {
  return keyPrompts[key].description;
};

module.exports.getValidateForKey = function(key) {
  return keyPrompts[key].validate || function() { return true; };
};

module.exports.getServerURL = getServerURL;
