// install   :      cordova plugin add https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin.git
// link      :      https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin

var angular = require('angular');
require('../modules');
require('../services/CommonService');
require('jsUtil');

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

module.factory('$toast', ['$q', '$window', 'CommonService', function($q, $window, CommonService) {
  var templateFn = function (showFn, argsLen) {
    return function() {
      var q = $q.defer();
      if (!($window.plugins && $window.plugins.toast)) {
        return CommonService.showAlert(arguments[0]);
      } else {
        var fn = $window.plugins.toast[showFn];
        var args = _.toArray(arguments).slice(0, argsLen);
        var successCallback = function(response) {
          q.resolve(response);
        };
        var errorCallback = function(error) {
          q.reject(error);
        };
        args.push(successCallback, errorCallback);
        fn.apply($window.plugins.toast, args);
      }
      
      return q.promise;
    };
  };

  return {
    showShortTop: templateFn('showShortTop', 1),
    showShortCenter: templateFn('showShortCenter', 1),
    showShortBottom: templateFn('showShortBottom', 1),
    showLongTop: templateFn('showLongTop', 1),
    showLongCenter: templateFn('showLongCenter', 1),
    showLongBottom: templateFn('showLongBottom', 1),
    show: templateFn('show', 3)
  };
}]);
