"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _appAuthMiddleware = _interopRequireDefault(require("../hooks/appAuthMiddleware"));

var _authMiddleware = _interopRequireDefault(require("../hooks/authMiddleware"));

var _AppService = _interopRequireDefault(require("../services/AppService"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * 
 * @param {import("fastify").FastifyInstance} fastifyInstance 
 * @param {import("fastify").FastifyPluginOptions} options 
 */
const appRouter = async (fastifyInstance, options) => {
  const appService = _AppService.default.getInstance();

  fastifyInstance.get('/', {
    preHandler: (0, _authMiddleware.default)([])
  }, async (request, reply) => {
    try {
      let appList = (await appService.getAll()) || [];
      return reply.status(200).send({
        message: 'OK',
        data: appList
      });
    } catch (error) {
      return reply.status(500).send({
        message: 'FAIL',
        data: error.message
      });
    }
  });
  fastifyInstance.post('/', {
    preHandler: (0, _authMiddleware.default)([])
  }, async (request, reply) => {
    const {
      name = null,
      active = false,
      secured = false,
      fleets = [],
      security = []
    } = request.body || {};

    try {
      let app = await appService.create(name, active, secured, fleets, security);
      reply.status(200).send({
        message: 'OK',
        data: app
      });
    } catch (error) {
      reply.status(422).send({
        message: 'FAIL',
        cause: error.message
      });
    }
  });
  fastifyInstance.get('/:appId', {
    preHandler: (0, _authMiddleware.default)([])
  }, async (request, reply) => {
    const {
      appId
    } = request.params || {};

    try {
      let app = await appService.getOne(appId);
      reply.status(200).send({
        message: 'OK',
        data: app
      });
    } catch (error) {
      reply.status(422).send({
        message: 'FAIL',
        cause: error.message
      });
    }
  });
  fastifyInstance.put('/:appId', {
    preHandler: (0, _authMiddleware.default)([])
  }, async (request, reply) => {
    const {
      appId
    } = request.params || {};
    const {
      name = null,
      active = false,
      secured = false,
      fleets = [],
      security = []
    } = request.body || {};

    try {
      let app = await appService.update(appId, name, active, secured, fleets, security);
      reply.status(200).send({
        message: 'OK',
        data: app
      });
    } catch (error) {
      reply.status(422).send({
        message: 'FAIL',
        cause: error.message
      });
    }
  });
  fastifyInstance.delete('/:appId', {
    preHandler: (0, _authMiddleware.default)([])
  }, async (request, reply) => {
    const {
      appId
    } = request.params || {};

    try {
      await appService.delete(appId);
      reply.status(200).send({
        message: 'OK'
      });
    } catch (error) {
      reply.status(422).send({
        message: 'FAIL',
        cause: error.message
      });
    }
  });
  fastifyInstance.get('/:appId/rooms', {
    preHandler: (0, _appAuthMiddleware.default)()
  }, (request, reply) => {
    return reply.status(500).send({
      message: 'not_implemented'
    });
  });
  fastifyInstance.post('/:appId/rooms', {
    preHandler: (0, _appAuthMiddleware.default)()
  }, (request, reply) => {
    return reply.status(500).send({
      message: 'not_implemented'
    });
  });
  fastifyInstance.post('/:appId/rooms/:roomId/join', {
    preHandler: (0, _appAuthMiddleware.default)()
  }, (request, reply) => {
    return reply.status(500).send({
      message: 'not_implemented'
    });
  });
  fastifyInstance.delete('/:appId/rooms/:roomId', {
    preHandler: (0, _appAuthMiddleware.default)()
  }, (request, reply) => {
    return reply.status(500).send({
      message: 'not_implemented'
    });
  });
};

var _default = appRouter;
exports.default = _default;