import { RootState } from "app/store";
import { convertUtcToLocal } from "common/convertUtcToLocal";
import { ceil, floor } from "lodash";
import { BACKUP_POPUP_LIST_SIZE } from "./constants";

// ミリ秒の数値をhh:mm:ss形式に変換する
const formatMillisecondsToHHMMSS = (milliseconds: number): string => {
  const seconds = Math.floor(milliseconds / 1000);
  if (seconds < 0) {
    return "00:00:00";
  }

  const hours = Math.floor(seconds / 3600);
  const minutes = Math.floor((seconds % 3600) / 60);
  const remainingSeconds = Math.floor(seconds % 60);

  // "0x"となるように0埋めする
  const formattedHours = String(hours).padStart(2, "0");
  const formattedMinutes = String(minutes).padStart(2, "0");
  const formattedSeconds = String(remainingSeconds).padStart(2, "0");

  return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
};

export const selectTasks = (state: RootState) => {
  const { tasks } = state.dictation.domain;

  const tasksWithLocalDate = tasks.map((task) => ({
    ...task,

    // UTCからローカルタイムゾーンに変換して詰めなおす
    audioCreatedDate: convertUtcToLocal(task.audioCreatedDate),
    audioFinishedDate: convertUtcToLocal(task.audioFinishedDate),
    audioUploadedDate: convertUtcToLocal(task.audioUploadedDate),
    transcriptionStartedDate:
      task.transcriptionStartedDate &&
      convertUtcToLocal(task.transcriptionStartedDate),
    transcriptionFinishedDate:
      task.transcriptionFinishedDate &&
      convertUtcToLocal(task.transcriptionFinishedDate),

    // ミリ秒からhh:mm:ss形式に変換して詰めなおす
    audioDuration: formatMillisecondsToHHMMSS(parseInt(task.audioDuration, 10)),
  }));

  return tasksWithLocalDate;
};
export const selectTotal = (state: RootState) => state.dictation.domain.total;

export const seletctLimit = (state: RootState) => state.dictation.domain.limit;

export const selectOffset = (state: RootState) => state.dictation.domain.offset;

export const selectTotalPage = (state: RootState) => {
  const { limit, total } = state.dictation.domain;
  const page = ceil(total / limit);
  return page;
};

export const selectCurrentPage = (state: RootState) => {
  const { limit, offset } = state.dictation.domain;
  const page = floor(offset / limit) + 1;
  return page;
};

export const selectDisplayInfo = (state: RootState) =>
  state.dictation.apps.displayInfo;

export const selectDirection = (state: RootState) =>
  state.dictation.apps.direction;

export const selectParamName = (state: RootState) =>
  state.dictation.apps.paramName;

export const selectSelectedTask = (state: RootState) =>
  state.dictation.apps.selectedTask;

export const selectSelectedFileTask = (state: RootState) =>
  state.dictation.apps.selectedFileTask;

export const selectSelectedTranscriptionists = (state: RootState) =>
  state.dictation.apps.assignee.selected;

export const selectPoolTranscriptionists = (state: RootState) =>
  state.dictation.apps.assignee.pool;

export const selectIsLoading = (state: RootState) =>
  state.dictation.apps.isLoading;

export const selectBackupTasks = (state: RootState) =>
  state.dictation.domain.backup.tasks;

export const selectTotalBackupPage = (state: RootState) => {
  const { total } = state.dictation.domain.backup;
  const page = ceil(total / BACKUP_POPUP_LIST_SIZE);
  return page;
};

export const selectCurrentBackupPage = (state: RootState) => {
  const { offset } = state.dictation.domain.backup;
  const page = floor(offset / BACKUP_POPUP_LIST_SIZE) + 1;
  return page;
};

export const selectBackupTotal = (state: RootState) =>
  state.dictation.domain.backup.total;

export const selectBackupAllChecked = (state: RootState) => {
  const { tasks } = state.dictation.domain.backup;
  return tasks.every((task) => task.checked);
};

export const selectIsBackupListLoading = (state: RootState) =>
  state.dictation.apps.isBackupListLoading;

export const selectIsDownloading = (state: RootState) =>
  state.dictation.apps.isDownloading;
