/*!
 * Copyright 2014 Apereo Foundation (AF) Licensed under the
 * Educational Community License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 *     http://opensource.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

var OAE = require('oae-util/lib/oae');
var OaeServer = require('oae-util/lib/server');
var log = require('oae-logger').logger('mediacore-rest');

var MediaCoreAPI = require('./api');

// Ensure that the MediaCore encoding callback endpoing bypasses CSRF validation. It has its own authenticity handling.
OaeServer.addSafePathPrefix('/api/mediacore/encodingCallback');

/**
 * @REST getMediacoreEmbedContentId
 *
 * Get the MediaCore embed url from a content id
 *
 * @Server      tenant
 * @Method      GET
 * @Path        /mediacore/embed/{contentId}
 * @PathParam   {string}                contentId           The contentId of the content to play in the MediaCore player
 * @Return      {MediacoreEmbedInfo}                        Returns object with MediaCore information
 */
OAE.tenantRouter.on('get', '/api/mediacore/embed/:contentId', function(req, res) {
    MediaCoreAPI.getEmbedCode(req.ctx, req.params.contentId, function(err, embedCode) {
        if (err) {
            return res.send(err.code, err.msg);
        }

        return res.send(200, embedCode);
    });
});

/**
 * @REST postMediacoreEncodingCallback
 *
 * Update the thumbnails from MediaCore
 *
 * @Server      tenant
 * @Method      POST
 * @Path        /mediacore/encodingCallback
 * @BodyParam   {MediacoreId}   body             Thumbnail to update
 */
OAE.tenantRouter.on('post', '/api/mediacore/encodingCallback', function(req, res) {
    MediaCoreAPI.updateThumbnails(req.body.media_id, function(err) {
        if (err) {
            return res.send(err.code, err.msg);
        }

        return res.send(200);
    });
});

/*!
 * Copyright 2014 Apereo Foundation (AF) Licensed under the
 * Educational Community License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 *     http://opensource.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

var OAE = require('oae-util/lib/oae');
var OaeServer = require('oae-util/lib/server');
var log = require('oae-logger').logger('mediacore-rest');

var MediaCoreAPI = require('./api');

// Ensure that the MediaCore encoding callback endpoing bypasses CSRF validation. It has its own authenticity handling.
OaeServer.addSafePathPrefix('/api/mediacore/encodingCallback');

/**
 * @REST getMediacoreEmbedContentId
 *
 * Get the MediaCore embed url from a content id
 *
 * @Server      tenant
 * @Method      GET
 * @Path        /mediacore/embed/{contentId}
 * @PathParam   {string}                contentId           The contentId of the content to play in the MediaCore player
 * @Return      {MediacoreEmbedInfo}                        Returns object with MediaCore information
 */
OAE.tenantRouter.on('get', '/api/mediacore/embed/:contentId', function(req, res) {
    MediaCoreAPI.getEmbedCode(req.ctx, req.params.contentId, function(err, embedCode) {
        if (err) {
            return res.send(err.code, err.msg);
        }

        return res.send(200, embedCode);
    });
});

/**
 * @REST postMediacoreEncodingCallback
 *
 * Update the thumbnails from MediaCore
 *
 * @Server      tenant
 * @Method      POST
 * @Path        /mediacore/encodingCallback
 * @BodyParam   {MediacoreId}   body             The MediaCore item id for which to update the thumbnail
 */
OAE.tenantRouter.on('post', '/api/mediacore/encodingCallback', function(req, res) {
    MediaCoreAPI.updateThumbnails(req.body.media_id, function(err) {
        if (err) {
            return res.send(err.code, err.msg);
        }

        return res.send(200);
    });
});

revise description