import { WebComponent } from '../../webComponent/WebComponent';
import React, { useState } from 'react';
import { PresentationMode, WebComponentType } from '../../types/internal/webComponent.types';
import { withReduxStore } from '../../helpers/store/helpers';
import { RESPONSE_KEYS, UnitOnLoadResponseEvent } from '../../messages/webMessages/onLoadMessage';
import { UNAccountData, UNError, UNOnLoadResponse, UNOnLoadResponseData } from '../../types/shared';
import { WebViewMessage } from '../../messages/webMessages';
import { PaymentMessage } from '../../messages/webMessages/paymentsMessage';
import { UnitMessage } from '../../messages/webMessages/unitMessages';
import { HeightEvent, PageMessage } from '../../messages/webMessages/pageMessage';
import { getWirePaymentParams, getWirePaymentScript } from './UNWirePaymentComponent.utils';
import { UNWirePaymentData, WirePaymentCreatedEvent } from '../../types/shared/wirePayment.types';
import { UNBaseView } from '../../nativeComponents/UNBaseView';
import { ensureArray } from '../../utils/onLoadMessages.utils';

export interface UNWirePaymentComponentProps {
  // inputs
  customerToken: string;
  accountId?: string;
  fee?: number;

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

  isAutoFocus?: boolean;
  initialStageBackButton?: boolean;
  finalStageDoneButton?: boolean;

  // events
  onPaymentCreated?: (data: UNWirePaymentData) => void;
  onLoad?: (response: UNOnLoadResponse<[UNAccountData]>) => void;
  onInitialStageBackButtonClicked?: () => void;
  onFinalStageDoneButtonClicked?: () => void;
}

const UNWirePaymentComponent = (props: UNWirePaymentComponentProps) => {
  const [height, setHeight] = useState<number>(0);
  const [presentationMode, setPresentationMode] = useState<PresentationMode>(PresentationMode.Inherit);

  const handleUnitOnLoad = (response: UnitOnLoadResponseEvent) => {
    if (!props.onLoad) {
      return;
    }

    if (RESPONSE_KEYS.errors in response) {
      props.onLoad(response as UNError);
      return;
    }

    if (RESPONSE_KEYS.account in response) {
      const account = response[RESPONSE_KEYS.account].data;
      const paymentOnLoad: UNOnLoadResponseData<[UNAccountData]> = {
        data: ensureArray(account),
      };
      props.onLoad(paymentOnLoad);
      return;
    }

    console.error('On Load Error: unexpected response type');
    return;
  };

  const handleWebViewMessage = (message: WebViewMessage) => {
    if (!message || !message.details) return;
    switch (message.type) {
      case PaymentMessage.INITIAL_STAGE_BACK_BUTTON_CLICKED: {
        props.onInitialStageBackButtonClicked && props.onInitialStageBackButtonClicked();
        break;
      }
      case PaymentMessage.FINAL_STAGE_DONE_BUTTON_CLICKED: {
        props.onFinalStageDoneButtonClicked && props.onFinalStageDoneButtonClicked();
        break;
      }
      case PaymentMessage.PAYMENT_CREATED:
        props.onPaymentCreated && props.onPaymentCreated((message.details as WirePaymentCreatedEvent).data);
        break;
      case UnitMessage.UNIT_ON_LOAD:
        handleUnitOnLoad(message.details as UnitOnLoadResponseEvent);
        break;
      case PageMessage.PAGE_HEIGHT: {
        const currentHeight = (message.details as HeightEvent).height;
        setHeight(currentHeight);
        if (presentationMode === PresentationMode.Inherit && currentHeight === 0) {
          setPresentationMode(PresentationMode.Default);
        }
        break;
      }
    }
  };

  const style = presentationMode === PresentationMode.Inherit ? { flex: 1 } : { height: height };
  return (
    <UNBaseView style={style}>
      <WebComponent
        type={WebComponentType.wirePayment}
        presentationMode={presentationMode}
        params={getWirePaymentParams(props)}
        script={getWirePaymentScript()}
        onMessage={(message: WebViewMessage) => handleWebViewMessage(message)}
        isScrollable={true}
        theme={props.theme}
        language={props.language}
      />
    </UNBaseView>
  );
};

export default withReduxStore<UNWirePaymentComponentProps>(UNWirePaymentComponent);
