'use strict';

exports.__esModule = true;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

// CameraRecorder
var CameraRecorder = {
  recorder: null, // 录制器
  recorderFile: null, // 录制完成文件
  stream: null, // 视频流
  timer: null, // 计时
  timerEl: null, // 计时元素
  onStop: null, // 当前停止录制回调
  onStopInit: null, // 初始化时停止录制回调, 当调用stopRecord传入回调时, 此回调将被覆盖
  // 关闭媒体流
  closeStream: function closeStream(stream) {
    if (typeof stream.stop === 'function') {
      stream.stop();
    } else {
      var trackList = [stream.getAudioTracks(), stream.getVideoTracks()];
      for (var i = 0; i < trackList.length; i++) {
        var tracks = trackList[i];
        if (tracks && tracks.length > 0) {
          for (var j = 0; j < tracks.length; j++) {
            var track = tracks[j];
            if (typeof track.stop === 'function') {
              track.stop();
            }
          }
        }
      }
    }
  },
  /**
    * 打开摄像头, 并监听录制结束事件
    * @param {Object} params
    * {
    *   {Object} constraints: 限制条件
    *   {DOM} video: video元素
    *   {DOM} timer: 计时元素
    *   {Function} onOpened: func(e, stream)已打开摄像头回调
    *   {Function} onStop: func(e)结束录制回调
    *   {Function} onError: func(e, {errMsg: ''})错误回调
    * }
    * @param {String} type 'wgs84 | gcj02'
    * @return {Object} 
    */
  init: function init() {
    var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

    var self = this;
    var _params$constraints = params.constraints,
        constraints = _params$constraints === undefined ? {} : _params$constraints,
        video = params.video,
        timer = params.timer,
        onOpened = params.onOpened,
        onStop = params.onStop,
        onError = params.onError;

    self.onStopInit = onStop;
    self.onStop = onStop;
    self.timerEl = timer;
    if (!video) {
      if (onError) onError({ target: video }, { errMsg: 'No params "video"' });
      return;
    }
    if (navigator.mediaDevices === undefined) {
      if (onError) onError({ target: video }, { errMsg: 'mediaDevices not supported' });
      return;
    }
    navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
    if (!navigator.getUserMedia) {
      if (onError) onError({ target: video }, { errMsg: 'getUserMedia not supported' });
      return;
    }
    var width = window.innerWidth || window.screen.width || window.screen.availWidth;
    var height = window.innerHeight || window.screen.height || window.screen.availHeight;
    video.width = width;
    video.height = height;
    navigator.getUserMedia(_extends({
      audio: constraints.audio || true,
      video: constraints.video || {
        width: width,
        height: height
      }
    }, constraints), function (stream) {
      // 流放入video
      video.srcObject = stream;
      video.onloadedmetadata = function (e) {
        if (onOpened) onOpened(e, { stream: stream });
      };
      // 录制器
      self.stream = stream;
      self.recorder = new MediaRecorder(stream);
      var chunks = []; // 数据块, 用于构建recorderFile
      // 录制器: 数据可用
      self.recorder.ondataavailable = function (e) {
        self.recorder.blobs.push(e.data);
        chunks.push(e.data);
      };
      // 录制器: 结束录制
      self.recorder.blobs = [];
      self.recorder.onstop = function (e) {
        self.recorderFile = new Blob(chunks, {
          'type': self.recorder.mimeType
        });
        chunks = [];
        if (self.onStop) self.onStop(e, self.getFile());
      };
    }, function (err) {
      if (onError) onError({ target: video }, { errMsg: 'The following error occurred: ' + err.name });
    });
  },
  startTimer: function startTimer(timerEl) {
    if (!timerEl) return;
    var t = 0;
    this.timer = setInterval(function () {
      t += 1000;
      // 将毫秒数转成时分
      var hours = parseInt(t / 3600000);
      var minutes = parseInt(t % 3600000 / 60000);
      var seconds = t % 60000 / 1000;
      var timeStr = '';
      // 显示时
      if (hours > 1 || minutes > 59) {
        timeStr = (hours < 10 ? '0' + hours : hours) + ':';
      }
      // 显示分
      if (minutes > 0) {
        timeStr += (minutes < 10 ? '0' + minutes : minutes) + ':';
      } else {
        timeStr += '00:';
      }
      // 显示秒
      if (seconds > 0) {
        timeStr += seconds < 10 ? '0' + seconds : seconds;
      } else {
        timeStr += '00';
      }
      timerEl.innerHTML = timeStr;
    }, 100);
  },
  // 开始录制
  startRecord: function startRecord() {
    var self = this;
    self.recorder.start();
    self.startTimer(self.timerEl);
  },
  // 结束录制
  stopRecord: function stopRecord(callback) {
    var self = this;
    if (callback) {
      self.onStop = callback;
    } else if (callback === false || callback === null) {
      self.onStop = null;
    } else {
      self.onStop = self.onStopInit;
    }
    // 终止计时
    if (self.timer) window.clearInterval(self.timer);
    if (self.recorder && self.recorder.state === 'recording') {
      // 终止录制器
      self.recorder.stop();
    }
    // 关闭媒体流
    self.closeStream(self.stream);
  },
  // 生成file用于提交表单
  getFile: function getFile() {
    var self = this;
    var file = new File([self.recorderFile], 'msr-' + new Date().toISOString().replace(/:|\./g, '-') + '.mp4', {
      type: 'video/mp4'
    });
    return file;
  }
};

exports.default = CameraRecorder;
module.exports = exports['default'];