// -*- coding: utf-8 -*-
//
// (c) 2014 Bjoern Ricks <bjoern.ricks@gmail.com>
//
// See LICENSE comming with the source of 'trex' for details.
//
'use strict';

var trexControllers = angular.module('trex.controllers', []);

trexControllers.controller('ProjectListCtrl', ['$scope', 'Project',
    function($scope, Project) {
        $scope.projects = Project.query();

        $scope.order = "name";
        $scope.orderreverse = false;

        $scope.setOrder = function(name) {
            if (name == $scope.order) {
                $scope.orderreverse = !$scope.orderreverse;
            }
            $scope.order = name;
        };
    }
]);

trexControllers.controller('ProjectDetailCtrl',
    ['$scope', '$routeParams', 'Project',
    function($scope, $routeParams, Project) {
        $scope.project = Project.get({projectId: $routeParams.id});
        $scope.entries = Project.entries({projectId: $routeParams.id});

        $scope.entries_tags = "";

        $scope.order = "id";
        $scope.orderreverse = false;

        $scope.$watch('project.$resolved', function(value) {
            $scope.project_loading = !value;
        });

        $scope.$watch('entries.$resolved', function(value) {
            $scope.entries_loading = !value;
        });

        $scope.setOrder = function(name) {
            if (name == $scope.order) {
                $scope.orderreverse = !$scope.orderreverse;
            }
            $scope.order = name;
        };

        $scope.searchEntries = function() {
            $scope.entries = Project.entries(
                    {
                        projectId: $routeParams.id,
                        from_date: $scope.entries_from_date,
                        to_date: $scope.entries_to_date,
                        state: $scope.entries_state,
                        tag: $scope.entries_tags
                    }
                );
            $scope.entries_loading = true;
        };

        $scope.addSearchTag = function(tag) {
            if ($scope.entries_tags.indexOf(tag) == -1) {
                if ($scope.entries_tags.length == 0) {
                  $scope.entries_tags += tag;
                }
                else {
                  $scope.entries_tags += "," + tag;
                }
            }
        };
    }
]);

trexControllers.controller('EntryDetailCtrl',
    ['$scope', '$routeParams', 'Entry',
    function($scope, $routeParams, Entry) {
        $scope.entry = Entry.get({entryId: $routeParams.id});

        $scope.order = "id";
        $scope.orderreverse = false;

        $scope.setOrder = function(name) {
            if (name == $scope.order) {
                $scope.orderreverse = !$scope.orderreverse;
            }
            $scope.order = name;
        };
    }
]);

@@ -953,10 +953,10 @@
 s = 
-%22%22
+%5B%5D
 ;%0A%0A 
@@ -1453,32 +1453,260 @@
 me;%0A        %7D;%0A%0A
+        $scope.tagList = function(tags) %7B%0A            var search = %22%22;%0A            angular.forEach(tags, function(value, key) %7B%0A                search += value.name + ',';%0A%0A            %7D);%0A            return search;%0A        %7D;%0A%0A
         $scope.s
@@ -2053,16 +2053,31 @@
    tag: 
+$scope.tagList(
 $scope.e
@@ -2087,16 +2087,17 @@
 ies_tags
+)
 %0A       
@@ -2235,36 +2235,51 @@
 ) %7B%0A            
-if (
+for(var i = 0; i %3C 
 $scope.entries_t
@@ -2286,26 +2286,19 @@
 ags.
-indexOf(tag) == -1
+length; i++
 ) %7B%0A
@@ -2313,20 +2313,28 @@
         
-if (
+var value = 
 $scope.e
@@ -2348,20 +2348,50 @@
 tags
-.length
+%5Bi%5D;%0A                if (value.name
  == 
-0
+tag
 ) %7B%0A
@@ -2400,32 +2400,87 @@
                 
+ 
+ return;%0A                %7D%0A            %7D;%0A            
 $scope.entries_t
@@ -2486,15 +2486,28 @@
 tags
- +=
+.push(%7B'name':
  tag
+%7D)
 ;%0A  
@@ -2508,32 +2508,36 @@
 %7D);%0A        
+%7D;%0A%0A
         
 %7D%0A          
@@ -2528,103 +2528,203 @@
     
-%7D%0A                else %7B%0A                  $scope.entries_tags += %22,%22 + tag;%0A                %7D%0A
+$scope.completeTag = function(query) %7B%0A            return Project.tags(%0A                    %7B%0A                        projectId: $routeParams.id,%0A                        name_like: query%0A        
     
@@ -2724,32 +2724,43 @@
                %7D
+).$promise;
 %0A        %7D;%0A    
@@ -2747,32 +2747,33 @@
 ise;%0A        %7D;%0A
+%0A
     %7D%0A%5D);%0A%0AtrexC

Update entries tags usage for tag-input