(function () {
    'use strict';

    var module = angular.module('Aeonium.models');

    //A service for message
    module.factory('Aeonium.models.MessageList', [
        '$http', 'djangoUrl', '$rootScope',
        'Aeonium.models.Message',
        function messageListFactory($http, djangoUrl, $rootScope, Message) {

            var MessageList = function () {
                var self = this;
                self.listCache = {};
                self.listCache.time = {};
                self.listCache.last_updated = {};
                self.listCache.ambiguity = {};
            };

            //class Message {
            //	id: number;
            //	label: number;
            //	source: string;
            //	isAmbiguous: boolean;
            //	isSaved: boolean;
            //	isExample: boolean;
            //}

            //class MessageDetail extends MessageData {
            //	text: string;
            //	tokens: string[];
            //	filteredToFull: Map<number, number>;
            //	fullToFiltered: Map<number, number>;
            //	featureHighlights: FeatureHighlight[];
            //	characters: string[];
            //	charToToken: Map<number, number>;
            //
            //	// interaction
            //  hoveredCharStart: number;
            //  hoveredCharEnd: number;
            //  clickStartTokenItem: Token;
            //  selectedTokens: Token[];
            //  selectedTokenIndices: Map<number, number>;
            //}

            angular.extend(MessageList.prototype, {
                getList: function(order_by, page){
                    var self = this;
                    var apiUrl = djangoUrl.reverse('list');

                    var request = {
                        params: {
                            order_by: order_by || "time",
                            page: page || 1
                        }
                    };
                    if (self.listCache[order_by].hasOwnProperty(page)){
                        // TODO return and trigger broadcast event


                    }


                    return $http.get(apiUrl, request)
                        .success(function (data) {
                            // TODO update cache

                            $rootScope.$broadcast("list::getList", data);
                        });

                },

                // code: Code
                getCodedMessages: function(code){
                    var self = this;
                    var param = {};
                    var source = "user";
                    //if (source != "master")
                    //    param.stage = "current";

                    var apiUrl = djangoUrl.reverse('code_messages', param);


                    var request = {
                        params: {
                            code: code.codeId,
                            source: source
                        }
                    };

                    return $http.get(apiUrl, request)
                        .success(function (data) {
                            self.userCodedMessages[code.name] = data.assignments.map(function(d){ return Utils.extractMessageDetail(d);});
                            $rootScope.$broadcast("messages::userCodedMessages::" + code.name, self.userCodedMessages);
                        });

                },

                // message: Message
                getMessageDetail: function (message) {
                    var self = this;

                    var request = {
                        message_id: message.id,
                        params: {
                            source: "user"
                        }
                    };

                    var apiUrl = djangoUrl.reverse('vector', request);

                    return $http.get(apiUrl, request)
                        .success(function (data) {
                            var messageDetail = Utils.extractMessageDetail(data);
                            $rootScope.$broadcast("messages::messageDetail::" + message.id, messageDetail);
                        });

                },

                // message: Message
                submitLabel: function (message) {
                    var self = this;
                    var apiUrl = djangoUrl.reverse('assignment', {message_id: message.id});

                    var request = {
                        code: message.label,
                        is_example: message.isExample,
                        is_ambiguous: message.isAmbiguous,
                        is_saved: message.isSaved
                    };


                    return $http.post(apiUrl, request)
                        .success(function (data) {
                            $rootScope.$broadcast("messages::submitLabel::" + message.id, message);
                        });

                },
            });

            return new Message();
        }
    ]);
})();

@@ -589,32 +589,33 @@
 %0A%0A            //
+ 
 class Message %7B%0A
@@ -611,16 +611,20 @@
  Message
+List
  %7B%0A     
@@ -636,185 +636,28 @@
   //
-%09id: number;%0A            //%09label: number;%0A            //%09source: string;%0A            //%09isAmbiguous: boolean;%0A            //%09isSaved: boolean;%0A            //%09isExample: boolean
+     listCache: dict
 ;%0A  
@@ -672,659 +672,147 @@
   //
-%7D%0A%0A
      
-       //class MessageDetail extends MessageData %7B%0A            //%09text: string;%0A            //%09tokens: string%5B%5D;%0A            //%09filteredToFull: Map%3Cnumber, number%3E;%0A            //%09fullToFiltered: Map%3Cnumber, number%3E;%0A            //%09featureHighlights: FeatureHighlight%5B%5D;%0A            //%09characters: string%5B%5D;%0A            //%09charToToken: Map%3Cnumber, number%3E;%0A            //%0A            //%09// interaction%0A            //  hoveredCharStart: number;%0A            //  hoveredCharEnd: number;%0A            //  clickStartTokenItem: Token;%0A            //  selectedTokens: Token%5B%5D;%0A            //  selectedTokenIndices: Map%3Cnumber, number%3E;%0A            //%7D
+listCache.time: dict;%0A            //     listCache.last_updated: dict;%0A            //     listCache.ambiguity: dict;%0A            // %7D%0A
 %0A%0A  
@@ -1331,52 +1331,158 @@
     
-// TODO return and trigger broadcast event%0A%0A
+var data = self.listCache%5Border_by%5D%5Bpage%5D;%0A                        $rootScope.$broadcast(%22list::getList%22, data);%0A                        return false;
 %0A   
@@ -1497,25 +1497,24 @@
          %7D%0A%0A
-%0A
             
@@ -1598,32 +1598,33 @@
 nction (data) %7B%0A
+%0A
                 
@@ -1639,29 +1639,46 @@
     
-// TODO update cache%0A
+self.listCache%5Border_by%5D%5Bpage%5D = data;
 %0A   
@@ -1801,2497 +1801,8 @@
 %7D,%0A%0A
-                // code: Code%0A                getCodedMessages: function(code)%7B%0A                    var self = this;%0A                    var param = %7B%7D;%0A                    var source = %22user%22;%0A                    //if (source != %22master%22)%0A                    //    param.stage = %22current%22;%0A%0A                    var apiUrl = djangoUrl.reverse('code_messages', param);%0A%0A%0A                    var request = %7B%0A                        params: %7B%0A                            code: code.codeId,%0A                            source: source%0A                        %7D%0A                    %7D;%0A%0A                    return $http.get(apiUrl, request)%0A                        .success(function (data) %7B%0A                            self.userCodedMessages%5Bcode.name%5D = data.assignments.map(function(d)%7B return Utils.extractMessageDetail(d);%7D);%0A                            $rootScope.$broadcast(%22messages::userCodedMessages::%22 + code.name, self.userCodedMessages);%0A                        %7D);%0A%0A                %7D,%0A%0A                // message: Message%0A                getMessageDetail: function (message) %7B%0A                    var self = this;%0A%0A                    var request = %7B%0A                        message_id: message.id,%0A                        params: %7B%0A                            source: %22user%22%0A                        %7D%0A                    %7D;%0A%0A                    var apiUrl = djangoUrl.reverse('vector', request);%0A%0A                    return $http.get(apiUrl, request)%0A                        .success(function (data) %7B%0A                            var messageDetail = Utils.extractMessageDetail(data);%0A                            $rootScope.$broadcast(%22messages::messageDetail::%22 + message.id, messageDetail);%0A                        %7D);%0A%0A                %7D,%0A%0A                // message: Message%0A                submitLabel: function (message) %7B%0A                    var self = this;%0A                    var apiUrl = djangoUrl.reverse('assignment', %7Bmessage_id: message.id%7D);%0A%0A                    var request = %7B%0A                        code: message.label,%0A                        is_example: message.isExample,%0A                        is_ambiguous: message.isAmbiguous,%0A                        is_saved: message.isSaved%0A                    %7D;%0A%0A%0A                    return $http.post(apiUrl, request)%0A                        .success(function (data) %7B%0A                            $rootScope.$broadcast(%22messages::submitLabel::%22 + message.id, message);%0A                        %7D);%0A%0A                %7D,%0A
     

add to cache & broadcast data