/* istanbul ignore next */
if (FEAT.TCF_DEMO) {
  angular.module('prx.analyze-audio', ['angular-id3', 'angular-aurora'])
  .factory('MimeDefinition', function() {

    function MimeDefinition (type) {
      this.type = type;
    }

    MimeDefinition.prototype = {
      major: function () { return this.type.split('/')[0]; },
      minor: function () { return this.type.split('/')[1]; },
      full:  function () { return this.type; }
    };

    return MimeDefinition;
  })
  .service('MimeType', function MimeTypeService(MimeDefinition) {

    var expectedMimeTypes = {
      "aif": "audio\/x-aiff",
      "aifc": "audio\/x-aiff",
      "aiff": "audio\/x-aiff",
      "caf": "audio\/x-caf",
      "flac": "audio\/x-flac",
      "m2a": "audio\/mpeg",
      "m3a": "audio\/mpeg",
      "m4a": "audio\/mp4",
      "mp2": "audio\/mpeg",
      "mp2a": "audio\/mpeg",
      "mp3": "audio\/mpeg",
      "mp4": "video\/mp4",
      "mp4a": "audio\/mp4",
      "mpga": "audio\/mpeg",
      "oga": "audio\/ogg",
      "ogg": "audio\/ogg",
      "spx": "audio\/ogg",
      "wav": "audio\/x-wav",
      "weba": "audio\/webm",
      "gif": "image\/gif",
      "jpe": "image\/jpeg",
      "jpeg": "image\/jpeg",
      "jpg": "image\/jpeg",
      "png": "image\/png",
      "svg": "image\/svg+xml",
      "svgz": "image\/svg+xml",
      "webp": "image\/webp"
    };

    this.lookup = function(file, defaultType) {
      defaultType = defaultType || "application\/octet-stream";

      var type = file.type;
      if (typeof type === 'undefined' || type === null || type === '') {
        var ext = file.name.split('.').pop();
        type = expectedMimeTypes[ext];
      }
      return new MimeDefinition(type || defaultType);
    };

  })
  .service('AnalyzeAudio', function AnalyzeAudio($q, MimeType, Id3Service, AuroraService) {

    this.analyze = function (file) {

      // try to figure out the mime type
      file.mimeType = MimeType.lookup(file);

      var analysis = [];

      // metadata may be redundant with tags, getting it anyway
      // analysis.push( AuroraService.metadata(file).then( function (m) { file.metadata = m; return m; }) );
      analysis.push( AuroraService.format(file).then(   function (f) { file.format   = f; return f; }) );
      analysis.push( AuroraService.duration(file).then( function (d) { file.duration = d; return d; }) );
      analysis.push( Id3Service.analyze(file).then(     function (t) { file.tags     = t; return t; }) );

      // return just the file back in the collected promise
      return $q.all(analysis).then(function (data) { return file; });
    };
  })
  .service('ValidateAudio', function (AnalyzeAudio) {

    var ValidateAudio = this;

    function ValidationResults() {
    }

    ValidationResults.prototype = {
      addMessage: function (validation, options) {
        this[validation] = options;
      },
      error: function(validation, options) {
        options = options || {};
        options.severity = 'error';
        return this.addMessage(validation, options);
      },
      warning: function(context, validation, options) {
        options = options || {};
        options.severity = 'warning';
        return this.addMessage(validation, options);
      }
    };

    // notAudio:      error:   This is not an audio file or media file containing audio
    // videoFile:     warning: the file is video, will use audio track
    // lossyEncoding: warning: uses a lossy encoding, not so good for transcoding
    ValidateAudio.validateType = function(file) {
      file._results = file._results || new ValidationResults();

      var mt = file.mimeType.full();

      var t = file.mimeType.major();
      if (t == 'video') {
        file._results.warning('videoFile', {mimeType: mt});
      } else if (t == 'audio') {
        if (!file.mimeType.minor().match(/(wav|flac|aiff|alac|raw|pcm)/)) {
          file._results.warning('lossyEncoding', {mimeType: mt});
        }
      } else {
        file._results.error('notAudio', {mimeType: mt});
      }
      return ValidateAudio;
    };

    // need to figure out what warnings we want per type, and per channel count
    // for an mp2, bitrate should be 256 for stereo, and 128 for mono
    ValidateAudio.validateBitRate = function(file) {
      file._results = file._results || new ValidationResults();
      if (file.mimeType.major() != 'audio') { return ValidateAudio; }
      return ValidateAudio;
    };

    // need to figure out what warnings we want per type
    // for an mp2, sample rate should by 44100
    ValidateAudio.validateSampleRate = function(file) {
      file._results = file._results || new ValidationResults();
      if (file.mimeType.major() != 'audio') { return ValidateAudio; }
      return ValidateAudio;
    };

    ValidateAudio.validate = function (file) {
      return AnalyzeAudio.analyze(file).then( function (file) {

        file._results = new ValidationResults();
        ValidateAudio.validateType(file);
        ValidateAudio.validateBitRate(file);
        ValidateAudio.validateSampleRate(file);

        return file;
      });

    };

  });
}
@@ -2038,26 +2038,8 @@
     
- // analysis.push(
  Aur
@@ -2112,27 +2112,26 @@
 return m; %7D)
- )
 ;
+%0A
 %0A      analy
@@ -2954,39 +2954,32 @@
 error';%0A        
-return 
 this.addMessage(
@@ -3037,17 +3037,8 @@
 ion(
-context, 
 vali
@@ -3135,23 +3135,16 @@
         
-return 
 this.add
@@ -3928,21 +3928,23 @@
 results.
-error
+warning
 ('notAud
@@ -4338,32 +4338,259 @@
 alidateAudio; %7D%0A
+%0A      // mp2 validation%0A      if (file.format.format == 'mp2') %7B%0A        if (file.format.sampleRate != 44100) %7B%0A          file._results.warning('broadcastSamplerate', %7BsampleRate: file.format.sampleRate%7D );%0A        %7D%0A      %7D%0A%0A
       return Val
@@ -4902,16 +4902,350 @@
 Audio; %7D
+%0A%0A      // mp2 validation%0A      if (file.format.format == 'mp2') %7B%0A        var channelRate = (file.format.bitrate / file.format.channelsPerFrame);%0A        if (channelRate %3C 128) %7B%0A          file._results.error('broadcastLowBitrate', %7Bbitrate: file.format.bitrate, channelsPerFrame: file.format.channelsPerFrame%7D );%0A        %7D%0A      %7D%0A%0A
 %0A      r

fix warning method, add actual validations