import {
  errorRecord,
  firebaseAttributes,
} from "react-native-digitcamera/src/common/libEventLog/libCrashlytics/libCrashlyticsEvent";
import { UELogEvent } from "react-native-digitcamera/src/common/libEventLog/UserExperior/LibUserExperior";
import {
  callChassisPoseDetection,
  callPoseDetection,
  getCurrentActivity,
} from "react-native-digitcamera/src/Screens/OfflineFlow/CommonFunc";
import { initMediaFile } from "react-native-digitcamera/src/Screens/SilentUploadV2/DatabaseHelpers";
var invalidData;

export const checkPoseDetection = async () => {
  try {
    var activity = await getCurrentActivity();
    UELogEvent("Check Pose Detection started in InvalidImages");
    mediaData = activity.mediaUpload;
    invalidData = mediaData.filter((item) => {
      return item.status == "pending";
    });

    var index = 0;
    // var fillteredInvalidData = invalidData.filter((item) => {
    //   return !canUpload(item.imageName);
    // });
    // console.log("fillteredInvalidData :", fillteredInvalidData);

    nextImage(index);
  } catch (error) {
    UELogEvent("Check Pose Detection Stopped in Catch", {
      error: JSON.stringify(error),
    });
    firebaseAttributes(error);
    errorRecord(error);
    console.log("in checkPoseDetection() ", error);
  }
};
const nextImage = async (index) => {
  try {
    console.log(
      "nextImage : invalidData :",
      invalidData.length,
      invalidData[index],
      invalidData
    );
    await getImageForPoseDetection(invalidData[index], () => {
      console.log(
        "CreateImage : Gone For Pose Detection :",
        index,
        invalidData
      );
      index = index + 1;
      if (index < invalidData.length) {
        nextImage(index);
      }
    });
  } catch (error) {
    console.log("Error : nextImage : ", error);
  }
};
const getImageForPoseDetection = async (item, updateIndexCallback) => {
  try {
    console.log(
      "createActivityId : getImageForPoseDetection :  Item for Pose dettection ",
      item
    );
    if (
      item?.imageName == "SF" ||
      item?.imageName == "VDO" ||
      item?.imageName == "SF" ||
      item?.imageName == "CNG" ||
      item?.imageName == "IWS" ||
      item?.imageName?.includes("RCB") ||
      item?.imageName == "PUC"
    ) {
      console.log(
        "item?.imageUri item?.imageUri : will upload",
        item?.uri,
        item?.imageName
      );
      // var ext = item?.imageName == "VDO" ? ".mp4" : ".jpg";
      // await initMediaFile(
      //   item?.uri,
      //   item?.imageName,
      //   ext,
      //   "poseSuccess",
      //   () => {
      //     updateIndexCallback();
      //   }
      // );
      updateIndexCallback();
    } else if (item?.imageName === "CHP") {
      try {
        console.log("item : CHP POSE :", item);
        await callChassisPoseDetection(
          item?.uri,
          item?.imageName,
          async () => {
            await chassiesPoseDetectionSucess(
              item?.uri,
              item?.imageName,
              () => {
                updateIndexCallback();
              }
            );
          },
          async () => {
            await chassiesPDetectionFailed(item?.uri, item?.imageName, () => {
              updateIndexCallback();
            });
          }
        );
      } catch (error) {
        await chassiesPDetectionFailed(item?.uri, item?.imageName, () => {
          updateIndexCallback();
        });
        console.log("Error : callChassisPoseDetection call : ", error);
      }
    } else {
      try {
        console.log("item? : POSE :", item);
        await callPoseDetection(
          item?.uri,
          item?.imageName,
          async () => {
            await poseDetectionSucess(item?.uri, item?.imageName, () => {
              updateIndexCallback();
            });
          },
          async () => {
            await poseDetectionFailed(item?.uri, item?.imageName, () => {
              updateIndexCallback();
            });
          }
        );
      } catch (error) {
        await poseDetectionFailed(item?.uri, item?.imageName, () => {
          updateIndexCallback();
        });
        console.log("Error : callPoseDetection call : ", error);
      }
    }
  } catch (error) {
    console.log("CreateActivityId : getImageForPoseDetection : ", error);
  }
};

const chassiesPoseDetectionSucess = async (
  uri,
  imageName,
  updateIndexCallback
) => {
  try {
    console.log("createActivity : chassiesPoseDetectionSucess ", imageName);
    console.log("Uploading chassiesPoseDetectionSucess ", imageName);
    var ext = imageName == "VDO" ? ".mp4" : ".jpg";
    await initMediaFile(uri, imageName, ext, "poseSuccess", () => {
      updateIndexCallback();
    });
    updateIndexCallback();
  } catch (error) {
    console.log("Error : chassiesPoseDetectionSucess : ", error);
  }
};

const chassiesPDetectionFailed = async (
  uri,
  imageName,
  updateIndexCallback
) => {
  try {
    console.log("createActivity : chassiesPDetectionFailed ", imageName);
    var ext = imageName == "VDO" ? ".mp4" : ".jpg";
    await initMediaFile(uri, imageName, ext, "poseFailed", () => {
      updateIndexCallback();
    });
  } catch (error) {
    console.log("Error : chassiesPDetectionFailed : ", error);
  }
};

const poseDetectionSucess = async (uri, imageName, updateIndexCallback) => {
  try {
    console.log("createActivity : poseDetectionSucess ", imageName);
    console.log("Uploading poseDetectionSucess ", imageName);
    var ext = imageName == "VDO" ? ".mp4" : ".jpg";
    await initMediaFile(uri, imageName, ext, "poseSuccess", () => {
      updateIndexCallback();
    });
    updateIndexCallback();
  } catch (error) {
    console.log("Error : poseDetectionSucess : ", error);
  }
};

const poseDetectionFailed = async (uri, imageName, updateIndexCallback) => {
  try {
    console.log("createActivity : poseDetectionFailed ", imageName);
    var ext = imageName == "VDO" ? ".mp4" : ".jpg";
    await initMediaFile(uri, imageName, ext, "poseFailed", () => {
      updateIndexCallback();
    });
  } catch (error) {
    console.log("Error : poseDetectionFailed : ", error);
  }
};
