'use strict';

(function () {
  var Checksum;

  if (typeof module === 'undefined') {
    Checksum = Peeracle.Checksum;
  } else {
    Checksum = require('./checksum');
  }

  function Metadata() {
    var _checksum;
    var _checksumId = 'crc32';
    var _chunksize = 0;
    var _trackers = [];
    var _initSegment = [];
    var _mediaSegments = {};

    var getChecksum = function () {
      return _checksumId;
    };

    var getChunkSize = function () {
      return _chunksize;
    };

    var getTrackers = function () {
      return _trackers;
    };

    var getInitSegment = function () {
      return _initSegment;
    };

    var getMediaSegments = function () {
      return _mediaSegments;
    };

    var setChecksum = function (checksum) {
      _checksumId = checksum;
    };

    var setTrackers = function (trackers) {
      _trackers = trackers;
    };

    var setInitSegment = function (initSegment) {
      _initSegment = initSegment;
    };

    var _loadChecksum = function () {
      for (var c in Checksum) {
        if (Checksum[c].getIdentifier() === _checksumId) {
          _checksum = Checksum[c].create();
          break;
        }
      }

      if (!_checksum) {
        throw 'Unknown checksum ' + _checksumId;
      }
    };

    var addMediaSegment = function (timecode, mediaSegment, progressCallback) {
      var clusterLength = mediaSegment.length;
      var chunkLength = _chunksize;

      if (!_checksum) {
        _loadChecksum();
      }

      _mediaSegments[timecode] = [clusterLength, _checksum.checksum(mediaSegment), []];

      for (var i = 0; i < clusterLength; i += chunkLength) {
        var chunk = mediaSegment.subarray(i, i + chunkLength);

        _mediaSegments[timecode][2].push(_checksum.checksum(chunk));

        if (progressCallback) {
          progressCallback(chunk);
        }

        if (clusterLength - i < chunkLength) {
          chunkLength = clusterLength - i;
        }
      }

      if (progressCallback) {
        progressCallback(null);
      }
    };

    var validateMediaSegment = function (timecode, mediaSegment) {
      if (!_checksum) {
        _loadChecksum();
      }

      return _mediaSegments[timecode][1] === _checksum.checksum(mediaSegment);
    };

    var calculateChunkSize = function (fileLength) {
      var i;
      var targetLength = 40 * 1024;
      var chunkSize = fileLength / (targetLength / 20);

      for (i = 16 * 1024; i < 1024 * 1024; i *= 2) {
        if (chunkSize > i) {
          continue;
        }
        break;
      }
      _chunksize = i;
    };

    return {
      getChecksum: getChecksum,
      getChunkSize: getChunkSize,
      getTrackers: getTrackers,
      getInitSegment: getInitSegment,
      getMediaSegments: getMediaSegments,

      setChecksum: setChecksum,
      setTrackers: setTrackers,
      setInitSegment: setInitSegment,
      addMediaSegment: addMediaSegment,

      validateMediaSegment: validateMediaSegment,
      calculateChunkSize: calculateChunkSize
    };
  }

  module.exports = Metadata;
})();

@@ -187,24 +187,37 @@
 etadata() %7B%0A
+    var _id;%0A
     var _che
@@ -355,24 +355,445 @@
 ents = %7B%7D;%0A%0A
+    var getId = function () %7B%0A      if (!_id) %7B%0A        _checksum.init();%0A        _checksum.update(_initSegment);%0A        for (var m in _mediaSegments) %7B%0A          _checksum.update(_mediaSegments%5Bm%5D%5B1%5D);%0A          for (var c = 0, l = _mediaSegments%5Bm%5D%5B2%5D.length; c %3C l; ++c) %7B%0A            _checksum.update(_mediaSegments%5Bm%5D%5B2%5D%5Bc%5D);%0A          %7D%0A        %7D%0A        _id = _checksum.final();%0A      %7D%0A      return _id;%0A    %7D;%0A%0A
     var getC
@@ -3012,16 +3012,36 @@
 eturn %7B%0A
+      getId: getId,%0A
       ge

Add getId method inside Metadata module, returns the unique metadata's ID