'use strict';

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

var _async = require('async');

var _async2 = _interopRequireDefault(_async);

var _fogLayoutsService = require('../../services/fogLayoutsService');

var _fogLayoutsService2 = _interopRequireDefault(_fogLayoutsService);

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 }; }

/***** Update Fog Layout EndPoint (Post: api/v2/authoring/fog/instances/layout/update *****/
var updateFogLayoutsEndPoint = function updateFogLayoutsEndPoint(req, res) {
  _winstonLogs2.default.info("Endpoint hit: " + req.originalUrl);

  var params = {};

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

  var fogProps = {
    instanceId: 'bodyParams.instanceId',
    setProperty: 'fogLayoutsData'
  };

  _async2.default.waterfall([_async2.default.apply(_fogLayoutsService2.default.findFogLayoutsByInstanceId, fogProps, params), updateFogLayout], function (err, result) {
    _appUtils2.default.sendResponse(res, err, '', '', 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 updateFogLayout = function updateFogLayout(params, callback) {
  if (params.fogLayoutsData.length) {
    var updateLayoutProps = {
      updatedData: {
        isSelected: params.bodyParams.selected,
        layoutX: params.bodyParams.layoutX,
        layoutY: params.bodyParams.layoutY
      },
      instanceId: 'bodyParams.instanceId'
    };

    _fogLayoutsService2.default.updateFogLayout(updateLayoutProps, params, callback);
  } else {
    if (params.bodyParams.layoutX) {
      var createLayoutProps = {
        data: {
          instanceId: params.bodyParams.instanceId,
          isSelected: params.bodyParams.selected,
          layoutX: params.bodyParams.layoutX,
          layoutY: params.bodyParams.layoutY
        },
        setProperty: 'newFogLayout'
      };
      _fogLayoutsService2.default.createFogLayout(createLayoutProps, params, callback);
    } else {
      callback(null, params);
    }
  }
};

/***** Get Layout of Fog EndPoint (Post: /api/v2/authoring/fog/instances/list *****/
var getFogLayoutsEndPoint = function getFogLayoutsEndPoint(req, res) {
  _winstonLogs2.default.info("Endpoint hit: " + req.originalUrl);

  var params = {},
      layoutProps = {
    fogData: 'bodyParams.uuids',
    setProperty: 'fogLayouts'
  };

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

  _async2.default.waterfall([_async2.default.apply(_fogLayoutsService2.default.findFogLayoutsByInstanceIds, layoutProps, params)], function (err, result) {
    _appUtils2.default.sendResponse(res, err, 'layoutData', params.fogLayouts, result);
  });
};

/***** Delete Layouts of Fog EndPoint (Post: /api/v2/authoring/fog/instance/delete *****/
var deleteFogLayoutsEndPoint = function deleteFogLayoutsEndPoint(req, res) {
  _winstonLogs2.default.info("Endpoint hit: " + req.originalUrl);

  var params = {},
      layoutProps = {
    instanceId: 'bodyParams.instanceId'
  };

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

  _async2.default.waterfall([_async2.default.apply(_fogLayoutsService2.default.deleteFogLayoutsByInstanceId, layoutProps, params)], function (err, result) {
    _appUtils2.default.sendResponse(res, err, 'instanceId', params.bodyParams.instanceId, result);
  });
};

exports.default = {
  updateFogLayoutsEndPoint: updateFogLayoutsEndPoint,
  deleteFogLayoutsEndPoint: deleteFogLayoutsEndPoint,
  getFogLayoutsEndPoint: getFogLayoutsEndPoint
};