var angular = require('angular');
require('../modules');
require('../services/fileService');
require('./$toast');

var module = angular.module('commons.directives.footerinput');

module.directive('footerInput', function() {
  return {
    restrict: 'AE',
    replace: true,
    templateUrl: 'views/commons/directives/footerinput.tpl.html',
    scope: {
      footerInputOptions: '='
    },
    controller: 'FooterInputCtrl'
  };
});

module.controller('FooterInputCtrl', function($scope, $timeout, $rootScope,
  $ionicActionSheet, AudioFileService, $toast) {
  $scope.state = 'text';
  $scope.footer = {
    recordReady: false,
    recording: false
  };
  $scope.confirmText = $scope.footerInputOptions && $scope.footerInputOptions.confirmText || '发送';
  $scope.placeHolder = $scope.footerInputOptions && $scope.footerInputOptions.placeHolder || '';

  $scope.$watch(function() {
    return $scope.footer.recordReady ?
      'record-ready' : $scope.footer.recording ?
      'recording' : $scope.text ?
      'texting' : 'text';
  }, function(state) {
    $scope.state = state;
  });

  /**
   * 成功录音后的调用函数。
   */
  function onCaptureAudioSuccess(media) {
    $scope.footer.recording = false;
    $scope.footer.recordReady = true;
    if ($scope.footerInputOptions && $scope.footerInputOptions.onCaptureAudioSuccess) {
      $scope.footerInputOptions.onCaptureAudioSuccess(media);
    }
  }
  /**
   * 录音失败后的调用函数。
   */
  function onCaptureAudioFail(error) {
    $scope.footer.recording = false;
    $scope.footer.recordReady = true;
    if ($scope.footerInputOptions && $scope.footerInputOptions.onCaptureAudioFail) {
      $scope.footerInputOptions.onCaptureAudioFail(error);
    }
  }

  /**
   * 录语音。
   */
  $scope.captureAudio = function() {
    $scope.footer.recordReady = true;
    $scope.footer.recording = false;
  };
  /**
   * 开始录音。
   */
  $scope.startRecord = function() {
    $scope.footer.recordReady = false;
    $scope.footer.recording = true;
    $rootScope.$broadcast('audio.release');
    var promise;
    promise = $scope.recordMediaPromise = AudioFileService.record();
    promise.then(function(media) {
      media.promise.then(onCaptureAudioSuccess, onCaptureAudioFail);
    }, onCaptureAudioFail);
  };

  $scope.onTouch = function() {
    $scope.touchBeginTime = new Date();
  };
  /**
   * 结束录音。
   */
  $scope.stopRecord = function() {
    $scope.touchEndTime = new Date();
    if(!$scope.touchBeginTime || $scope.touchEndTime - $scope.touchBeginTime < 1000) {
      $toast.showShortCenter('说话时间太短！');
    } else {
      $scope.footer.recordReady = true;
      $scope.footer.recording = false;
      $scope.recordMediaPromise.stopRecord();
    }
  };
  /**
   * 选取照片。
   */
  $scope.getPicture = function() {
    $ionicActionSheet.show({
      title: '选取照片',
      buttons: [{
        text: '拍照'
      }, {
        text: '从相册中选取'
      }],
      cancelText: '取消',
      cancel: function() {
        return true;
      },
      buttonClicked: function(index) {
        if (index === 0) {
          $scope.takePicture();
        } else if (index === 1) {
          $scope.choosePicture();
        }
        return true;
      }
    });
  };

  /**
   * 拍照。
   */
  $scope.takePicture = function() {
    if ($scope.footerInputOptions && $scope.footerInputOptions.onGetPictureSuccess) {
      window.navigator.camera.getPicture($scope.footerInputOptions.onGetPictureSuccess, $scope.footerInputOptions.onGetPictureFail, {
        quality: 50,
        destinationType: window.Camera.DestinationType.FILE_URI,
        saveToPhotoAlbum: true
      });
    }
  };
  /**
   * 选取现有照片。
   */
  $scope.choosePicture = function() {
    if ($scope.footerInputOptions && $scope.footerInputOptions.onGetPictureSuccess) {
      window.navigator.camera.getPicture($scope.footerInputOptions.onGetPictureSuccess, $scope.footerInputOptions.onGetPictureFail, {
        quality: 50,
        destinationType: window.Camera.DestinationType.FILE_URI,
        sourceType: window.Camera.PictureSourceType.SAVEDPHOTOALBUM
      });
    }
  };

  $scope.startEdit = function() {
    $scope.footer.recordReady = false;
    $scope.footer.recording = false;
    $timeout(function() {
      document.querySelector('.footerinput input').focus();
    });
  };
  /**
   * 发送文字消息。
   */
  $scope.sendText = function() {
    if ($scope.footerInputOptions && $scope.footerInputOptions.onSendText) {
      $scope.footerInputOptions.onSendText($scope.text);
      $scope.text = '';
    }
  };

  document.querySelector('.send').addEventListener('touchstart', function(event) {
    $scope.sendText();
    event.preventDefault();
    event.stopPropagation();
    event.stopImmediatePropagation();
    $scope.$digest();
  });
});
