<commit_before>'use strict';

angular.module('streamium.provider.controller', ['ngRoute'])

.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.when('/provider', {
      templateUrl: 'provider/create.html',
      controller: 'CreateStreamCtrl'
    });

    $routeProvider.when('/provider/:streamId', {
      templateUrl: 'provider/stream.html',
      controller: 'BroadcastStreamCtrl'
    });

    $routeProvider.when('/provider/:streamId/cashout', {
      templateUrl: 'provider/cashout.html',
      controller: 'CashoutStreamCtrl'
    });
  }
])

.controller('CreateStreamCtrl', function($scope, $location, Rates, StreamiumProvider, bitcore) {
  $scope.stream = {};

  $scope.stream.name = config.DEBUG ? config.defaults.providerStream : '';
  $scope.stream.address = config.DEBUG ? config.defaults.providerAddress : '';
  $scope.stream.rate = 60;

  $scope.stream.error = null;
  $scope.stream.loading = false;

  $scope.otherNetwork = config.otherNetwork;
  $scope.linkToOther = config.linkToOther;

  if (!DetectRTC.isWebRTCSupported) {
    return $location.path('/no-webrtc');
  }

  $scope.normalizeName = function() {
    var name = $scope.stream.name || '';
    name = name.trim().toLowerCase().replace(/ |\\/g, '-');
    $scope.stream.name = name;
  };

  $scope.usdHourToBtcSec = function(usdHour) {
    var usdSecond = usdHour / 3600;
    var btcSecond = bitcore.Unit.fromFiat(usdSecond, Rates.rate).toBTC();
    return btcSecond ? btcSecond : 0;
  }

  $scope.usdHourToBtcMin = function(usdHour) {
    var usdSecond = usdHour / 60;
    var btcSecond = bitcore.Unit.fromFiat(usdSecond, Rates.rate).toBTC();
    return btcSecond ? btcSecond : 0;
  }

  $scope.submit = function() {
    if (!$scope.form.$valid) return;
    $scope.stream.loading = true;
    StreamiumProvider.init(
      $scope.stream.name,
      $scope.stream.address,
      $scope.usdHourToBtcMin($scope.stream.rate),
      function onCreate(err, done) {
        $scope.stream.loading = false;
        if (err) {
          $scope.stream.error = "Channel name is taken, please pick a different one";
        } else {
          $location.path('/provider/' + $scope.stream.name);
        }

        $scope.$apply();
      });
  };
})

.controller('BroadcastStreamCtrl', function($scope, $location, $routeParams, video, StreamiumProvider) {
  var name = $location.$$url.split('/')[2];<commit_msg>Returns form error if price rate is a negative number<commit_after>'use strict';

angular.module('streamium.provider.controller', ['ngRoute'])

.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.when('/provider', {
      templateUrl: 'provider/create.html',
      controller: 'CreateStreamCtrl'
    });

    $routeProvider.when('/provider/:streamId', {
      templateUrl: 'provider/stream.html',
      controller: 'BroadcastStreamCtrl'
    });

    $routeProvider.when('/provider/:streamId/cashout', {
      templateUrl: 'provider/cashout.html',
      controller: 'CashoutStreamCtrl'
    });
  }
])

.controller('CreateStreamCtrl', function($scope, $location, Rates, StreamiumProvider, bitcore) {
  $scope.stream = {};

  $scope.stream.name = config.DEBUG ? config.defaults.providerStream : '';
  $scope.stream.address = config.DEBUG ? config.defaults.providerAddress : '';
  $scope.stream.rate = 60;

  $scope.stream.error = null;
  $scope.stream.loading = false;

  $scope.otherNetwork = config.otherNetwork;
  $scope.linkToOther = config.linkToOther;

  if (!DetectRTC.isWebRTCSupported) {
    return $location.path('/no-webrtc');
  }

  $scope.normalizeName = function() {
    var name = $scope.stream.name || '';
    name = name.trim().toLowerCase().replace(/ |\\/g, '-');
    $scope.stream.name = name;
  };

  $scope.usdHourToBtcSec = function(usdHour) {
    var usdSecond = usdHour / 3600;
    var btcSecond = bitcore.Unit.fromFiat(usdSecond, Rates.rate).toBTC();
    return btcSecond ? btcSecond : 0;
  }

  $scope.usdHourToBtcMin = function(usdHour) {
    var usdSecond = usdHour / 60;
    var btcSecond = bitcore.Unit.fromFiat(usdSecond, Rates.rate).toBTC();
    return btcSecond ? btcSecond : 0;
  }

  $scope.submit = function() {
    if (!$scope.form.$valid) return;

    var priceRate = $scope.usdHourToBtcMin($scope.stream.rate);

    if (priceRate < 0) {
      $scope.stream.error = "Price rate must be a positive number";
      return;
    }

    $scope.stream.loading = true;
    StreamiumProvider.init(
      $scope.stream.name,
      $scope.stream.address,
      priceRate,
      function onCreate(err, done) {
        $scope.stream.loading = false;
        if (err) {
          $scope.stream.error = "Channel name is taken, please pick a different one";
        } else {
          $location.path('/provider/' + $scope.stream.name);
        }

        $scope.$apply();
      });
  };
})

.controller('BroadcastStreamCtrl', function($scope, $location, $routeParams, video, StreamiumProvider) {
  var name = $location.$$url.split('/')[2];
{'fraction_non_alphanumeric': 0.15819095477386935, 'fraction_numerical': 0.004623115577889447, 'mean_word_length': 3.293356341673857, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5364562', 'n_tokens_mistral': 1652, 'n_tokens_neox': 1595, 'n_words': 277}