const path = require('path'),
  process = require('process');

import { sshServer } from './ssh';
import { mergeAttributes, createAttributes } from 'model-attributes';
import { ReceiveEndpoint } from 'kronos-endpoint';
import { Service, defineServiceConsumerProperties } from 'kronos-service';

/**
 *
 */
export class ServiceAdmin extends Service {
  /**
   * @return {string} 'admin'
   */
  static get name() {
    return 'admin';
  }

  static get configurationAttributes() {
    return mergeAttributes(
      createAttributes({
        ssh: {
          description: 'ssh admin interface',
          attributes: {
            port: {
              description: 'listen port',
              needsRestart: true,
              type: 'ip-port'
            },
            hostKeys: {
              description: 'server ssh private host key(s)',
              type: 'blob',
              needsRestart: true
            }
          }
        }
      }),
      Service.configurationAttributes
    );
  }

  constructor(config, owner) {
    super(config, owner);

    this.addEndpoint(
      new ReceiveEndpoint('software', this)
    ).receive = async request => {
      return {
        versions: process.versions,
        platform: process.platform
      };
    };
  }

  get autostart() {
    return true;
  }

  async _start() {
    await super._start();

    await defineServiceConsumerProperties(
      this,
      {
        listener: {
          name: 'koa-admin',
          type: 'koa'
        }
      },
      this.owner,
      true
    );

    if (this.config.ssh !== undefined) {
      this.sshServer = sshServer(this, this.config.ssh);
    }

    return this.owner.loadFlowFromFile(
      path.join(__dirname, '..', 'admin.flow')
    );
  }

  async _stop() {
    await super._stop();

    if (this.sshServer) {
      return new Promise((fullfill, reject) => {
        this.sshServer.close(err => {
          if (err) {
            reject(err);
            return;
          }
          delete this.sshServer;
          fullfill();
        });
      });
    }
  }
}

export async function registerWithManager(manager) {
  const admin = await manager.registerServiceFactory(ServiceAdmin);
  return manager.declareService({
    type: admin.name,
    name: admin.name
  });
}

@@ -294,16 +294,46 @@
 %0A%0A/**%0A *
+ Kronos administration service
 %0A */%0Aexp
@@ -1038,29 +1038,23 @@
 tructor(
-config, owner
+...args
 ) %7B%0A    
@@ -1063,21 +1063,15 @@
 per(
-config, owner
+...args
 );%0A%0A

use varags to pass argumtnets to the super construcotr