import { createSlice } from '@reduxjs/toolkit';

import { RUN_CONTEXT, TYPE_MANUAL } from '../../constants';

const initialState = {
  runContext: RUN_CONTEXT.IN_TAB,
  // ccFiles shape
  // { name?, file, lang, version, source, isFormItem?, toApproveAfterUpload?, original? }
  ccFiles: {
    [RUN_CONTEXT.IN_TAB]: [],
    [RUN_CONTEXT.IN_VIDEO_UPLOAD]: [],
  },
  languages: [],
  totalSegments: 0,
  autoTranslateLanguages: [],
  isShowAutoTranslateInfoDialog: false,
  checkCcFileStateMonitoringIsActive: false,
};

export const slice = createSlice({
  name: '@@COMMON/CC_FILES',
  initialState,

  reducers: {
    getCcTotalSegmentsAction: (state) => state,
    uploadStartAction: (state) => state,
    uploadProcessingAction: (state) => state,
    replaceUploadStartAction: (state) => state,
    replaceUploadProcessingAction: (state) => state,
    loadLanguagesAction: (state) => state,
    loadTranslateLanguagesAction: (state) => state,
    translateLanguagesProcessingAction: (state) => state,
    getCcFilesAction: (state) => state,
    addCcFileAction: (state) => state,
    deleteCcFileAction: (state) => state,
    setCcFilesAction: (state, action) => {
      state.ccFiles[state.runContext] = action.payload;
    },
    setCcTotalSegmentsAction: (state, action) => {
      state.totalSegments = action.payload || initialState.totalSegments;
    },
    setLanguagesAction: (state, action) => {
      state.languages = action.payload;
    },
    setTranslateLanguagesAction: (state, action) => {
      state.autoTranslateLanguages = action.payload;
    },
    appendUploadedFileAction: (state, action) => {
      const { name, file, fileObject = null } = action.payload;
      state.ccFiles[state.runContext] = [].concat(state.ccFiles[state.runContext], {
        name,
        file,
        lang: null,
        version: TYPE_MANUAL,
        isFormItem: true,
        toApproveAfterUpload: true,
        fileObject,
      });
    },
    replaceAppendUploadedFileAction: (state, action) => {
      const { name, file, ccFileForReplace } = action.payload;
      state.ccFiles[state.runContext] = state.ccFiles[state.runContext].map((cc) =>
        cc.file === ccFileForReplace.file
          ? {
              name,
              file,
              lang: null,
              version: TYPE_MANUAL,
              isFormItem: true,
              original: ccFileForReplace,
            }
          : cc,
      );
    },
    setRunContextAction: (state, action) => {
      state.runContext = action.payload;
    },
    resetCcFilesInVideoUploadAction: (state) => {
      state.ccFiles[RUN_CONTEXT.IN_VIDEO_UPLOAD] = [];
    },
    showAutoTranslateInfoDialogAction: (state, action) => {
      state.isShowAutoTranslateInfoDialog = action.payload;
    },
    clearAction: (state) => {
      state.ccFiles[state.runContext] = [];
      state.runContext = RUN_CONTEXT.IN_TAB;
    },
    checkCcFilesStateAction: (state, action) => {
      state.checkCcFileStateMonitoringIsActive = action.payload.isActive;
    },
  },
});

export const {
  addCcFileAction,
  appendUploadedFileAction,
  checkCcFilesStateAction,
  clearAction,
  deleteCcFileAction,
  getCcFilesAction,
  getCcTotalSegmentsAction,
  loadLanguagesAction,
  loadTranslateLanguagesAction,
  replaceAppendUploadedFileAction,
  replaceUploadProcessingAction,
  replaceUploadStartAction,
  resetCcFilesInVideoUploadAction,
  setCcFilesAction,
  setCcTotalSegmentsAction,
  setLanguagesAction,
  setRunContextAction,
  setTranslateLanguagesAction,
  showAutoTranslateInfoDialogAction,
  translateLanguagesProcessingAction,
  uploadProcessingAction,
  uploadStartAction,
} = slice.actions;

export default slice.reducer;
