import firebase from 'react-native-firebase';
import { NavigationActions } from 'react-navigation';
import { createLogic } from 'redux-logic';
import { AuthActions } from '../actions';
import {
  Dispatch,
  RootState,
  UserRole,
} from '../models';
import { Routes } from '../navigation';

const listenForNotification = createLogic({
  type: AuthActions.ActionKey.INITIALIZE_USER_FLOW,
  warnTimeout: 0,
  processOptions: {
    dispatchMultiple: true,
  },
  process({ getState }: { getState: () => RootState }, dispatch: Dispatch) {
    firebase.messaging().getInitialNotification().then((body: any) => {
      if (body.conversationId) {
        dispatch(NavigationActions.navigate({
          routeName: Routes.CHATROOM,
          params: { conversationId: body.conversationId },
        }));
      } else if (body.offerId) {
        if (getState().user.role === UserRole.EMPLOYEE) {
          dispatch(NavigationActions.navigate({
            routeName: Routes.OFFER_DETAILS_EMPLOYEE,
            params: { offerId: body.offerId },
          }));
        }
      }
    });
    firebase.messaging().onMessage((message) => {
      const navigation = getState().navigation;
      const currentRoute = navigation.routes[navigation.index];
      const inSameChatroom = currentRoute.routeName === Routes.CHATROOM
        && currentRoute.params.conversationId === message.conversationId;
      const inSameOfferDetails = currentRoute.routeName === Routes.OFFER_DETAILS_EMPLOYEE
        && currentRoute.params.offerId === message.offerId;
      if (message.conversationId) {
        if (message.opened_from_tray) {
          dispatch(NavigationActions.navigate({
            routeName: Routes.CHATROOM,
            params: { conversationId: message.conversationId },
          }));
        } else if (!message.local_notification && !inSameChatroom) {
          firebase.messaging().createLocalNotification({
            local_notification: true,
            title: message.fcm.title,
            body: message.fcm.body,
            sound: 'default',
            icon: 'ic_work',
            fire_date: Date.now(),
            id: message.conversationId,
            show_in_foreground: true,
            conversationId: message.conversationId,
          });
        }
      } else if (message.offerId) {
        if (message.opened_from_tray) {
          dispatch(NavigationActions.navigate({
            routeName: Routes.OFFER_DETAILS_EMPLOYEE,
            params: { offerId: message.offerId },
          }));
        } else if (!message.local_notification && !inSameOfferDetails) {
          firebase.messaging().createLocalNotification({
            local_notification: true,
            title: message.fcm.title,
            body: message.fcm.body,
            sound: 'default',
            icon: 'ic_work',
            fire_date: Date.now(),
            id: message.offerId,
            show_in_foreground: true,
            offerId: message.offerId,
          });
        }
      }
    });
  },
});

export const notificationLogic = [
  listenForNotification,
];
