'use strict';

/* Controllers */

var builderControllers = angular.module('builderControllers', []);

builderControllers.controller('DebBuildListCtrl', ['$scope', '$http',
    function ($scope, $http) {
        $http.get('/api/build_confs/').success(function (data) {
            if ($scope.build_confs != data) {
                $scope.build_confs = data;
            }
        });

        $scope.orderProp = '-fields.status';
        $scope.alerts = [];

        $scope.closeAlert = function (index) {
            $scope.alerts.splice(index, 1);
        };

        $scope.rebuild_pkg = function (build_conf_id) {
            $http.get('/api/rebuild_package/' + build_conf_id).success(function (data) {
                $scope.alerts.push(data);
            });
        }

        $scope.autobuild_on_off = function (build_conf_id) {
            $http.get('/api/autobuild_on_off/' + build_conf_id).success(function (data) {
                for (var build_conf in $scope.build_confs) {
                    if ($scope.build_confs[build_conf].pk == build_conf_id) {
                        $scope.build_confs[build_conf].fields.auto_build = data[0].fields.auto_build;
                    }
                }
            });
        }

        $scope.remove_build_conf = function (build_conf_id) {
            var is_confirm = confirm('Are you sure remove build config #' + build_conf_id + '?');
            if (is_confirm) {
                $http.get('/api/remove_build_conf/' + build_conf_id).success(function (data) {
                    $scope.alerts.push(data);
                });
            }
        }

        $scope.getData = function () {
            $http.get('/api/build_confs/').success(function (data) {
                if ($scope.build_confs != data) {
                    $scope.build_confs = data;
                }
            });
        };
        setInterval($scope.getData, 5000);
    }]);


builderControllers.controller('BranchestListCtrl', ['$scope', '$http',
    function ($scope, $http) {
        $http.get('/api/branches/').success(function (data) {
            if ($scope.branches != data) {
                $scope.branches = data;
            }
        });

        $scope.alerts = [];

        $scope.closeAlert = function (index) {
            $scope.alerts.splice(index, 1);
        };

        $scope.remove_branch = function (branch_id) {
            var is_confirm = confirm('Are you sure remove branch #' + branch_id + '?');
            if (is_confirm) {
                $http.get('/api/remove_branch/' + branch_id).success(function (data) {
                    $scope.alerts.push(data);
                });
            }
        }

        $scope.getData = function () {
            $http.get('/api/branches/').success(function (data) {
                if ($scope.branches != data) {
                    $scope.branches = data;
                }
            });
        };
        setInterval($scope.getData, 5000);
    }]);

builderControllers.controller('MappingCtrl', ['$scope', '$http',
    function ($scope, $http) {
        $http.get('/api/mapping/').success(function (data) {
            if ($scope.mappings != data) {
                $scope.mappings = data;
            }
        });
        $scope.alerts = [];

        $scope.closeAlert = function (index) {
            $scope.alerts.splice(index, 1);
        };

        $scope.remove_mapping = function (mapping_id) {
            var is_confirm = confirm('Are you sure remove mapping #' + mapping_id + '?');
            if (is_confirm) {
                $http.get('/api/remove_mapping/' + mapping_id).success(function (data) {
                    $scope.alerts.push(data);
                });
            }
        }

        $scope.getData = function () {
            $http.get('/api/mapping/').success(function (data) {
                if ($scope.mappings != data) {
                    $scope.mappings = data;
                }
            });
        };
        setInterval($scope.getData, 5000);
    }]);

@@ -1409,32 +1409,285 @@
  (is_confirm) %7B%0A
+                for (var build_conf in $scope.build_confs) %7B%0A                    if ($scope.build_confs%5Bbuild_conf%5D.pk == build_conf_id) %7B%0A                        $scope.build_confs%5Bbuild_conf%5D.fields.status = 3;%0A                    %7D%0A                %7D%0A
                 
@@ -1757,32 +1757,32 @@
 nction (data) %7B%0A
-
                 
@@ -1968,62 +1968,8 @@
 ) %7B%0A
-                if ($scope.build_confs != data) %7B%0A    
     
@@ -2007,34 +2007,16 @@
 = data;%0A
-                %7D%0A
         
@@ -2953,59 +2953,8 @@
 ) %7B%0A
-                if ($scope.branches != data) %7B%0A    
     
@@ -2989,34 +2989,16 @@
 = data;%0A
-                %7D%0A
         
@@ -3931,59 +3931,8 @@
 ) %7B%0A
-                if ($scope.mappings != data) %7B%0A    
     
@@ -3967,34 +3967,16 @@
 = data;%0A
-                %7D%0A
         

add pending status in step from send task to task in progress