/*!
 * Copyright 2012 Sakai Foundation (SF) 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://www.osedu.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.
 */

/*
 * Dependencies
 *
 * /dev/lib/misc/trimpath.template.js (TrimpathTemplates)
 * /dev/lib/jquery/plugins/jquery.threedots.js (ThreeDots)
 */

/*global $ */

require(['jquery', 'sakai/sakai.api.core'], function($, sakai) {


    /**
     * @name sakai_global.relatedcontent
     *
     * @class relatedcontent
     *
     * @description
     * Initialize the relatedcontent widget
     *
     * @version 0.0.1
     * @param {String} tuid Unique id of the widget
     * @param {Boolean} showSettings Show the settings of the widget or not
     */
    sakai_global.relatedcontent = function(tuid,showSettings) {

        ///////////////////
        // CSS Selectors //
        ///////////////////

        var relatedcontent = '#relatedcontent';
        var relatedcontentContainer = relatedcontent + '_container';
        var relatedcontentDefaultTemplate = relatedcontent + '_default_template';
        var relatedcontentContent = '.relatedcontent_content';
        var relatedcontentFooter = '#relatedcontent_footer';
        var relatedcontentShowMore = '#relatedcontent_show_more';

        var contentData = {};
        var page = 0;
        var numberofitems = 5;


        //////////////////////
        // Render functions //
        //////////////////////

        /**
         * Render the template
         */
        var renderTemplate = function(relatedcontentData) {
            // Render the relatedcontent
            relatedcontentData.sakai = sakai;
            if (relatedcontentData.hasOwnProperty('relatedContent') && relatedcontentData.relatedContent.hasOwnProperty('results')) {
                for (var item in relatedcontentData.relatedContent.results) {
                    if (relatedcontentData.relatedContent.results.hasOwnProperty(item)) {
                        relatedcontentData.relatedContent.results[item]['sakai:pooled-content-file-name-dotted'] = sakai.api.Util.applyThreeDots(relatedcontentData.relatedContent.results[item]['sakai:pooled-content-file-name'], $('.relatedcontent').width() - 30, {max_rows: 1,whole_word: false}, 'oae-bold');
                    }
                }
                sakai.api.Util.TemplateRenderer(relatedcontentDefaultTemplate, relatedcontentData, $(relatedcontentContainer));
                $(relatedcontentContainer).show();
            }
        };


        ////////////////////
        // Util functions //
        ////////////////////

        /**
         * Fetches the related content
         */
        var getRelatedContent = function() {
            var managersList = '';
            var viewersList = '';
            var ajaxSuccess = function(data) {
                var itemsDisplayed = data.items * (page + 1);
                var moreResults = itemsDisplayed < data.total;
                $.each(data.results, function(index, item) {
                    data.results[index].commentcount = sakai.api.Content.getCommentCount(item);
                    var mimeType = sakai.api.Content.getMimeType(data.results[index]);
                    var mimeTypeDescription = sakai.api.i18n.getValueForKey(sakai.config.MimeTypes['other'].description);
                    if (sakai.config.MimeTypes[mimeType]) {
                        mimeTypeDescription = sakai.api.i18n.getValueForKey(sakai.config.MimeTypes[mimeType].description);
                    }
                    data.results[index].mimeTypeDescription = mimeTypeDescription;
                });
                var json = {
                    'content': contentData,
                    'relatedContent': data
                };
                renderTemplate(json);
                if (!moreResults) {
                    $(relatedcontentShowMore).hide();
                    $('#relatedcontent_footer').addClass('relatedcontent_footer_norelated');
                } else {
                    $(relatedcontentShowMore).show();
                    $('#relatedcontent_footer').removeClass('relatedcontent_footer_norelated');
                }
            };
            var ajaxError = function() {
                renderTemplate({});
            };

            for (var i = 0; i < contentData.members.managers.length; i++) {
                if (contentData.members.managers[i]) {
                    managersList += ' ' + (contentData.members.managers[i]['rep:userId'] || contentData.members.managers[i]['sakai:group-id']);
                }
            }
            for (var j = 0; j < contentData.members.viewers.length; j++) {
                if (contentData.members.viewers[j]) {
                    viewersList += ' ' + (contentData.members.viewers[j]['rep:userId'] || contentData.members.viewers[j]['sakai:group-id']);
                }
            }
            var searchterm = contentData.data['sakai:pooled-content-file-name'].substring(0,400) + ' ' + managersList + ' ' + viewersList;
            if (contentData.data['sakai:tags'] && contentData.data['sakai:tags'].length) {
                searchterm = searchterm + ' ' + contentData.data['sakai:tags'].join(' ');
            }
            var searchquery = sakai.api.Server.createSearchString(searchterm, false, 'OR');

            // get related content for contentData
            // return some search results for now
            var params = {
                'items': numberofitems,
                'page': page
            };
            var url = sakai.config.URL.SEARCH_ALL_FILES.replace('.json', '.0.json');
            if (searchquery === '*' || searchquery === '**') {
                url = sakai.config.URL.SEARCH_ALL_FILES_ALL;
            } else {
                params['q'] = searchquery;
            }
            $.ajax({
                url: url,
                data: params,
                success: ajaxSuccess,
                error: ajaxError
            });

        };

        var showMore = function() {
            $(relatedcontentShowMore).attr('disabled','disabled');
            page++;
            getRelatedContent();
        };

        //////////////
        // Bindings //
        //////////////

        /**
         * Bind the widget's links
         */
        var addBinding = function() {
            // bind the more link
            $(relatedcontentShowMore).off('click', showMore);
            $(relatedcontentShowMore).on('click', showMore);
        };

        ////////////////////
        // Initialization //
        ////////////////////

        /**
         * Render function
         */
       $(window).on('render.relatedcontent.sakai', function(e, data) {
           page = 0;
           addBinding();
           contentData = data;
           getRelatedContent();
        });

        $(relatedcontentContent).on('click', function() {
            $.bbq.pushState($(this).attr('data-href'));
        });

        // Indicate that the widget has finished loading
        $(window).trigger('ready.relatedcontent.sakai', {});
        sakai_global.relatedcontent.isReady = true;
    };

    sakai.api.Widgets.widgetLoader.informOnLoad('relatedcontent');
});
