'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _async = require('async');

var _async2 = _interopRequireDefault(_async);

var _elementInstancesLayoutsService = require('../../services/elementInstancesLayoutsService');

var _elementInstancesLayoutsService2 = _interopRequireDefault(_elementInstancesLayoutsService);

var _appUtils = require('../../utils/appUtils');

var _appUtils2 = _interopRequireDefault(_appUtils);

var _winstonLogs = require('../../utils/winstonLogs');

var _winstonLogs2 = _interopRequireDefault(_winstonLogs);

var _underscore = require('underscore');

var _underscore2 = _interopRequireDefault(_underscore);

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

/********************************************* EndPoints ******************************************************/

/***** Create Element Instance Layout EndPoint (Post: api/v2/authoring/build/element/instance/layout/create *****/
var createElementInstancesLayoutsEndPoint = function createElementInstancesLayoutsEndPoint(req, res) {
  _winstonLogs2.default.info("Endpoint hit: " + req.originalUrl);

  var params = {};

  params.bodyParams = req.body;
  _winstonLogs2.default.info("Parameters:" + JSON.stringify(params.bodyParams));

  _async2.default.waterfall([_async2.default.apply(createElementInstanceLayout, params)], function (err, result) {
    _appUtils2.default.sendResponse(res, err, 'id', params.newElementInstanceLayout.elementInstanceUuid, result);
  });
}; /*
    * *******************************************************************************
    *  * Copyright (c) 2018 Edgeworx, Inc.
    *  *
    *  * This program and the accompanying materials are made available under the
    *  * terms of the Eclipse Public License v. 2.0 which is available at
    *  * http://www.eclipse.org/legal/epl-2.0
    *  *
    *  * SPDX-License-Identifier: EPL-2.0
    *  *******************************************************************************
    *
    */

var createElementInstanceLayout = function createElementInstanceLayout(params, callback) {
  var layoutProps = {
    data: {
      isSelected: 1,
      layoutX: params.bodyParams.layoutX,
      layoutY: params.bodyParams.layoutY,
      elementInstanceUuid: params.bodyParams.elementId
    },
    setProperty: 'newElementInstanceLayout'
  };

  _elementInstancesLayoutsService2.default.createElementInstanceLayout(layoutProps, params, callback);
};

/***** Update Layout Element Instance EndPoint (Post: api/v2/authoring/element/instance/layout/update *****/
var updateElementInstancesLayoutsEndPoint = function updateElementInstancesLayoutsEndPoint(req, res) {
  try {
    _winstonLogs2.default.info("Endpoint hit: " + req.originalUrl);

    var params = {},
        elementInstanceProps = {
      elementInstance: 'bodyParams.ElementID',
      setProperty: 'elementInstanceLayout'
    };

    params.bodyParams = req.body;
    _winstonLogs2.default.info("Parameters:" + JSON.stringify(params.bodyParams));

    _async2.default.waterfall([_async2.default.apply(_elementInstancesLayoutsService2.default.findElementInstancesLayoutsByUuid, elementInstanceProps, params), updateLayout], function (err, result) {
      _appUtils2.default.sendResponse(res, err, '', '', result);
    });
  } catch (e) {
    _winstonLogs2.default.error(e);
  }
};

/***** Get Layout of Element Instance EndPoint (Post: api/v2/get/element/instances/layouts*****/
var getElementInstancesLayoutsEndPoint = function getElementInstancesLayoutsEndPoint(req, res) {
  try {
    _winstonLogs2.default.info("Endpoint hit: " + req.originalUrl);

    var params = {},
        layoutProps = {
      elementInstanceData: 'bodyParams.instance',
      field: 'id',
      setProperty: 'elementInstancesLayouts'
    };

    params.bodyParams = req.body;
    _winstonLogs2.default.info("Parameters:" + JSON.stringify(params.bodyParams));

    _async2.default.waterfall([_async2.default.apply(_elementInstancesLayoutsService2.default.findElementInstancesLayoutsByUuids, layoutProps, params), outputData], function (err, result) {
      _appUtils2.default.sendResponse(res, err, 'instance', params.response, result);
    });
  } catch (e) {
    _winstonLogs2.default.error(e);
  }
};

/***** Delete Layout of Element Instance EndPoint (Post: /api/v2/authoring/element/instance/layout/delete*****/
var deleteElementInstancesLayoutsEndPoint = function deleteElementInstancesLayoutsEndPoint(req, res) {
  _winstonLogs2.default.info("Endpoint hit: " + req.originalUrl);

  var params = {},
      layoutProps = {
    elementInstanceData: 'bodyParams.elementInstances',
    field: 'uuid'
  };

  params.bodyParams = req.body;
  _winstonLogs2.default.info("Parameters:" + JSON.stringify(params.bodyParams));

  _async2.default.waterfall([_async2.default.apply(_elementInstancesLayoutsService2.default.deleteElementInstancesLayoutsByUuids, layoutProps, params)], function (err, result) {
    var trackId = '';
    if (params.bodyParams) {
      if (params.bodyParams.elementInstances) {
        trackId = params.bodyParams.elementInstances.trackId;
      }
    }

    _appUtils2.default.sendResponse(res, err, 'trackId', trackId, result);
  });
};

/************************ Extra Functions ************************************/
var outputData = function outputData(params, callback) {
  try {
    var response = [];

    _async2.default.each(params.bodyParams.instance, function (instance, cb) {

      var elementInstancesLayouts = _underscore2.default.findWhere(params.elementInstancesLayouts, {
        elementInstanceUuid: instance.id
      });

      var elementInstance = {
        id: instance.id,
        elementInstanceName: instance.elementInstanceName,
        config: instance.config,
        fogInstanceId: instance.fogInstanceId,
        rootHostAccess: instance.rootHostAccess,
        strace: instance.strace,
        logSize: instance.logSize,
        viewerEnabled: 0,
        debugEnabled: 0,
        volumeMappings: instance.volumeMappings,
        elementName: instance.elementName,
        picture: instance.picture,
        isX86: instance.images.x86ContainerImage ? 1 : 0,
        isArm: instance.images.armContainerImage ? 1 : 0,
        connections: instance.connections,
        ports: instance.ports,
        debug: 0,
        viewer: 0,
        status: instance.status,
        layoutX: elementInstancesLayouts ? elementInstancesLayouts.layoutX : 0,
        layoutY: elementInstancesLayouts ? elementInstancesLayouts.layoutY : 0,
        isSelected: elementInstancesLayouts ? elementInstancesLayouts.isSelected : 0
      };
      response.push(elementInstance);
      cb();
    }, function (err) {
      params.response = response;
      callback(null, params);
    });
  } catch (e) {
    _winstonLogs2.default.error(e);
  }
};

var updateLayout = function updateLayout(params, callback) {
  if (params.elementInstanceLayout.length) {
    var layoutProps = {
      updatedData: {
        isSelected: params.bodyParams.Selected,
        layoutX: params.bodyParams.LayoutX,
        layoutY: params.bodyParams.LayoutY
      },
      elementInstanceId: 'bodyParams.ElementID'
    };
    _elementInstancesLayoutsService2.default.updateLayout(layoutProps, params, callback);
  } else if (!params.elementInstanceLayout.length && params.bodyParams.ElementID) {
    var layoutProps = {
      data: {
        isSelected: params.bodyParams.Selected,
        layoutX: params.bodyParams.LayoutX,
        layoutY: params.bodyParams.LayoutY,
        elementInstanceUuid: params.bodyParams.ElementID
      },
      setProperty: 'newElementInstanceLayout'
    };
    _elementInstancesLayoutsService2.default.createElementInstanceLayout(layoutProps, params, callback);
  } else {
    callback(null, params);
  }
};

exports.default = {
  createElementInstancesLayoutsEndPoint: createElementInstancesLayoutsEndPoint,
  getElementInstancesLayoutsEndPoint: getElementInstancesLayoutsEndPoint,
  updateElementInstancesLayoutsEndPoint: updateElementInstancesLayoutsEndPoint,
  deleteElementInstancesLayoutsEndPoint: deleteElementInstancesLayoutsEndPoint
};