import { Dispatch, SetStateAction } from "react";
import {
  createQuote,
  fetchComparisonPlanList,
  getBenefitPageData,
  getMarketBenefitComparePageData,
  getMigrationBenefitPageData,
  getQuotePageData,
  getRenewalPlanMigrationPageData,
  getWaitingPeriodScreenData,
} from "../../Container/Navi-Insurance/network";
import { ActionMetaData, BaseActionTypes } from "../actions/GenericAction";
import {
  AXIOS_CODE_CANCELED,
  AXIOS_CODE_NETWORK,
  AnalyticsEventNameConstants,
  AnalyticsModuleNameConstant,
  EVENT_NAMES,
} from "../constants";
import {
  sendAsAnalyticsEvent,
  sendAsGlobalErrorEvent,
} from "../hooks/useAnalyticsEvent";
import { ScreenData } from "../interface/widgets/screenData/ScreenData";
import {
  getErrorTitleFromError,
  getErrorTypeFromStatusCode,
} from "../utilities/ErrorUtils";
import { ScreenState } from "./BaseScreen";
import { ScreenActionTypes } from "./ScreenActionTypes";

export const ScreenActionHandler = {
  handleScreenAction: (
    screenMetaData: ActionMetaData,
    setScreenData: Dispatch<SetStateAction<ScreenData | null>>,
    screenData?: ScreenData | null,
    navigation?: any,
  ) => {
    switch (screenMetaData.actionType) {
      case ScreenActionTypes.FETCH_INSURANCE_QUOTE_PAGE_FROM_BACKEND: {
        return getQuotePageData(setScreenData, screenMetaData);
      }
      case ScreenActionTypes.FETCH_COMPARE_PLAN_LIST: {
        return fetchComparisonPlanList(screenMetaData, setScreenData);
      }
      case ScreenActionTypes.FETCH_QUOTE_V4: {
        return createQuote(screenMetaData, setScreenData, navigation);
      }
      case ScreenActionTypes.FETCH_BENEFIT_COMPARE_LIST: {
        return getMarketBenefitComparePageData(screenMetaData, setScreenData);
      }
      case ScreenActionTypes.FETCH_WAITING_PERIOD_SCREEN: {
        return getWaitingPeriodScreenData(screenMetaData, setScreenData);
      }
      case ScreenActionTypes.FETCH_BENEFIT_LIST: {
        return getBenefitPageData(screenMetaData, setScreenData);
      }
      case ScreenActionTypes.FETCH_MIGRATION_BENEFIT_LIST: {
        return getMigrationBenefitPageData(screenMetaData, setScreenData);
      }

      case ScreenActionTypes.FETCH_RENEWAL_PLAN_MIGRATION_SCREEN: {
        return getRenewalPlanMigrationPageData(screenMetaData, setScreenData);
      }

      case ScreenActionTypes.SHOW_LOADER: {
        const updatedScreenData: ScreenData = {
          ...screenData,
          screenState: ScreenState.LOADING,
        };
        setScreenData(updatedScreenData);
        return;
      }
      default:
        return;
    }
  },
};

export const handleResponseData = (
  setScreenData: Dispatch<SetStateAction<ScreenData | null>>,
  response: any,
) => {
  const updatedScreenData: ScreenData = {
    ...(response.data as ScreenData),
    screenState: ScreenState.LOADED,
  };
  setScreenData(updatedScreenData);
};

export const handleErrorData = (
  error: any,
  setScreenData: Dispatch<SetStateAction<ScreenData | null>>,
  screenMetaData: ActionMetaData,
  flowName: string,
  methodName: string,
  startTime: number,
  apiUrl: string,
  apiMethod: string,
) => {
  sendAsAnalyticsEvent({
    name: AnalyticsEventNameConstants.HI_RN_INVALID_DATA_ERROR,
    properties: {
      errorType: EVENT_NAMES.API_NO_RESPONSE_ERROR,
      error: `${error}`,
      methodName: "handleScreenAction",
    },
  });
  const latency = Date.now() - startTime;
  const errorEvent: GlobalErrorData = {
    reason: `${error.message}, axiosError: ${error.axiosCode}`,
    source: screenMetaData.screenName || "",
    statusCode: error.statusCode,
    moduleName: AnalyticsModuleNameConstant.GI,
    flowName,
    methodName: methodName,
    globalErrorType: getErrorTypeFromStatusCode(error.statusCode),
    isAppDowntimeEvent: false,
    errorCode: error.axiosCode,
    errorTitle: getErrorTitleFromError(error.statusCode, error.axiosCode),
    latency: latency,
    isNae: !(
      error.statusCode === 20 ||
      error.axiosCode === AXIOS_CODE_NETWORK ||
      error.axiosCode === AXIOS_CODE_CANCELED
    ),
    apiUrl: apiUrl,
    apiMethod: apiMethod,
  };
  sendAsGlobalErrorEvent(errorEvent);
  const updatedScreenData: ScreenData = {
    screenState: ScreenState.ERROR,
    errorMetaData: {
      baseActionType: BaseActionTypes.SCREEN_ACTION,
      metaData: [
        {
          actionType: ScreenActionTypes.SHOW_LOADER,
        },
        screenMetaData,
      ],
      errorStatusCode: error.statusCode,
      errorAxiosCode: error.axiosCode,
    },
  };
  setScreenData(updatedScreenData);
};
