'use strict';

var angular = require('angular');
require('../modules');

var module = angular.module('offlinereminder.services');

module.factory('OfflineRemoteService', OfflineRemoteService);

/**
 * @ngInject
 */

function OfflineRemoteService($http) {

  return {
    sendReminder: sendReminder
  };

  /**
   * 绑定离线消息通道的id与设备id
   * @return {Promise}
   */
  function sendReminder(devSn, manufacturer, clientIdXiaomi, clientIdGetui, clientIdHuawei) {
    var url = '/offline/register?devSn=' + window.device.uuid;
    url= url + '&manufacturer=' + window.device.manufacturer;
    url = !_.isEmpty(clientIdGetui) ? url + '&getuiClientId=' + clientIdGetui : url;
    url = !_.isEmpty(clientIdXiaomi) ? url + '&xiaomiClientId=' + clientIdXiaomi : url;
    url = !_.isEmpty(clientIdHuawei) ? url + '&huaweiClientId=' + clientIdHuawei : url;

    return $http({
      url: url,
      method: 'post',
      timeout: 5000
    }).then(function (response) {
      if (response.status >= 200 && response.status < 300) {
        return true;
      } else {
        throw new Error('绑定离线消息通道的id与设备id失败');
      }
    });
    }
}