import { Notifications } from 'expo';

import Constants from 'expo-constants';

// In this test app we contact the Expo push service directly. You *never*
// should do this in a real app. You should always store the push tokens on your
// own server or use the local notification API if you want to notify this user.
const PUSH_ENDPOINT = 'https://expo.io/--/api/v2/push/send';

export default async function registerForPushNotificationsAsync() {
  // this method assumes the user has already granted permission
  // to receive remote notificartions.

  // Get the token that uniquely identifies this device
  const token = await Notifications.getExpoPushTokenAsync();

  // Log it so we can easily copy it if we need to work with it
  console.log(`Got this device's push token: ${token}`);

  await Notifications.createCategoryAsync('welcome', [
    {
      actionId: 'tada',
      buttonTitle: '🎉',
      isDestructive: false,
      isAuthenticationRequired: false,
    },
    {
      actionId: 'heart_eyes',
      buttonTitle: '😍',
      isDestructive: false,
      isAuthenticationRequired: true,
    },
  ]);

  // POST the token to the Expo push server
  const response = await fetch(PUSH_ENDPOINT, {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify([
      {
        to: token,
        title: 'Welcome to Expo!',
        body: 'Native Component List is registered for push notifications.',
        data: { example: 'sample data' },
        _category: `${Constants.manifest.id}:welcome`,
      },
    ]),
  });

  const result = await response.json();
  if (result.errors) {
    for (const error of result.errors) {
      console.warn(`API error sending push notification:`, error);
    }
  }

  const receipts = result.data;
  if (receipts) {
    const receipt = receipts[0];
    if (receipt.status === 'error') {
      if (receipt.details) {
        console.warn(
          `Expo push service reported an error sending a notification: ${
            receipt.details.error
          }`
        );
      }
      if (receipt.__debug) {
        console.warn(receipt.__debug);
      }
    }
  }
}
