/* global describe, it */
/* jslint node: true, esnext: true */

"use strict";

const chai = require('chai'),
  assert = chai.assert,
  expect = chai.expect,
  should = chai.should(),
  request = require("supertest-as-promised")(Promise),
  kronos = require('kronos-service-manager'),
  admin = require('../lib/manager.js'),
  wt = require('koa-jwt');

chai.use(require("chai-as-promised"));

let testPort = 12345;

describe('service manager admin', function () {
  const flowDecl = {
    "name": "flow1",
    "type": "kronos-flow",
    "steps": {
      "s0": {
        "type": "kronos-stdin",
        "endpoints": {
          "out": "s1/in"
        }
      },
      "s1": {
        "type": "kronos-copy",
        "endpoints": {
          "out": "s2/in"
        }
      },
      "s2": {
        "type": "kronos-stdout"
      }
    }
  };

  function shutdownManager(manager, done) {
    return function (error, result) {
      manager.shutdown().then(function () {
        if (error) {
          return done(error); 
        } else done();
      }, done);
    };
  }

  function initManager() {
    return admin.manager(kronos.manager({
      name: 'myManager'
    }), {
      port: testPort
        /*,
              jwt: { secret: "the secret" }*/
    }).then(function (manager) {
      require('kronos-step-stdio').registerWithManager(manager);
      require('kronos-flow').registerWithManager(manager);
      manager.registerFlow(manager.getStepInstance(flowDecl));
      return manager;
    });

    testPort++; // TODO somehow koa-websocket does not shutdown correctly
  }

  describe('flows', function () {
    it('GET /flows', function (done) {
      initManager().then(function (manager) {
        request(manager.app.listen())
          .get('/flows')
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(function (res) {
            const response = JSON.parse(res.text);
            //console.log(`RES: ${JSON.stringify(response)}`);
            if (response[0].url !== 'flow1') throw Error("flow missing");
          })
          .expect(200)
          .end(shutdownManager(manager, done));
      }, done);
    });
    it('GET /flows/flow1', function (done) {
      initManager().then(function (manager) {
        request(manager.app.listen())
          .get('/flows/flow1')
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(200)
          .expect(function (res) {
            //console.log(`reponse: ${res.text}`);
            const response = JSON.parse(res.text);
            if (response.name !== 'flow1') throw Error("flow flow1 missing");
          })
          .end(shutdownManager(manager, done));
      }, done);
    });

    it('DELETE /flows/flow1', function (done) {
      initManager().then(function (manager) {
        request(manager.app.listen())
          .delete('/flows/flow1')
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(200)
          .expect(function (res) {
            const response = JSON.parse(res.text);
            if (Object.keys(response).length > 0) throw Error("delete error");
          })
          /*
                    .get('/flows')
                    .expect(200)
                    .expect(function(res) {
                      const response = JSON.parse(res.text);
                      console.log(`RES: ${JSON.stringify(response)}`);
                    })
          */
          .end(shutdownManager(manager, done));
      }, done);
    });

    xit('POST /flows', function (done) {
      initManager().then(function (manager) {
        request(manager.app.listen())
          .post('/flows')
          .send({
            "name": "a",
            "type": "kronos-flow",
            "steps": {}
          })
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(200)
          .expect(function (res) {
            console.log(res.text);
            //const response = JSON.parse(res.text);
            //if (response.name !== 'flow1') throw Error("flow flow1 missing");
          })
          .end(shutdownManager(manager, done));
      }, done);
    });

    xit('POST /flows with error', function (done) {
      initManager().then(function (manager) {
        request(manager.app.listen())
          .post('/flows')
          .send({
            "a": {
              "steps": {
                "s1": {
                  "type": "no-such-type"
                }
              }
            }
          })
          .set('Accept', 'application/json')
          .expect('Content-Type', /json/)
          .expect(200)
          .expect(function (res) {
            console.log(res.text);
            //const response = JSON.parse(res.text);
            //if (response.name !== 'flow1') throw Error("flow flow1 missing");
          })
          .end(shutdownManager(manager, done));
      }, done);
    });

  });
});

@@ -3560,33 +3560,32 @@
 );%0A    %7D);%0A%0A    
-x
 it('POST /flows'
@@ -4232,17 +4232,16 @@
 );%0A%0A    
-x
 it('POST
@@ -4424,16 +4424,55 @@
    %22
-a%22: %7B
+name%22: %22a%22,
 %0A  
+          %22type%22: %22kronos-flow%22,%0A
     
@@ -4500,26 +4500,24 @@
             
-  
 %22s1%22: %7B%0A    
@@ -4520,34 +4520,32 @@
                 
- 
- 
 %22type%22: %22no-such
@@ -4555,26 +4555,8 @@
 pe%22%0A
-                %7D%0A
     

enable post flow tests