import { withReduxStore } from '../../../helpers/store/helpers';
import { ActivityIndicator } from 'react-native';
import React, { useRef } from 'react';
import { WebComponent } from '../../../webComponent/WebComponent';
import { PresentationMode, WebComponentType } from '../../../types/internal/webComponent.types';
import { UNCardAddToWalletStatus } from '../../../types/shared/wallet.types';
import UnitAddToWalletButtonView from '../UnitAddToWalletButtonNativeComponent';
import WebView from 'react-native-webview';
import {
  getStylesObject,
} from './UNAddToWalletBottomSheetItem.styles';
import { useCardWallet } from '../../../helpers/pushProvisioningService/hooks/useCardWallet';
import { getAddToWalletParams, getAddToWalletWindowParams } from './UNAddToWalletComponent.utils';
import { WebViewMessage } from '../../../messages/webMessages';
import { UnitComponentsMessage } from '../../../messages/webMessages/unitComponentsMessages';
import { injectHtmlFullScreenHeight } from '../../../components/UNBottomSheetComponent/UNBottomSheetComponent.utils';
import { eventBus } from '../../../utils/eventBus';
import {
  overFullScreenHeight,
} from '../../../components/UNBottomSheetComponent/UNBottomSheetComponent.constants';
import { launchStartCardProvisioning } from '../../../helpers/pushProvisioningService/startProvisioning';
import { PageMessage } from '../../../messages/webMessages/pageMessage';
import { UNBaseView } from '../../UNBaseView';

export interface UNAddToWalletComponentProps {
  customerToken: string;
  cardId: string;

  // ui
  theme?: string;
  language?: string;
}

const UNAddToWalletComponent = (props: UNAddToWalletComponentProps) => {
  const webRef = useRef<WebView>(null);
  const { currentUNWallet } = useCardWallet(props.cardId);
  const styles = getStylesObject();

  const shouldRenderAddToWalletWebView = () => {
    return currentUNWallet &&
      (currentUNWallet.status === UNCardAddToWalletStatus.readyToProvisioning ||
        currentUNWallet.status === UNCardAddToWalletStatus.addedToWallet);
  };
  const handleAddToWalletClicked = () => {
    if (!currentUNWallet) return;
    launchStartCardProvisioning(currentUNWallet);
  };

  const handleWebViewMessage = (message: WebViewMessage) => {
    if (!message || !message.type) return;
    switch (message.type) {
      case PageMessage.PAGE_LOADED:
        injectHtmlFullScreenHeight(webRef.current, overFullScreenHeight);
        break;
      case UnitComponentsMessage.UNIT_REQUEST_CLOSE_FLOW:
        eventBus.emit(
          UnitComponentsMessage.UNIT_REQUEST_CLOSE_FLOW,
          {}
        );
        break;
      default:
        break;
    }
  };
  const renderAddToWalletWebView = () => {
    if (currentUNWallet == undefined || currentUNWallet?.status === UNCardAddToWalletStatus.pending) {
      return <ActivityIndicator style={styles.loader} />;
    }
    return (
      <UNBaseView style={{flex: 1}} fallback={<></>}>
        {
          shouldRenderAddToWalletWebView() &&
            <WebComponent
              ref={webRef}
              type={WebComponentType.cardAction}
              presentationMode={PresentationMode.CoverInjectedHeight}
              params={getAddToWalletParams(props)}
              theme={props.theme}
              language={props.language}
              isScrollable={false}
              onMessage={(message: WebViewMessage) => handleWebViewMessage(message)}
              windowParams={getAddToWalletWindowParams(currentUNWallet)}
            />
        }

        {
          currentUNWallet?.status === UNCardAddToWalletStatus.readyToProvisioning &&
          <UnitAddToWalletButtonView style={styles.addToWalletButton} onAddToWalletClicked={handleAddToWalletClicked} />
        }
      </UNBaseView>
    );
  };
  return (
    renderAddToWalletWebView()
  );
};

export default withReduxStore<UNAddToWalletComponentProps>(UNAddToWalletComponent);
