/*
	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 Q = require('q');
var path = require('path');

var exec = require('mdo-exec');
var MFPConfig = require('mfp-config-xml').mfpConfigXMLAPI;
var strings = require('ibm-strings');
var platformMan = require('platform-manager');
var messages = require('./externalizedStrings');


module.exports.printInfo = function() {
    return Q.all([getCordovaVersion(), getInstalledCordovaPlugins(),
        getInstalledCordovaPlatforms(), getCordovaProjectRoot()]).then(
        function (values) {
            // print stock information I know about personally
            printSystemInfo(values[0], values[1], values[2], values[3]);
        }
    );
};

function printSystemInfo(cordovaVersion, installedPlugins, installedPlatforms, cordovaDir) {

    console.log();
    console.log(messages.cordova_info);

    if (cordovaVersion !== messages.cordova_not_installed) {
        console.log(messages.cordova_ver + cordovaVersion);
        console.log(messages.cordova_plugins + installedPlugins);
        console.log(messages.cordova_platforms + installedPlatforms);
        console.log(messages.cordova_app_config + getCordovaConfig(cordovaDir));
    } else
        console.log('  ' + messages.cordova_not_installed);
}

function getCordovaVersion() {
    var def;

    def = Q.defer();

    exec.run('cordova', ['-v']).done(
        function (version) {
            def.resolve(version.stdOut.replace('\n', ''));
        },
        function () {
            def.resolve(messages.cordova_not_installed);
        }
    );

    return def.promise;
}

function getInstalledCordovaPlugins() {
    var def;

    def = Q.defer();

    exec.run('cordova', ['plugin', 'ls']).done(
        function (plugins) {
            plugins = plugins.stdOut.split('\n');
            plugins = plugins.filter(
                function (element) {
                    return element;
                }
            );
            plugins = '\n    ' + plugins.join('\n    ');

            def.resolve(plugins);
        },
        function () {
            def.resolve(messages.not_cordova_proj);
        }
    );

    return def.promise;
}

function getInstalledCordovaPlatforms() {
    var def;

    def = Q.defer();

    exec.run('cordova', ['platform', 'ls']).done(
        function (platforms) {
            platforms = platforms.stdOut.split(':')[1];
            platforms=  platforms.split('Available platforms')
            platforms = '\n    ' + platforms.join('\n    ');

            def.resolve(platforms);
        },
        function () {
            def.resolve(messages.not_cordova_proj);
        }
    );

    return def.promise;
}

function getCordovaProjectRoot() {
    var def;

    def = Q.defer();

    platformMan.detectPlatforms().then(
        function (info) {
            def.resolve(info.rootDir);
        },
        function () {
            def.resolve();
        }
    );

    return def.promise;
}

function getCordovaConfig(rootDir) {
  var mfpConfig;
  var res = '';

  function addLine(message, value) {
    res += '\n    ';
    res += strings.format(message, value);
  }

  try {
    mfpConfig = new MFPConfig(path.join(rootDir, 'config.xml'));
    addLine(messages.cordovaNameSpace, mfpConfig.getMFPNameSpace());
    addLine(messages.cordovaWidgetId, mfpConfig.getWidgetId());
    addLine(messages.cordovaWidgetVersion, mfpConfig.getWidgetVersion());
    addLine(messages.cordovaWidgetName, mfpConfig.getWidgetName());
    addLine(messages.cordovaMainFile, mfpConfig.getMainFile());

    addLine(messages.cordovaMFPSDKChecksumAndroid, mfpConfig.getMFPSDKChecksum('android'));
    addLine(messages.cordovaMFPSDKChecksumIOS, mfpConfig.getMFPSDKChecksum('ios'));
    addLine(messages.cordovaMFPSDKChecksumWindows10, mfpConfig.getMFPSDKChecksum('windows10'));
    addLine(messages.cordovaMFPSDKChecksumWindows8, mfpConfig.getMFPSDKChecksum('windows8'));
    addLine(messages.cordovaMFPSDKChecksumWindowsPhone8, mfpConfig.getMFPSDKChecksum('windowsphone8'));

    addLine(messages.cordovaMFPPlatformVersion, mfpConfig.getMFPPlatformVersion());
    addLine(messages.cordovaMFPClientCustomInit, mfpConfig.getMFPClientCustomInit());
    addLine(messages.cordovaMFPServerURL, mfpConfig.getMFPServerURL());
    addLine(messages.cordovaMFPServerRuntime, mfpConfig.getMFPServerRuntime());
    addLine(messages.cordovaMFPDirectUpdateAuthKey, mfpConfig.getMFPDirectUpdateAuthKey());

    addLine(messages.cordovaMFPTestWebResourcesAndroid, mfpConfig.getMFPTestWebResources('android'));
    addLine(messages.cordovaMFPTestWebResourcesIOS, mfpConfig.getMFPTestWebResources('ios'));
    addLine(messages.cordovaMFPTestWebResourcesWindows10, mfpConfig.getMFPTestWebResources('windows10'));
    addLine(messages.cordovaMFPTestWebResourcesWindows8, mfpConfig.getMFPTestWebResources('windows8'));
    addLine(messages.cordovaMFPTestWebResourcesWindowsPhone8, mfpConfig.getMFPTestWebResources('windowsphone8'));

    addLine(messages.cordovaMFPIgnoreFileExtensionsAndroid, mfpConfig.getMFPIgnoreFileExtensions('android'));
    addLine(messages.cordovaMFPIgnoreFileExtensionsIOS, mfpConfig.getMFPIgnoreFileExtensions('ios'));
    addLine(messages.cordovaMFPIgnoreFileExtensionsWindows10, mfpConfig.getMFPIgnoreFileExtensions('windows10'));
    addLine(messages.cordovaMFPIgnoreFileExtensionsWindows8, mfpConfig.getMFPIgnoreFileExtensions('windows8'));
    addLine(messages.cordovaMFPIgnoreFileExtensionsWindowsPhone8, mfpConfig.getMFPIgnoreFileExtensions('windowsphone8'));

    addLine(messages.cordovaMFPLanguagePrefs, mfpConfig.getMFPLanguagePrefs());
  } catch (err) {
    res = messages.not_cordova_proj;
  }

  return res;
}


