import { View, Text } from "react-native";
import React, { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import Database from "../../../../Components/common/DAsyncStorage";
import store from "react-native-digitcamera/src/redux/store";
import { CreateActivityId } from "react-native-digitcamera/src/Screens/OfflineFlow/CreateActivityId";
import { dlog } from "react-native-digitcamera/src/common/DLogger";
import { NamedModal } from "react-native-digitcamera/src/common/GlobalModal";
import { startOfflineBackgroundUpload } from "react-native-digitcamera/src/Screens/SilentUpload/SilentUploadUtils";

function oLog() {
  dlog("[OFFLINE ACTIVITY LISTENER] [INFO]", ...arguments);
}

const updateReduxCallback = async () => {
  const vechicleData = await Database.get("PIVechicleData");
  var registrationNumber = vechicleData?.licensePlateNumber;
  var quoteNumber = vechicleData?.quoteNumber;
  var activityTypeId = vechicleData?.activityTypeId;
  var activityId = vechicleData?.activityId;
  var toTutorial = vechicleData?.toTutorial;
  var toWithQuote = vechicleData?.toWithQuote;

  store.dispatch({
    type: "ACTIVITY_DATA",
    payload: {
      activityId: activityId,
      activityTypeId: activityTypeId,
      licensePlateNumber:
        registrationNumber === undefined ? "" : registrationNumber,
      toWithQuote: toWithQuote,
      toTutorial: toTutorial,
      quoteNumber: quoteNumber,
    },
  });
};

const addSensorDataCallback = (sensorImageDetails) => {
  store.dispatch({
    type: "SENSOR_DETAILS",
    payload: sensorImageDetails,
  });
};

const createActivity = async () => {
  try {
    var mediaStatusObjs = await Database.get("PIMediaStatus");
    var offlineActivities = mediaStatusObjs.filter((activity) => {
      return activity.mediaUpload.some((images) => {
        return images.status == "pending";
      });
    });

    console.log(
      "offlineActivities : offlineActivities",
      JSON.stringify(offlineActivities, null, "  ")
    );

    for (let activity of offlineActivities) {
      if (activity?.mediaUpload?.length > 0) {
        oLog("[CREATING ACTIVITY HERE]", activity);
        await CreateActivityId(
          activity,
          updateReduxCallback,
          addSensorDataCallback
        );
      } else {
        oLog("[CANNOT CREATE ACTIVITY HERE] [Empty offline data]");
      }
    }
  } catch (error) {
    console.log("Error : offlineActivityListener : createActivity : ", error);
  }
};

const OfflineActivityListener = () => {
  const dispatch = useDispatch();

  const network = useSelector((state) => state.network);
  const { isInternetReachable } = network;

  useEffect(() => {
    if (isInternetReachable === true) {
      // dispatch({
      //   type: "GLOBAL_MODAL_HIDE",
      // });
      oLog(" calling CreateActivityid");
      createActivity();
    } else if (isInternetReachable === false) {
      oLog(" no internet access");
    }
  }, [network]);

  return null;
};

export default OfflineActivityListener;
