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

export const initMediaFile = async (uri, DocumentImageName, ext) => {
  const PIVechicleData = await Database.get("PIVechicleData");
  const activityId = PIVechicleData?.activityId;
  const quoteNumber = PIVechicleData?.quoteNumber;
  const regNumber = PIVechicleData?.licensePlateNumber;
  const activityTypeId = PIVechicleData?.activityTypeId;
  const oldStatus = await Database.get("PIMediaStatus");

  console.log(
    "DatabaseHelpers - URI - ",
    uri,
    " imagename -",
    DocumentImageName
  );
  const imageObj = [
    {
      status: "pending", // pending,uploading, uploaded, failed
      uri: uri,
      imageName: DocumentImageName,
    },
  ];

  const currentActivityObj = [
    {
      activityId: activityId,
      activityTypeId: activityTypeId,
      policyNumber: quoteNumber,
      regNumber: regNumber,
      activityStatus: "IN-PROGRESS", // PENDING, IN-PROGRESS, COMPLETED  // this is the status of activity not a perticular image
      mode: "ONLINE", // ONLINE, OFFLINE  // have to make dyanamic
      mediaUpload: imageObj,
      date: moment().format("DD MMM YYYY"),
    },
  ];

  const newObj = await addMediaStatusObj(oldStatus, currentActivityObj);
  console.log(
    "DatabaseHelpers - newObj : ",
    JSON.stringify(newObj, null, "  ")
  );
  await Database.set("PIMediaStatus", newObj);
  const PIMediaStatus = await Database.get("PIMediaStatus");
  console.log(
    "DatabaseHelpers - PIMediaStatus initStatusRealm",
    JSON.stringify(PIMediaStatus, null, "  ")
  );
};

const addMediaStatusObj = async (oldStatus, currentActivityObj) => {
  try {
    console.log("DatabaseHelpers - currentActivityObj", currentActivityObj);
    var objectTobeUpdate = currentActivityObj[0]?.mediaUpload[0];
    var newObj;

    console.log("oldStatus", JSON.stringify(oldStatus, null, " "));
    console.log(
      "DatabaseHelpers - currentActivityObj",
      JSON.stringify(currentActivityObj, null, " ")
    );

    // this will excute only if there is no activity in DB
    if (oldStatus == null || oldStatus == undefined) {
      console.log(
        "DatabaseHelpers - oldStatus == null || oldStatus == undefined",
        currentActivityObj
      );
      return currentActivityObj;
    }

    // this will check for activity is present or not
    console.log(
      "DatabaseHelpers - currentActivityObj[0]?.activityId : ",
      currentActivityObj[0]?.activityId,
      JSON.stringify(oldStatus, null, "  ")
    );
    var isActivityAlreadyExist = oldStatus.find((item) => {
      return item.activityId === currentActivityObj[0]?.activityId;
    });

    console.log(
      "DatabaseHelpers - isActivityAlreadyExist",
      !!isActivityAlreadyExist
    );

    if (!!isActivityAlreadyExist) {
      // checking is current image object is already exist or not
      const isImageExist = isActivityAlreadyExist?.mediaUpload?.some((item) => {
        item.imageName === objectTobeUpdate?.imageName;
      });

      // this will check if any image with same name is exist or not
      // var isImageAlreadyExist = isActivityAlreadyExist?.mediaUpload?.filter(
      //   (imageItem) => imageItem.imageName !== objectTobeUpdate.imageItem
      // );

      const isImageAlreadyExist = isActivityAlreadyExist?.mediaUpload?.filter(
        (imageItem) => {
          return imageItem.imageName != objectTobeUpdate.imageName;
        }
      );
      console.log(
        "DataBaseHelper : isImageAlreadyExist",
        JSON.stringify(isImageAlreadyExist, null, "  ")
      );
      var updatedImageWithOutDuplicates = [
        ...isImageAlreadyExist,
        objectTobeUpdate,
      ];
      console.log(
        "DataBaseHelper : isImageAlreadyExist",
        JSON.stringify(updatedImageWithOutDuplicates, null, "  ")
      );
      // newObj is for activity
      newObj = [
        {
          ...isActivityAlreadyExist,
          mediaUpload: updatedImageWithOutDuplicates,
        },
      ];
      console.log(
        "DatabaseHelpers - Activity is present and new Image obj : ",
        JSON.stringify(newObj, null, "  ")
      );
      oldStatus.pop();
      return oldStatus.concat(newObj); // adding previous Activity + current different
    } else {
      console.log(
        "DatabaseHelpers - Activity is not present so Adding  New Activity ",
        JSON.stringify(currentActivityObj, null, "  ")
      );
      return oldStatus.concat(currentActivityObj);
    }
  } catch (error) {
    console.log("Error in addMediaObj ", error);
  }
};
