import Database from "../../../../../Components/common/DAsyncStorage";
import NetInfo from "@react-native-community/netinfo";
import { is2Wflow } from "react-native-digitcamera/src/common/Constants";

// save data in realm
export const offlinePIDataToRealm = async(isPIDone) => {
  try {
    const mediaStatusObjs = await Database.get("PIMediaStatus");
    const vechicleData = await Database.get("PIVechicleData");
    console.log("!is2Wflow(vechicleData?.activityTypeId)  isPIDone",!is2Wflow(vechicleData?.activityTypeId),isPIDone);
    if(!is2Wflow(vechicleData?.activityTypeId)||isPIDone)
    {
      console.log("In if offlinePIDataToRealm ");
    let offlineImageData = [];
    console.log(
      "CommonFunc  offlinePIDataToRealm() mediaStatusObjs \n\n\n ",
      mediaStatusObjs
    );
    offlineImageData = [];
    mediaStatusObjs?.filter((item) => {
      if (item?.status === "failed") {
        offlineImageData.push(item);
      }
    });

    await Database.update("PIOfflineVechicleData", {
        activityId: vechicleData.activityId,
        registrationNumber: vechicleData.licensePlateNumber,
        quoteNumber: vechicleData.quoteNumber,
        dataTobeUploaded: JSON.stringify(offlineImageData),
        isPICompleted:isPIDone
      });
    }
    else
    {
      console.log("In else offlinePIDataToRealm ");
    }
  } catch (error) {
    console.log("***** offlinePIDataToRealm catch ",error);
  }
};

// checking internet
export const checkInternet = async () => {
  const state = await NetInfo.fetch();
  console.log("Connection type", state.type);
  console.log("Is connected?", state.isConnected);
  return state.isConnected;
};

// make desion for offline/online flow
export const checkFlow = () => {};

export const clearAllRealmData = async() => {
  try {
    await Database.remove("PIOfflineData");
    await Database.remove("PIVechicleData");
    await Database.remove("PIVechicleInstruction");

    await Database.update("PIGonePoseDetection", {
        GoneposeDetectionCount: 0,
      });

      await Database.update("PIPoseDetectionCompleted", {
        poseDetectionCompletedCount: 0,
      });

      await Database.update("PIRetryCount", {
        retryCount: 0,
        index: 0,
        isAddNewImageData: false,
      });

      await Database.update("PIclickedCount", {
        clickedCount: 0,
      });

      await Database.update("PIUploadedCount", {
        uploadedCount: 0,
      });
      await Database.remove("PIMediaStatus");
      await Database.remove("PIVideoCompleteStatus");
  } catch (error) {}
};

export const updateStatusRealm = async(uri, DocumentImageName, newStatus) => {
  var PIVechicleData =  await Database.get("PIVechicleData");
  console.log("CommonFunc.js  updateStatusRealm() PIVechicleDat?.activityId ",PIVechicleData?.activityId);
  var activityId = PIVechicleData?.activityId;
  var quoteNumber = PIVechicleData?.quoteNumber;
  var licensePlateNumber = PIVechicleData?.licensePlateNumber;
  const uploadObj = await Database.get("PIMediaStatus");
  console.log("uploadObj from data base",JSON.stringify(uploadObj,null,"  "));
var newObj=[]
if(uploadObj!=null)
{
    if (uploadObj instanceof Array) {
        var tempObj=[]
        uploadObj.map((item)=>{
           tempObj.push(item)
        })

        // adding new object in array
        tempObj.push({
        activityId,
        imageName: DocumentImageName,
        status: newStatus,
        uri: uri,
        quoteNumber: quoteNumber,
        regNumber: licensePlateNumber,
      })
      newObj=tempObj
    }
}else
{
    newObj.push({
        activityId,
        imageName: DocumentImageName,
        status: newStatus,
        uri: uri,
        quoteNumber: quoteNumber,
        regNumber: licensePlateNumber,
      })
}
  await Database.set("PIMediaStatus", newObj);
var media= await Database.get("PIMediaStatus")
  console.log("*** CommonFunc.js updateStatusRealm():  ",JSON.stringify( media,null,"  "));
};

