/*
	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';

// Public modules
var Q = require('q');
var mfpConfigXMLAPI = require('mfp-config-xml').mfpConfigXMLAPI;
var errors = require('mdo-errors');
var url = require('url');
var punycode = require('punycode');

// Constants
var K = Object.freeze({
  ANDROID: 'android',
  IOS: 'ios',
  WINDOWS_10: 'windows10',
  WINDOWS_8: 'windows8',
  WINDOWS_PHONE_8: 'windowsphone8',
});

module.exports.getAppConfigKeys = function(configXMLPath, platformIds) {
  var d = Q.defer();
  var configSettings = {};
  var configAPI = new mfpConfigXMLAPI(configXMLPath);

  configSettings['server'] = configAPI.getMFPServerURL();
  configSettings['runtime'] = configAPI.getMFPServerRuntime();
  configSettings['direct_update_authenticity_public_key'] =
    configAPI.getMFPDirectUpdateAuthKey();
  configSettings['language_preferences'] = configAPI.getMFPLanguagePrefs();

  if (platformIds.indexOf('cordova-android') > -1) {
    configSettings['android_security_ignore_file_extensions'] =
      configAPI.getMFPIgnoreFileExtensions(K.ANDROID);
    configSettings['android_security_test_web_resources_checksum'] =
      configAPI.getMFPTestWebResources(K.ANDROID);
  }
  if (platformIds.indexOf('cordova-ios') > -1) {
    configSettings['ios_security_ignore_file_extensions'] =
      configAPI.getMFPIgnoreFileExtensions(K.IOS);
    configSettings['ios_security_test_web_resources_checksum'] =
      configAPI.getMFPTestWebResources(K.IOS);
  }
  if (platformIds.indexOf('cordova-windows10') > -1) {
    configSettings['windows10_security_ignore_file_extensions'] =
      configAPI.getMFPIgnoreFileExtensions(K.WINDOWS_10);
    configSettings['windows10_security_test_web_resources_checksum'] =
      configAPI.getMFPTestWebResources(K.WINDOWS_10);
  }
  if (platformIds.indexOf('cordova-windows8') > -1) {
    configSettings['windows8_security_ignore_file_extensions'] =
      configAPI.getMFPIgnoreFileExtensions(K.WINDOWS_8);
    configSettings['windows8_security_test_web_resources_checksum'] =
      configAPI.getMFPTestWebResources(K.WINDOWS_8);
  }
  if (platformIds.indexOf('cordova-windows8phone') > -1) {
    configSettings['windowsphone8_security_ignore_file_extensions'] =
      configAPI.getMFPIgnoreFileExtensions(K.WINDOWS_PHONE_8);
    configSettings['windowsphone8_security_test_web_resources_checksum'] =
      configAPI.getMFPTestWebResources(K.WINDOWS_PHONE_8);
  }
  d.resolve(configSettings);
  return d.promise;
};

module.exports.setAppConfigKey = function(configXMLPath, key, value) {
  var d = Q.defer();
  var configAPI = new mfpConfigXMLAPI(configXMLPath);
  switch (key) {
    case 'server':
      var parsedURL = url.parse(value);
      var punycodeValue = parsedURL.protocol + '//' +
        punycode.toASCII(parsedURL.hostname) + ':' + parsedURL.port;
      configAPI.setMFPServerURL(punycodeValue);
      break;
    case 'runtime':
      configAPI.setMFPServerRuntime(value);
      break;
    case 'direct_update_authenticity_public_key':
      configAPI.setMFPDirectUpdateAuthKey(value);
      break;
    case 'language_preferences':
      configAPI.setMFPLanguagePrefs(value);
      break;
    case 'android_security_ignore_file_extensions':
      configAPI.setMFPIgnoreFileExtensions(value, K.ANDROID);
      break;
    case 'android_security_test_web_resources_checksum':
      configAPI.setMFPTestWebResources(value, K.ANDROID);
      break;
    case 'ios_security_ignore_file_extensions':
      configAPI.setMFPIgnoreFileExtensions(value, K.IOS);
      break;
    case 'ios_security_test_web_resources_checksum':
      configAPI.setMFPTestWebResources(value, K.IOS);
      break;
    case 'windows10_security_ignore_file_extensions':
      configAPI.setMFPIgnoreFileExtensions(value, K.WINDOWS_10);
      break;
    case 'windows10_security_test_web_resources_checksum':
      configAPI.setMFPTestWebResources(value, K.WINDOWS_10);
      break;
    case 'windows8_security_ignore_file_extensions':
      configAPI.setMFPIgnoreFileExtensions(value, K.WINDOWS_8);
      break;
    case 'windows8_security_test_web_resources_checksum':
      configAPI.setMFPTestWebResources(value, K.WINDOWS_8);
      break;
    case 'windowsphone8_security_ignore_file_extensions':
      configAPI.setMFPIgnoreFileExtensions(value, K.WINDOWS_PHONE_8);
      break;
    case 'windowsphone8_security_test_web_resources_checksum':
      configAPI.setMFPTestWebResources(value, K.WINDOWS_PHONE_8);
      break;
  }
  if (configAPI.writeToFile()) {
    d.resolve();
  } else {
    d.reject(errors.getError('ERRWRITEFILE', configXMLPath));
  }
  return d.promise;
};
