#! /usr/bin/env node

// Get a list of models in the user's account

var program = require("commander");
var prompt = require("cli-prompt");
var Table = require("cli-table");
var fs = require("fs");

var ImpConfig = require("../lib/impConfig.js");
var config = new ImpConfig();

program
    .option("--active", "Filters list to only display active models")
    .option("--inactive", "Filters list to only display inactive models")

program.parse(process.argv);

config.init(["apiKey"], function(err, success) {
    if (err) {
        console.log("ERROR: Could not find a Build API key. Run 'imp setup' to set your global Build API key");
        return;
    }

    imp = config.createImpWithConfig();

    if ("active" in program && "inactive" in program) {
        console.log("ERROR: You cannot specify --active and --inactive");
        return;
    }

    var activeState = null;
    if ("active" in program) activeState = true;
    if ("inactive" in program) activeState = false;

    imp.getModels(null, function(err, modelData) {
        if (err) {
            console.log("ERROR: " + err.message_short);
            return;
        }

        imp.getDevices(null, function(err, deviceData) {
            if (err) {
                console.log("ERROR: " + err.message_short);
                return;
            }

            var filteredModels = [];

            modelData.models.some(function(model) {
                if (activeState == null) {
                    filteredModels.push(model);
                } else if (activeState) {
                    deviceData.devices.some(function(device) {
                        if (device.model_id == model.id) {
                            filteredModels.push(model);
                            return true;
                        }
                    });
                } else {
                    var found = false;
                    deviceData.devices.some(function(device) {
                        if (device.model_id == model.id) {
                            found = true;
                            return true;
                        }
                    });

                    if (!found) filteredModels.push(model);
                }
            });

            if (filteredModels.length > 0) {
                // Create the model list as a table
                var table = new Table({
                    head: ['Model ID', 'Model Name']
                    , colWidths: [20, 30]
                });

                filteredModels.forEach(function(model) {
                    table.push([model.id, model.name]);
                })

                console.log(table.toString());
            } else {
                // Report there are no found models
                console.log("No models meet your search criteria");
            }
        });
    });
});

@@ -551,88 +551,85 @@
 OR: 
-Could not find a Build API key. Run 'imp setup' to set your global Build API key
+Global Build API key is not set - run 'imp setup' then try 'imp models' again
 %22);%0A
@@ -1348,17 +1348,16 @@
 s = %5B%5D;%0A
-%0A
         
@@ -1439,24 +1439,99 @@
  == null) %7B%0A
+                    // Not filtering by state, so just push it to the list%0A
             
@@ -1604,24 +1604,133 @@
 iveState) %7B%0A
+                    // Filtering by state == active, so check for%0A                    // device associations%0A
             
@@ -1831,32 +1831,190 @@
  == model.id) %7B%0A
+                            // At least one device is listing this as its model,%0A                            // so this model is active - push it to the list%0A
                 
@@ -2161,32 +2161,86 @@
        %7D else %7B%0A
+                    // Filtering by state == inactive%0A
                 
@@ -2510,32 +2510,160 @@
            %7D);%0A%0A
+                    // Model is not associated with any device, ie. it's inactive%0A                    // so push it to the list%0A
                 
@@ -2809,24 +2809,45 @@
  // 
-Create the model
+We have models to list, so create the
  lis
@@ -3321,14 +3321,14 @@
 our 
-search
+filter
  cri

Update imp-models.js