/**
 * This file is part of tenside/ui.
 *
 * (c) Christian Schiffler <c.schiffler@cyberspectrum.de>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * This project is provided in good faith and hope to be usable by anyone.
 *
 * @package    tenside/ui
 * @author     Christian Schiffler <c.schiffler@cyberspectrum.de>
 * @copyright  2015 Christian Schiffler <c.schiffler@cyberspectrum.de>
 * @license    https://github.com/tenside/ui/blob/master/LICENSE MIT
 * @link       https://github.com/tenside/ui
 * @filesource
 */

(function () {
    "use strict";
    angular
        .module('tenside-search', ['tenside-api', 'tenside-package-list-entry'])
        .config(
            [
                '$stateProvider',
                function ($stateProvider) {
                    $stateProvider.state(
                        'search',
                        {
                            url: '/search?keywords&type',
                            templateUrl: 'pages/search.html',
                            controller: 'tensideSearchController'
                        }
                    );
                }
            ]
        )
        .directive(
            'searchHeader',
            [
                '$state', '$stateParams',
                function ($state, $stateParams) {
                    return {
                        restrict: 'E',
                        scope: true,
                        templateUrl: 'pages/search-head.html',
                        link: function (scope, element, attrs) {
                            scope.keywords = $stateParams.keywords ? $stateParams.keywords : '';
                            scope.search = function () {
                                if (scope.keywords != '') {
                                    var params = { keywords: scope.keywords};
                                    if ($stateParams.type) {
                                        params.type = $stateParams.type;
                                    }
                                    $state.go('search', params, {reload: true});
                                }
                            }
                        }
                    }
                }
            ]
        )
        .controller(
            'tensideSearchController',
            [
                '$scope', '$tensideApi', '$stateParams',
                function ($scope, $tensideApi, $stateParams) {

                    $tensideApi.search.search($stateParams.keywords).success(function (data) {
                        $scope.packages = data;
                    }).error(function() {

                    });

                    $scope.packages = {};
                }
            ]
        );

    // Late dependency injection
    TENSIDE.requires.push('tenside-search');
})();

@@ -2424,32 +2424,42 @@
  '$stateParams',
+ '$state',
 %0A               
@@ -2502,19 +2502,150 @@
 teParams
-) %7B
+, $state) %7B%0A                    $scope.packages = %7B%7D;%0A                    $scope.type = $stateParams.type ? $stateParams.type : 'all';
 %0A%0A      
@@ -2705,17 +2705,55 @@
 keywords
-)
+, $scope.type)%0A                        
 .success
@@ -2767,24 +2767,28 @@
 on (data) %7B%0A
+    
             
@@ -2847,96 +2847,613 @@
     
-%7D).error(function() %7B%0A%0A                    %7D);%0A%0A                    $scope.packages = %7B%7D
+    %7D)%0A                        .error(function() %7B%0A                            // FIXME: handle search error here.%0A                        %7D);%0A%0A                    $scope.$watch('type', function (newValue, oldValue) %7B%0A                        if (oldValue != newValue) %7B%0A                            var params = %7B keywords: $stateParams.keywords %7D;%0A                            if ($scope.type) %7B%0A                                params.type = $scope.type;%0A                            %7D%0A                            $state.go('search', params, %7Breload: true%7D);%0A                        %7D%0A                    %7D)
 ;%0A  

Handle search type