/*
	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.
*/


'use strict';

//private modules
var ConfigXMLAPI = require('mfp-config-xml');
//public modules
var log = require('npmlog');

var moduleName = 'generateDeploymentData';

/**
// Returns a JSON object representing the deployment.data file contents.  Each
// JSON attribute is the deployment.data attribute.
//
// Parameters:
//   @param platform - the platform of the project
//   @param unpackedSize - the unpacked size of the www directory
**/
module.exports = function (platform, unpackedSize) {
  log.silly('generateDeploymentData','Building deployment.data');

  var configXMLAPI = new ConfigXMLAPI.mfpConfigXMLAPI(platform.configXMLPath);
  var props = { };

  props['app.id'] = getAppId(platform.platform, configXMLAPI);

  props['app.version'] = configXMLAPI.getWidgetVersion();
  props['app.displayname'] = configXMLAPI.getWidgetName();
  props['app.platform'] = platform.platform;
  props['platform.version'] = configXMLAPI.getMFPPlatformVersion();
  props['native.sdk.checksum'] = configXMLAPI.getMFPSDKChecksum(platform.platform);
  props['app.checksum'] = configXMLAPI.getMFPAppChecksum(platform.platform.indexOf('windows') >= 0 ? 'windows' : platform.platform);
  props['app.unpacked.size'] = unpackedSize;
  props['app.build.time'] = new Date().getTime();

  log.silly(moduleName, JSON.stringify(props));
  return props;
};

function getAppId(platform, configXMLAPI) {
	var appId = configXMLAPI.getWidgetId();

	// Android environment with customer package name in config.xml
	if(platform === 'android' && configXMLAPI.android_packageName()) {
		appId = configXMLAPI.android_packageName();
	}
	// iOS environment with custom bundle identifier in config.xml
	else if(platform === 'ios' && configXMLAPI.ios_CFBundleIdentifier()) {
		appId = configXMLAPI.ios_CFBundleIdentifier();
	}

	return appId;
}
