// Copyright © 2014 Intel Corporation. All rights reserved.
// Use  of this  source  code is  governed by  an Apache v2
// license that can be found in the LICENSE-APACHE-V2 file.

/**
 * The AndroidTargets object wraps targets enumeration and selection.
 * @param {String} buffer Output from "android list targets" for evaluation.
 * @throws {TypeError} When buffer is not a string.
 * @constructor
 */
function AndroidTargets(buffer) {

    if (typeof buffer != "string") {
        throw new TypeError("AndroidTargets(buffer) must be of type String (is " + typeof buffer + ").");
    }

    this._buffer = buffer;
}

/**
 * Parse SDK targets buffer passed to the constructor.
 * @returns Targets object in the form of { target : ABIs }.
 * @memberOf AndroidTargets
 */
AndroidTargets.prototype.parse = function() {

    var lines = this._buffer.split('\n');
    var target = null;
    var targets = {};
    lines.forEach(function(line) {

        if (target == null) {
            // Look for target paragraph.
            var match = "id: ";
            if (line.substring(0, match.length) === match &&
                line.indexOf('"') < line.lastIndexOf('"')) {

                target = line.substring(line.indexOf('"') + 1,
                                        line.lastIndexOf('"'));
            }
        } else {
            // Look for ABI line.
            var match = " Tag/ABIs : ";
            var abis = line.substring(line.indexOf(':') + 1, line.length);
            if (line.substring(0, match.length) === match &&
                abis != " no ABIs.") {

                targets[target] = abis;
                target = null;
            }
        }
    });

    return targets;
};

/**
 * Pick preferred target for creation of new projects.
 * @returns Target identifier string or null if no ABIs are installed.
 * @memberOf AndroidTargets
 */
AndroidTargets.prototype.pick = function() {

    var targets = this.parse();

    var pick1 = null;
    var pick2 = null;
    var pick3 = null;

    for (target in targets) {
        var abis = targets[target];

        if (abis.indexOf("armeabi-v7a") > -1 &&
            abis.indexOf("x86") > -1) {

            // Both ABIs, best pick.
            // If already having a pick, update only if it's "default" (not TV) target
            if (pick1 != null && abis.indexOf("default") > -1) {
                pick1 = target;
            } else if (pick1 == null) {
                pick1 = target;
            }

        } else if (abis.indexOf("armeabi-v7a") > -1) {

            // Let's be honest.
            // If already having a pick, update only if it's "default" (not TV) target
            if (pick2 != null && abis.indexOf("default") > -1) {
                pick2 = target;
            } else if (pick2 == null) {
                pick2 = target;
            }

        } else if (abis.indexOf("x86") > -1) {

            // If already having a pick, update only if it's "default" (not TV) target
            if (pick3 != null && abis.indexOf("default") > -1) {
                pick3 = target;
            } else if (pick3 == null) {
                pick3 = target;
            }
        }
    }

    return pick1 != null ? pick1 :
           pick2 != null ? pick2 :
           pick3 != null ? pick3 :
           null;
};

module.exports = AndroidTargets;

@@ -930,24 +930,55 @@
 on(line) %7B%0A%0A
+        var match = null;%0A    %0A
         if (
@@ -986,16 +986,17 @@
 arget ==
+=
  null) %7B
@@ -1042,36 +1042,32 @@
 ph.%0A            
-var 
 match = %22id: %22;%0A
@@ -1385,36 +1385,32 @@
 ne.%0A            
-var 
 match = %22 Tag/AB
@@ -2038,16 +2038,20 @@
    for (
+var 
 target i
@@ -2333,24 +2333,25 @@
 if (pick1 !=
+=
  null && abi
@@ -2442,16 +2442,17 @@
 pick1 ==
+=
  null) %7B
@@ -2694,24 +2694,25 @@
 if (pick2 !=
+=
  null && abi
@@ -2803,16 +2803,17 @@
 pick2 ==
+=
  null) %7B
@@ -3015,24 +3015,25 @@
 if (pick3 !=
+=
  null && abi
@@ -3124,16 +3124,17 @@
 pick3 ==
+=
  null) %7B
@@ -3216,16 +3216,17 @@
 pick1 !=
+=
  null ? 
@@ -3252,16 +3252,17 @@
 pick2 !=
+=
  null ? 
@@ -3288,16 +3288,17 @@
 pick3 !=
+=
  null ? 

Make lint happy