import { ITelephonyCapabilities } from '../../store/pbx/types';
import React, { useEffect } from 'react';
import { eventEmitter, EventType } from '../../services/EventEmitter';
import {
  checkTelephonyCapabilities,
  getCurrentForwardStatus,
  getForwardAvailableOptions,
  getForwardAvailableOptionsResult,
  setNomadicNumber,
  setForwardPhoneNumber,
  setTelephonyCapabilities,
  unregisterForwardStatusChangeListener,
  updateCallForwardStatus,
} from '../../store/pbx/pbxSlice';
import { Logger } from '../../utils/Log';
import { MenuTelephonyView } from './MenuTelephonyView';
import { Presence } from '../../store/contacts/types';
import { useAppDispatch, useAppSelector } from '../../store/hooks';

const logger = new Logger('TelephonyContainer');

/**
 * @module
 * @name Telephony
 * @public
 */
export const TelephonyContainer: React.FunctionComponent = () => {
  const dispatch = useAppDispatch();
  const user = useAppSelector((state) => state.authReducer.user);
  const isTelephonyEnabled = useAppSelector((state) => state.pbx.isTelephonyEnabled);

  const presence = user ? (user.contact.presence as keyof typeof Presence) : 'unsubscribed';
  const busyUser =
    presence === 'busy_phone' || presence === 'busy_audio' || presence === 'busy_video' || presence === 'busy';

  useEffect(() => {
    getCurrentForwardStatus();
    getForwardAvailableOptions();
    checkTelephonyCapabilities();

    eventEmitter.addListener(EventType.TelephonyCapabilityUpdate, (eventData: ITelephonyCapabilities) => {
      logger.info(`'onTelephonyCapabilityUpdated'${eventData}`);
      dispatch(setTelephonyCapabilities(eventData));
    });
    const handleCallForwardStatus = (status: string) => {
      logger.info(`'onCallForwardStatusUpdates'${status}`);
      dispatch(updateCallForwardStatus(status));
    };
    const handleCallForwardOptionsUpdated = (options: []) => {
      logger.info(`onCallForwardOptionsUpdates${options}`);
      dispatch(getForwardAvailableOptionsResult(options));
    };
    const handleNomadicNumberUpdates = (nomadicNumber: string) => {
      logger.info(`onNomadicNumberUpdates: ${nomadicNumber}`);
      dispatch(setNomadicNumber(nomadicNumber));
    };

    const handleCallForwardNumberDestination = (destinationNumber: string) => {
      logger.info(`forwardNumberDestination: ${destinationNumber}`);
      setForwardPhoneNumber(destinationNumber);
    };
    const callForwardNumberDestinationListener = eventEmitter.addListener(
      EventType.CallForwardNumberDestination,
      handleCallForwardNumberDestination
    );
    const callForwardStatusListener = eventEmitter.addListener(
      EventType.CallForwardStatus,
      handleCallForwardStatus
    );
    const callForwardOptionsUpdatedListener = eventEmitter.addListener(
      EventType.CallForwardOptionsUpdated,
      handleCallForwardOptionsUpdated
    );
    const nomadicNumberUpdatedListener = eventEmitter.addListener(
      EventType.NomadicNumberUpdated,
      handleNomadicNumberUpdates
    );
    return () => {
      dispatch(unregisterForwardStatusChangeListener());
      callForwardNumberDestinationListener.remove();
      callForwardOptionsUpdatedListener.remove();
      callForwardStatusListener.remove();
      nomadicNumberUpdatedListener.remove();
    };
  }, []);
  return (
    (isTelephonyEnabled && !busyUser) ? <MenuTelephonyView  /> : null);
};