/**
 * Renders a log feed.
 *
 */
'use strict';
var ko = require('knockout');
var $ = require('jquery');
var moment = require('moment');
require('knockout-punches');
var $osf = require('osfHelpers');
var Paginator = require('js/paginator');
var oop = require('js/oop');

ko.punches.enableAll();  // Enable knockout punches
/**
  * Log model.
  */
var Log = function(params) {
    var self = this;

    $.extend(self, params);
    self.date = new $osf.FormattableDate(params.date);
    self.wikiUrl = ko.computed(function() {
        return self.nodeUrl + 'wiki/' + encodeURIComponent(self.params.page);
    });
    self.wikiIdUrl = ko.computed(function() {
        return self.nodeUrl + 'wiki/id/' + encodeURIComponent(self.params.page_id);
    });

    /**
      * Given an item in self.contributors, return its anchor element representation.
      */
    self._asContribLink = function(person) {
        return '<a class="contrib-link" href="/profile/' + person.id + '/">' + person.fullname + '</a>';
    };

    /**
      * Return whether a knockout template exists for the log.
      */
    self.hasTemplate = ko.computed(function() {
        return $('script#' + self.action).length > 0;
    });


    /**
      * Return the html for a comma-delimited list of contributor links, formatted
      * with correct list grammar.
      * e.g. "Dasher and Dancer", "Comet, Cupid, and Blitzen"
      */
    self.displayContributors = ko.computed(function(){
        var ret = '';
        if (self.anonymous){
            ret += '<span class="contributor-anonymous">some anonymous contributor(s)</span>';
        } else {
            for (var i = 0; i < self.contributors.length; i++) {
                var person = self.contributors[i];
                if (i === self.contributors.length - 1 && self.contributors.length > 2) {
                    ret += ' and ';
                }
                if (person.registered) {
                    ret += self._asContribLink(person);
                } else {
                    ret += '<span>' + person.fullname + '</span>';
                }
                if (i < self.contributors.length - 1 && self.contributors.length > 2) {
                    ret += ', ';
                } else if (i < self.contributors.length - 1 && self.contributors.length === 2) {
                    ret += ' and ';
                }
            }
        }
        return ret;
    });
};

/**
  * View model for a log list.
  * @param {Log[]} logs An array of Log model objects to render.
  * @param url the url ajax request post to
  */
var LogsViewModel = oop.extend(Paginator, {
    constructor: function(logs, url) {
        this.super.constructor();
        var self = this;
        self.logs = ko.observableArray(logs);
        self.url = url;

        self.tzname = ko.computed(function() {
            var logs = self.logs();
            if (logs.length) {
                var tz =  moment(logs[0].date).format('ZZ');
                return tz;
            }
            return '';
        });
    },
    //send request to get more logs when the more button is clicked
    fetchResult: function(){
        var self = this;
        self.logs.removeAll();
        return $.ajax({
            type: 'get',
            url: self.url,
            data:{
                page: self.currentPage()
            },
            cache: false
        }).done(function(response) {
            // Initialize LogViewModel
            self.logs.removeAll();
            var logModelObjects = createLogs(response.logs); // Array of Log model objects
            console.log("logNum" + logModelObjects.length);
            for (var i=0; i<logModelObjects.length; i++) {
                self.logs.push(logModelObjects[i]);
            }
            self.currentPage(response.page);
            self.numberOfPages(response.pages);
            console.log("currentPage" + self.currentPage());
            console.log("numerOfPages" + self.numberOfPages());
            console.log("-------");
            self.addNewPaginators();
        }).fail(
            $osf.handleJSONError
        );

    }
});


/**
  * Create an Array of Log model objects from data returned from an endpoint
  * @param  {Object[]} logData Log data returned from an endpoint.
  * @return {Log[]}         Array of Log objects.
  */
var createLogs = function(logData){
    var mappedLogs = $.map(logData, function(item) {
        return new Log({
            'anonymous': item.anonymous,
            'action': item.action,
            'date': item.date,
            // The node type, either 'project' or 'component'
            // NOTE: This is NOT the component category (e.g. 'hypothesis')
            'nodeType': item.node.is_registration ? 'registration': item.node.node_type,
            'nodeCategory': item.node.category,
            'contributors': item.contributors,
            'nodeUrl': item.node.url,
            'userFullName': item.user.fullname,
            'userURL': item.user.url,
            'apiKey': item.api_key,
            'params': item.params,
            'nodeTitle': item.node.title,
            'nodeDescription': item.params.description_new
        });
    });
    return mappedLogs;
};

////////////////
// Public API //
////////////////

var defaults = {
    /** Selector for the progress bar. */
    progBar: '#logProgressBar'
};


var initViewModel = function(self, logs, url){
    self.logs = createLogs(logs);
    self.viewModel = new LogsViewModel(self.logs, url);
    self.viewModel.fetchResult();
    self.init();
};

/**
  * A log list feed.
  * @param {string} selector
  * @param {string} url
  * @param {object} options
  */
function LogFeed(selector, data, options) {
    var self = this;
    self.selector = selector;
    self.$element = $(selector);
    self.options = $.extend({}, defaults, options);
    self.$progBar = $(self.options.progBar);
    if (Array.isArray(data)) { // data is an array of log object from server
        initViewModel(self, data, self.options.url);
    } else { // data is an URL
        $.getJSON(data, function(response) {
            initViewModel(self, response.logs, data);
        });
    }
}

LogFeed.prototype.init = function() {
    var self = this;
    self.$progBar.hide();
    ko.cleanNode(self.$element[0]);
    $osf.applyBindings(self.viewModel, self.selector);
};

module.exports = LogFeed;

@@ -3435,32 +3435,34 @@
 del%0A            
+//
 self.logs.remove
@@ -5478,24 +5478,42 @@
 logs, url);%0A
+    if(url) %7B%0A    
     self.vie
@@ -5530,24 +5530,30 @@
 chResult();%0A
+    %7D%0A
     self.ini
@@ -5893,24 +5893,52 @@
 s.progBar);%0A
+    //for recent activities%0A
     if (Arra
@@ -6085,25 +6085,123 @@
 s an URL
-%0A        
+, for watch log and project log%0A        var empty =%5B%5D;%0A        initViewModel(self, empty, data);%0A        //
 $.getJSO
@@ -6233,24 +6233,26 @@
 ) %7B%0A        
+//
     initView
@@ -6285,32 +6285,34 @@
  data);%0A        
+//
 %7D);%0A    %7D%0A%7D%0A%0ALog

fix the recent activity