/**
 * 信鸽推送
 * Created by Jeepeng on 16/8/3.
 */

import {
  Platform,
  NativeModules,
  NativeEventEmitter,
} from 'react-native';

let {XGPushManager} = NativeModules;
let XGNativeEventEmitter = new NativeEventEmitter(XGPushManager);

let _handlers = new Map();

const EventMapping = {
  register: 'remoteNotificationsRegistered',
  notification: 'remoteNotificationReceived',
  localNotification: 'localNotificationReceived',
  message: 'messageReceived'
};

class XPush {
  
  static init(accessId, accessKey) {
    let accessIdNum = Number(accessId);
    if (isNaN(accessIdNum)) {
      console.error(`[XGPush init] accessId is not a number!`);
    } else {
      XGPushManager.startApp(accessIdNum, accessKey);
    }
  }
  
  static register(account) {
    if (Platform.OS === 'ios') {
      XGPushManager.setAccount(account);
      return XGPushManager.requestPermissions({
        alert: true,
        badge: true,
        sound: true
      });
    } else {
      return XGPushManager.registerPush(account);
    }
  }
  
  /**
   * ios only
   * @param deviceToken
   * @returns {*}
   */
  static registerForXG(deviceToken) {
    if (Platform.OS === 'ios') {
      return XGPushManager.registerDevice(deviceToken, null);
    } else {
      return new Promise((resolve, reject) => {
        //reject('ios only');
      });
    }
  }
  
  static setTag(tagName) {
    return XGPushManager.setTag(tagName);
  }
  
  static deleteTag(tagName) {
    if (Platform.OS === 'ios') {
      return XGPushManager.delTag(tagName);
    } else {
      return XGPushManager.deleteTag(tagName);
    }
  }
  
  static unRegister() {
    if (Platform.OS === 'ios') {
      return XGPushManager.unRegisterDevice();
    } else {
      return XGPushManager.unregisterPush();
    }
    
  }
  
  static setApplicationIconBadgeNumber(number) {
    if (Platform.OS === 'ios') {
      XGPushManager.setApplicationIconBadgeNumber(number);
    }
  }
  
  static getApplicationIconBadgeNumber(callback) {
    if (Platform.OS === 'ios') {
      XGPushManager.getApplicationIconBadgeNumber(callback);
    }
  }
  
  static checkPermissions(callback) {
    if (Platform.OS === 'ios') {
      return XGPushManager.checkPermissions(callback);
    }
  }
  
  static getInitialNotification() {
    return XGPushManager.getInitialNotification();
  }
  
  
  static onRegister(callback) {
    this.addListener('register', callback);
  }
  
  static onNotification(callback) {
    this.addListener('notification', callback)
  }
  
  static onLocalNotification(callback) {
    this.addListener('localNotification', callback)
  }
  
  /**
   * 透传消息 Android only
   */
  static onMessage(callback) {
    if (Platform.OS === 'android') {
      this.addListener('message', callback)
    }
  }
  
  static addListener(eventType, callback) {
    let event = EventMapping[eventType];
    if (!event) {
      console.warn('XGPush only supports `notification`, `register` and `localNotification` events');
      return;
    }
    let listener = XGNativeEventEmitter.addListener(event, (data) => {
      let result = data;
      if (eventType === 'register') {
        result = data['deviceToken'];
      }
      callback(result);
    });
    _handlers.set(callback, listener);
  }
  
  static removeListener(eventType, callback) {
    if (!EventMapping[eventType]) {
      console.warn('XGPush only supports `notification`, `register` and `localNotification` events');
      return;
    }
    let listener = _handlers.get(callback);
    if (listener) {
      listener.remove();
      _handlers.delete(callback);
    }
  }
  
  static enableDebug(isDebug = true) {
    XGPushManager.enableDebug(isDebug);
  }
  
  static isEnableDebug() {
    return XGPushManager.isEnableDebug();
  }
  
  static destroy() {
    _handlers.clear();
  }
}

export default XPush;
