import UploadFile from "../UploadFile";
import Database from "../../../../../Components/common/DAsyncStorage";
import store from "../../redux/store";

let sensorImageDetails = [];

//Uploads the failed data
export const reuploadFailedDocuments = async (
  failedData,
  addSensorDataCallback
) => {
  failedData.forEach((item) => {
    let uri = item?.uri;
    let imageName = item?.imageName;
    let ext = imageName == "VDO" ? ".mp4" : ".jpg";
    console.log("[INFO] . Reupload . Utils . ReUploading Item -> ", item);
    UploadFile(
      uri,
      imageName,
      ext,
      () => {
        //   props.navigation.navigate("WithQuote");
      },
      async () => {
        await addSensorImageData(ext, true, imageName);
        addSensorDataCallback(sensorImageDetails);
      },
      async () => {
        addSensorImageData(ext, false, imageName);
        await addSensorDataCallback(sensorImageDetails);
      }
    );
  });
};

// Sensor data
export const addSensorImageData = async (ext, ImageStatus, docName) => {
  const PILocation = await Database.get("PILocation");
  const PIRetryCount = await Database.get("PIRetryCount");
  let offlineImageData = await Database.get("PIOfflineVechicleData");
  var currentLatitude = PILocation?.lat;
  var currentLongitude = PILocation?.long;
  var activityId = offlineImageData?.activityId;
  let imageName = docName;
  var attemptCount = PIRetryCount?.retryCount + 1;
  var fileName = activityId + "_" + imageName + ext;
  //adding sensor data
  sensorImageDetails.push({
    attemptCount: attemptCount,
    fileName: fileName,
    latitude: currentLatitude,
    longitude: currentLongitude,
    pose: imageName,
    success: ImageStatus,
    timestamp: new Date().getTime(),
  });
};
