import R from "ramda";
import log from "./log";


const delay = (msecs, func) => global.setTimeout(func, msecs);



/**
 * Manages actions that change the state of the service.
 *
 * @param {Object} settings:
 *                  - apps:     Collection of apps to start.
 *                  - mainApi:  The main API.
 *
 */
export default (settings = {}) => {
  const { apps, mainApi } = settings;

  const getApp = (req, res) => {
        const id = req.params.app;
        const app = R.find(item => item.id === id, apps);
        if (app) {
          return app;
        } else {
          res.status(404).send({ message: `The app '${ id }' does not exist.` });
        }
      };


  // API.
  return {
    /**
     * Restarts the gateway and all apps.
     */
    restart(req, res) {
      log.info(`API: Restarting the gateway and all apps.`);
      res.send({ message: "Restarting..." });
      delay(200, () => {
        mainApi
          .restart()
          .catch(err => res.status(500).send({ message: err.message }));
      });
    },


    /**
     * Restarts the app on all containers.
     */
    restartApp(req, res) {
      const app = getApp(req, res);
      if (app) {
        log.info(`API: Restarting app '${ app.id }'...`);
        app.restart()
          .then(result => {
              res.send({ app: app.id, restarted: true, version: result.version });
              log.info(`API:...Restarted app '${ app.id }'.`);
          })
          .catch(err => res.status(500).send({ message: err.message }));
      }
    },


    /**
     * Checks for updates for the app.
     */
    update(req, res) {
      const app = getApp(req, res);
      if (app) {
        log.info(`API: Updating app '${ app.id }'...`);
        app.update()
          .then(result => {
              res.send({ app: app.id, updated: result.updated, version: result.version });
              log.info(`API:...Updated app '${ app.id }'.`);
          })
          .catch(err => res.status(500).send({ message: err.message }));
      }
    }
  };
};

@@ -1883,32 +1883,162 @@
     
-log.info(
+const msg = result.updated%0A                ? %60API: ...updated app '$%7B app.id %7D' to version $%7B result.version %7D.%60%0A                : 
 %60API:
+ 
 ...
-U
+did not u
 pdate
-d
  app
@@ -2043,34 +2043,113 @@
 pp '$%7B app.id %7D'
-.%60
+, latest version $%7B result.version %7D already running.%60%0A              log.info(msg
 );%0A          %7D)%0A

Improve output log message for app/update