/*
This file contains project specific 
background process configurations & utility functions
*/

import {
  updateProgressNotification,
  updateTextNotification,
} from "./BackgroundTasks";
import { addSensorImageData } from "../ReUpload/Utils";
import { startBackgroundProcess } from "./BackgroundTasks";

export const NOTIFICATION_OPTIONS_FAILURE = {
  taskTitle: "Loading..",
  taskDesc: "Restarting the process",
};

// Default Notification settings based on upload type
// VIDEO - max value for progress will be 100
// DOCUMENTS - max value may vary on photos captured
export const createDefaultNotificationConfig = (maxProgressBarValue) => {
  console.log(
    "[INFO] . BackgroundTasksUtils . createDefaultNotificationConfig() . ",
    " maxProgressBarValue: ",
    maxProgressBarValue
  );

  const defaultObj = {
    taskName: "Uploading Documents",
    taskTitle: "Uploading Documents",
    taskDesc: "We are uploading your documents",
    taskIcon: {
      name: "ic_launcher",
      type: "mipmap",
    },
    color: "#DDDDDD",
    linkingURI: "yourSchemeHere://chat/jane",
    progressBar: {
      max: maxProgressBarValue,
      value: 0,
      indeterminate: false,
    },
  };

  console.log(
    "[INFO] . BackgroundTasksUtils . createDefaultNotificationConfig() . defaultObj - ",
    defaultObj
  );

  return defaultObj;
};

// create progress update object
// returns a object which updates the notification progress
export const createProgressUpdateObject = (dataObj) => {
  if (dataObj) {
    let updateObj = {};

    if (dataObj.type === "VIDEO") {
      updateObj = {
        taskTitle: `Uploading Video ${dataObj.uploadedPercentage}%`,
        progressBar: {
          max: 100,
          value: dataObj.uploadedPercentage,
          indeterminate: false,
        },
      };
    } else if (progressObject.type === "DOCUMENTS") {
      updateObj = {
        taskTitle: `Uploading Documents ${dataObj.uploadedImagesCount} / ${
          dataObj.totalImagesCount
        }`,
        progressBar: {
          max: dataObj.uploadedImagesCount,
          value: dataObj.totalImagesCount,
          indeterminate: false,
        },
      };
    }
    return updateObj;
  } else {
    // If no dataObj passed then show default failure object
    return NOTIFICATION_OPTIONS_FAILURE;
  }
};

// updates the notification via texts
// returns a object which updates the notification
export const createTextUpdateObject = (title, description) => {
  if (title && description) {
    return {
      taskTitle: title,
      taskDesc: description,
      progressBar: null,
    };
  }
  return NOTIFICATION_OPTIONS_FAILURE;
};

export const showThankYouAfterTimeout = (res) => {
  setTimeout(() => {
    res();
  }, 10000);
};

// Updates the notification contents defined in NotificationsConfig.js file
// Based on the conditions
export const updateNotificationContent = (config, progressBarConfig = null) => {
  const newConfig = {
    ...config,
    progressBar: {
      ...progressBarConfig,
    },
  };
  if (progressBarConfig !== null) {
    updateProgressNotification(newConfig);
  } else {
    updateTextNotification(newConfig);
  }
};
