angular.module('ippon').controller('PlayersController',[
  '$scope',
  '$stateParams',
  '$location',
  '$resource',
  '$state',
  'Auth',
function($scope, $stateParams, $location, $resource, $state, Auth){
  var controllerRoot = "/players/";
  var player = $resource(controllerRoot + ':playerId',
    {
      playerId: "@id",
      format: "json"
    }, {
      'save' : {
        method: 'PUT'
      },
      'create': {
        method: 'POST'
      }
    }
  );

  var club = $resource('/clubs/:clubId',
    {
      clubId: "@id",
      format: "json"
    }
  );

    if($state.is('players_show') || $state.is('players_edit')) {
      if(!$stateParams.playerId) {
        $state.go('players');
      } else {
        player.get({
          playerId: $stateParams.playerId
        }, function(player) {
          $scope.player = player;
          club.get({clubId: player.club_id}, function(club) {
            $scope.player.club = club;
          });
        }, function(httpResponse) {
          $scope.player = null;
          //flash.error = 'There is no club with Id + $routeParams.clubId'
        });
      }
    } else {
      if($state.is('players')){
        player.query(function(results) {
          return $scope.players = results;
        });
      }
      $scope.player = {};
    }

    if($state.is('players_edit') || $state.is('players_new')) {
      club.query(function(results) {
        return $scope.clubs = results;
      });
    }

    $scope.index = function() {
      $state.go('players');
    }


  $scope.index = function() {
    $state.go('players');
  }

  $scope.view = function(playerId) {
    $state.go("players_show",{playerId: playerId})
  }

  $scope.viewPlayersClub = function(clubId) {
    $state.go('clubs_show',{clubId: $scope.player.club.id});
  }

  $scope.save = function() {
    var onError = function(_httpResponse) {
      //TODO flash.error
    }

    if($scope.player.id) {
      $scope.player.$save((function() {
        $state.go('players_show',{playerId: $scope.player.id});
      }), onError)
    } else {
      player.create($scope.player, (function(newPlayer) {
        $state.go('players_show',{playerId: newPlayer.id});
      }), onError);
    }
  };

  $scope["delete"] = function() {
    $scope.player.$delete();
    $scope.index();
  }

  $scope.mapRanks = function(rank) {
    switch(rank) {
      case "dan_8": return '8 DAN';
      case "dan_7": return '7 DAN';
      case "dan_6": return '6 DAN';
      case "dan_5": return '5 DAN';
      case "dan_4": return '4 DAN';
      case "dan_3": return '3 DAN';
      case "dan_2": return '2 DAN';
      case "dan_1": return '1 DAN';
      case "kyu_1": return '1 KYU';
      case "kyu_2": return '2 KYU';
      case "kyu_3": return '3 KYU';
      case "kyu_4": return '4 KYU';
      case "kyu_5": return '5 KYU';
      case "kyu_6": return '6 KYU';
      default: return rank;
    }
  }
}]);

@@ -560,18 +560,16 @@
 %7D%0A  );%0A%0A
-  
   if($st
@@ -627,18 +627,16 @@
 ) %7B%0A    
-  
 if(!$sta
@@ -652,26 +652,24 @@
 playerId) %7B%0A
-  
       $state
@@ -688,18 +688,16 @@
 ');%0A    
-  
 %7D else %7B
@@ -693,26 +693,24 @@
    %7D else %7B%0A
-  
       player
@@ -716,18 +716,16 @@
 r.get(%7B%0A
-  
         
@@ -758,26 +758,24 @@
 yerId%0A      
-  
 %7D, function(
@@ -780,26 +780,24 @@
 n(player) %7B%0A
-  
         $sco
@@ -816,18 +816,16 @@
 player;%0A
-  
         
@@ -878,34 +878,32 @@
 ub) %7B%0A          
-  
 $scope.player.cl
@@ -917,32 +917,28 @@
 ub;%0A        
-  
 %7D);%0A
-  
       %7D, fun
@@ -959,34 +959,32 @@
 onse) %7B%0A        
-  
 $scope.player = 
@@ -989,18 +989,16 @@
 = null;%0A
-  
         
@@ -1068,32 +1068,26 @@
 '%0A      
-  
 %7D);%0A
-  
     %7D%0A
-  
   %7D else
@@ -1085,26 +1085,24 @@
 %0A  %7D else %7B%0A
-  
     if($stat
@@ -1121,26 +1121,24 @@
 s'))%7B%0A      
-  
 player.query
@@ -1154,26 +1154,24 @@
 (results) %7B%0A
-  
         retu
@@ -1203,31 +1203,91 @@
 ts;%0A    
+  %7D);%0A
     %7D
-);
 %0A    
-  %7D
+if($scope.player == null %7C%7C $scope.player.clubId == null) %7B
 %0A      $
@@ -1307,27 +1307,29 @@
 = %7B%7D;%0A    %7D%0A
-%0A
   
+%7D%0A%0A
   if($state.
@@ -1378,26 +1378,24 @@
 ew')) %7B%0A    
- 
- 
 club.query(f
@@ -1405,34 +1405,32 @@
 tion(results) %7B%0A
-  
       return $sc
@@ -1458,87 +1458,15 @@
     
-  
 %7D);%0A  
- 
- %7D%0A%0A    $scope.index = function() %7B%0A      $state.go('players');%0A    %7D%0A
+%7D
 %0A%0A  

Fix indentation in PlayersController