#!/usr/bin/env node
'use strict';

var pkg = require('./package.json');
var ipaMetadata = require('./');
var meow = require('meow');

var Table = require('cli-table');
var _ = require('lodash');
var chalk = require('chalk');

var cli = meow({
  help: [
    'Example',
    '  ipa-metadata Facebook.ipa',
    '',
    '  ipa-metadata Facebook.ipa --verbose',
    '',
    '  ipa-metadata Facebook.ipa --verify',
    '  (verifies entitlements between `.app` bundle and `embedded.mobileprovision`)',
    '',
    '  => data displayed in a table'
  ].join('\n')
});

function format(value, compare) {
  if(!_.isPlainObject(value)) {
    return value;
  }

  var string = [];
  _.each(value, function(value, key) {
    var shouldCompareEquality = cli.flags.verify && compare;

    if(shouldCompareEquality && (key === 'keychain-access-groups')){
      return string.push(compareKeychainEntitlement(value, key, compare[key]), '\n');
    }

    if(shouldCompareEquality){
      var match = value === compare[key];
      var stringResult = chalk[match ? 'green' : 'red'](key + ': ' + format(value));
      return string.push(stringResult, '\n');
    }

    string.push(key, ': ', format(value), '\n');
  });
  string.pop();

  return string.join('');
}

function compareKeychainEntitlement(value, key, compare) {
  function splitKeychain(value) {
    return value[0].split('.')[0];
  }

  var match = splitKeychain(value) === splitKeychain(compare);
  return chalk[match ? 'green' : 'red'](key + ': ' + format(value));
}

function verboseFormatting(data){
  var types = Object.keys(data);

  _.each(types, function(type){
    var table = new Table();
    _.each(data[type], function(value, key){
      table.push([key, format(value)]) ;
    });

    console.log(table.toString());
  });
}

if(!cli.input[0]){
  return cli.showHelp();
}

ipaMetadata(cli.input[0], function(error, data){
  if(error){
    return console.log(error.message);
  }

  if(cli.flags.verbose){
    return verboseFormatting(data);
  }

  var types = {
    metadata: ['CFBundleDisplayName', 'CFBundleIdentifier', 'CFBundleShortVersionString', 'CFBundleVersion'],
    provisioning: ['Name', 'TeamIdentifier', 'TeamName', 'Entitlements']
  };

  var table = new Table();
  _.each(types, function(keys, type) {
    if(data[type]){
      _.each(keys, function(key) {
        var value = data[type][key];
        table.push([key, format(value, data.entitlements)]);
      });
    }
  });

  console.log(table.toString());
});

@@ -760,32 +760,33 @@
 ompare;%0A%0A    if(
+!
 shouldCompareEqu
@@ -790,20 +790,90 @@
 Equality
- && 
+)%7B%0A      return string.push(key, ': ', format(value), '%5Cn');%0A    %7D%0A%0A    if
 (key ===
@@ -898,17 +898,16 @@
 groups')
-)
 %7B%0A      
@@ -1001,254 +1001,71 @@
     
-if(shouldCompareEquality)%7B%0A      var match = value === compare%5Bkey%5D;%0A      var stringResult = chalk%5Bmatch ? 'green' : 'red'%5D(key + ': ' + format(value));%0A      return string.push(stringResult, '%5Cn');%0A    %7D%0A%0A    string.push(key, ': ', format(value
+return string.push(compareOutputString(value, compare%5Bkey%5D, key
 ), '
@@ -1261,20 +1261,35 @@
 %0A%0A  
-var match = 
+return compareOutputString(
 spli
@@ -1304,20 +1304,17 @@
 n(value)
- ===
+,
  splitKe
@@ -1328,17 +1328,110 @@
 compare)
-;
+, key);%0A%7D%0A%0Afunction compareOutputString(value, target, key) %7B%0A  var match = value === target;%0A
 %0A  retur

Simplify CLI code for outputting comparison data