import {
  MAX_STATUS_TITLE_LENGTH,
  UPLOAD_STATUS_CANCELED,
  UPLOAD_STATUS_COMPLETE,
  UPLOAD_STATUS_FAILED,
  UPLOAD_STATUS_UPLOADING,
  UPLOAD_STATUS_WAIT,
} from '@/modules/uploaderNew/constants';

export const toMb = (sizeInBytes) => (sizeInBytes ? `${(sizeInBytes / (1024 * 1024)).toFixed(2)}MB` : '');
export const toPercentage = (progress) => progress * 100;
export const toPercentageWithSign = (progress) => `${Math.floor(toPercentage(progress))}% uploaded`;
export const toTitle = (title) =>
  title.length > MAX_STATUS_TITLE_LENGTH ? `${title.slice(0, MAX_STATUS_TITLE_LENGTH)}...` : title;

export const getHowManyFilesGroupedByStatuses = (q) => {
  const files = Object.values(q);

  const groupByStatuses = files.reduce((acc, value) => {
    acc[value.processedUploadStatus] = acc[value.processedUploadStatus]
      ? acc[value.processedUploadStatus].concat(value)
      : [value];
    return acc;
  }, {});

  const getFilesCountByStatus = (status) => groupByStatuses[status]?.length ?? 0;

  return {
    filesCountInCanceledState: getFilesCountByStatus(UPLOAD_STATUS_CANCELED),
    filesCountInCompletedState: getFilesCountByStatus(UPLOAD_STATUS_COMPLETE),
    filesCountInWaitState: getFilesCountByStatus(UPLOAD_STATUS_WAIT),
    filesCountInUploadingState: getFilesCountByStatus(UPLOAD_STATUS_UPLOADING),
    filesCountInFailedState: getFilesCountByStatus(UPLOAD_STATUS_FAILED),
  };
};

export const getFilesProcessingStatuses = (q) => {
  const files = Object.values(q);
  const {
    filesCountInCanceledState,
    filesCountInCompletedState,
    filesCountInWaitState,
    filesCountInUploadingState,
    filesCountInFailedState,
  } = getHowManyFilesGroupedByStatuses(q);

  return {
    hasCompletedAndNextToUpload: filesCountInCompletedState && (filesCountInWaitState || filesCountInUploadingState),
    hasAllCanceled: filesCountInCanceledState === files.length,
    hasAllCompleted: filesCountInCompletedState === files.length,
    hasCanceledOrFailed: filesCountInCanceledState || filesCountInFailedState,
    hasInProgress: filesCountInWaitState || filesCountInUploadingState,
    hasAllFailed: filesCountInFailedState === files.length,
  };
};

export const getHeaderText = (q) => {
  const files = Object.values(q);
  const {
    filesCountInCanceledState,
    filesCountInCompletedState,

    filesCountInWaitState,
    filesCountInUploadingState,
  } = getHowManyFilesGroupedByStatuses(q);
  const {
    hasAllCanceled,
    hasAllCompleted,

    hasInProgress,
    hasCanceledOrFailed,
  } = getFilesProcessingStatuses(q);

  if (hasInProgress) {
    const filesCount = filesCountInWaitState + filesCountInUploadingState;
    return `Uploading ${filesCount} video${filesCount > 1 ? 's' : ''}`;
  }

  if (!hasInProgress && hasAllCompleted) {
    return `${filesCountInCompletedState} video${filesCountInCompletedState > 1 ? 's' : ''} completed`;
  }

  if (!hasInProgress && hasAllCanceled) {
    return `${filesCountInCanceledState} video${filesCountInCanceledState > 1 ? 's' : ''} cancelled`;
  }

  if (!hasInProgress && hasCanceledOrFailed) {
    return `${filesCountInCompletedState}/${files.length - filesCountInCompletedState} complete`;
  }

  return '';
};
