'use strict';
//dependent upon on Bootstrap

app.directive('goBack', function () {
  return {
    restrict: 'E',
    replace: true,
    scope: {
      state: '@',
      stateParams: '@'
    },
    template: '<button class="btn btn-primary" ng-click="goBack()"><i class="fa fa-caret-left"></i> Back</button>',
    controller: function($rootScope, $scope, $state) {
      $scope.goBack = function () {
        //if no state is provided the directive will check the rootScope for the previous state
        if (!$scope.state) {
          $state.go($rootScope.previousState.name, $rootScope.previousState.params);
        } else {
          $state.go($scope.state, $scope.stateParams);
        }
      };

    }
  };
});

'use strict';

app.directive('userPanel', function() {
  return {
    restrict: 'EA',
    replace: true,
    scope: {},
                    //'<a class="btn btn-nav pull-right" ui-sref="login" ng-if="!currentUser"><span>Login</span></a>'+
    template: '<div class="white pull-right" ng-if="currentUser">'+
                '<i class="fa fa-user fa-lg"></i>&nbsp;&nbsp; {{currentUser.username}} (<a class="link-unstyled link-default" ng-click="logoutUser()">Logout</a>)'+
               '</div>',
    controller: function($scope, $state, $window, Auth, User) {

      $scope.currentUser = null;

      $scope.logoutUser = function () {
        User.logout();
        $window.location.href = '/installer';
      };

      $scope.$on('user:set', function(event, currentUser) {
        console.log('caught user:set event');
        $scope.currentUser = currentUser;
      });

      $scope.$on('user:unset', function(event) {
        console.log('caught user:unset event');
        $scope.currentUser = null;
      });



    }
  };
});

'use strict';

app.directive('fileInfoForm', function (){
  return {
    restrict: 'EA',
    require: 'ngModel',
    replace: true,
    scope: {
      ngModel: '=',
      formName: '='
    },
    templateUrl: 'views/installer/new/file-info.html',
    controller: function ($scope) {
      

    } 
  };
});

'use strict';

app.directive('requestOverview', function (Database){
  return {
    restrict: 'EA',
    require: 'ngModel',
    replace: true,
    scope: {
      rooms: '=ngModel',
      modelTypes: '='
    },
    templateUrl: 'views/installer/new/overview.html',
    controller: function ($scope) {
      
      $scope.checkName = function(data) {
        if (!data.length) {
          return 'This field is required.';
        } else if (data.length > 32) {
          return 'This field cannot be longer than 32 characters.';
        } else {
          
        }
      };

      $scope.isEntDevice = Database.checkIfEntertainment;

    } 
  };
});
'use strict';

app.directive('volPunchThrough', function (){
  return {
    restrict: 'EA',
    require: 'ngModel',
    replace: true,
    scope: {
      rooms: '=ngModel'
    },
    templateUrl: 'views/installer/new/volume/vol-options.html',
    controller: function ($scope) {
      

    } 
  };
});
'use strict';

app.directive('selectActivityCmds', function (){
  return {
    restrict: 'EA',
    require: 'ngModel',
    replace: true,
    scope: {
      rooms: '=ngModel'
    },
    templateUrl: 'views/installer/new/activities/select-cmds.html',
    controller: function ($scope) {
      

    } 
  };
});
'use strict';

app.directive('selectActivityDevices', function (){
  return {
    restrict: 'EA',
    require: 'ngModel',
    replace: true,
    scope: {
      rooms: '=ngModel'
    },
    templateUrl: 'views/installer/new/activities/select-devices.html',
    controller: function ($scope) {
      

    } 
  };
});

'use strict';

app.directive('customDeviceForm', function (){
  return {
    restrict: 'EA',
    require: 'ngModel',
    replace: true,
    scope: {
      device: '=ngModel',
      formName: '='
    },
    templateUrl: 'views/installer/new/devices/write-in.html',
    controller: function ($scope) { 

      $scope.$watch('device.db.source', function(newValue, oldValue) {
        
        if (newValue === 'CUSTOM') {
          $scope.device.target.type = 1;
          $scope.device.db.brand = null;
          $scope.device.db.model = {}; 
        }
      });

      

    } 
  };
});

'use strict';

app.directive('dbDeviceForm', function (){
  return {
    restrict: 'EA',
    require: 'ngModel',
    replace: true,
    scope: {
      device: '=ngModel',
      brands: '=',
      formName: '=',
      categories: '='
    },
    templateUrl: 'views/installer/new/devices/db.html',
    controller: function ($scope) { 


      $scope.$watch('[device.id, device.db.type, device.db.source]', function(newValue, oldValue) {
        console.log('clearing query & models');
        $scope.device.db.brand = null;
        $scope.query = null;
        $scope.models = null;
        //$scope.formName.$setPristine();

      }, true);

      $scope.$watch('device.db.type', function(newValue, oldValue) {
        $scope.device.name = _.where($scope.categories, {
          name: $scope.device.db.type
        })[0].device;
      }, true);

    } 
  };
});

'use strict';

app.directive('deviceInfoForm', function (){
  return {
    restrict: 'EA',
    require: 'ngModel',
    replace: true,
    scope: {
      device: '=ngModel',
      rooms: '=',
      selectedRoom: '=',
      deviceTypes: '=',
      formName: '='
    },
    templateUrl: 'views/installer/new/devices/info.html',
    controller: function ($scope) {

      $scope.selectedRoom = $scope.rooms[0];
    }
  };
});

'use strict';

app.directive('deviceSettingsForm', function (){
  return {
    restrict: 'EA',
    require: 'ngModel',
    replace: true,
    scope: {
      device: '=ngModel',
      info: '=',
      room: '=',
      formName: '='
    },
    templateUrl: 'views/installer/new/devices/settings.html',
    controller: function ($scope) {


      $scope.$watch('info', function(newValue, oldValue) {
        var deviceInfo = $scope.info;
        console.log(deviceInfo);
        if (deviceInfo) {
          if ($scope.device.db.source === 'DB') {
            $scope.device.target.port = deviceInfo.codeset === 'ip'? parseInt(deviceInfo.port, 10) : 255;
          } else {
            $scope.device.target.port = $scope.device.target.type === 'IP' ? null:255;
          }
        } else {
          $scope.device.target.port = $scope.device.target.type === 'IP' ? null:255;
        }
      });


      $scope.$watch('device.target', function(newValue, oldValue) {
        $scope.device.target.addr = '0';
      });

      $scope.$watch('device.target.type', function(newValue, oldValue) {
        if (newValue == 1) {
          $scope.device.target.addr = '0';
        } else {
          delete $scope.device.target.addr;
        }
      });
                                      
      
      // $scope.device.target.addr = $scope.device.target.addr || $scope.selection.room.bstations[0].id;

      $scope.$watch('device.db.source', function(newValue, oldValue) {
        $scope.info = null;
      });

     
    }
  };
});

'use strict';

app.directive('favChannelsForm', function (){
  return {
    restrict: 'EA',
    require: 'ngModel',
    replace: true,
    scope: {
      device: '=ngModel',
      formName: '='
    },
    templateUrl: 'views/installer/new/devices/fav-channels.html',
    controller: function ($scope) {

      $scope.$watch('device.db.type', function(newValue, oldValue) {
        if (newValue !== 'Televisions' && newValue !== 'Satellite Set Top Boxes' && newValue !== 'Cable and Fios Set Top Boxes' && newValue !== 'DVR') {
          $scope.device.attr.fav = 0;
        }
      });
    }
  };
});

'use strict';

app.directive('addRoomsDetail', function (){
  return {
    restrict: 'EA',
    replace: true,
    scope: {
      room: '=',
      rooms: '=',
      availableModels: '='
    },
    templateUrl: 'views/installer/new/rooms/rooms.detail.html',
    controller: function ($scope, Rooms) {
      //$scope.room = room;
      console.log($scope.room);      $scope.createNewBstation = function() {
        return Rooms.generateBstation();
      };

      $scope.$watch('room', function(newValue, oldValue) {
        if (!newValue && $scope.rooms.indexOf(newValue) > -1) {
          $scope.room = newValue;
        }
      });
    }
  };
});

'use strict';

app.directive('addRooms', function (){
  return {
    restrict: 'EA',
    require: 'ngModel',
    replace: true,
    scope: {
      ngModel: '=',
      modelTypes: '='
    },
    templateUrl: 'views/installer/new/rooms/rooms.html',
    controller: function ($scope, Rooms) {
      $scope.selected = {};
      $scope.selected.room = $scope.ngModel.rooms[0];

      $scope.createNewRoom = function() {
        return Rooms.generateOne();
      };

      $scope.$watchCollection('ngModel.rooms', function(newValue, oldValue) {
        if (newValue.length != oldValue.length) {
          $scope.selected.room = $scope.ngModel.rooms[$scope.ngModel.rooms.length - 1];
        }
      });
    } 
  };
});
