import PhotosActionTypes from "./photos.types";

const INITIAL_STATE = {
  photos: [],
  uploadInProgress: true,
  uploadError: null,
  jobid: null,
  progress: {}
};

const photosReducer = (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case PhotosActionTypes.MEDIA_UPLOAD_START:
      return {
        ...state,
        photos: action.payload.photos,
        jobid: action.payload.jobid,
        uploadInProgress: true,
        uploadError: null,
        progress: {}
      };
    case PhotosActionTypes.MEDIA_UPLOAD_FAILURE:
      return {
        ...state,
        uploadInProgress: false,
        uploadError: action.payload,
      };
    case PhotosActionTypes.MEDIA_UPLOAD_PROGRESS_UPDATE_ONE:
      return {
        ...state,
        progress: { ...state.progress, [action.payload.assetId]: { ...state.progress[action.payload.assetId], ...action.payload } }
      };
    case PhotosActionTypes.MEDIA_UPLOAD_SUCCESS_ONE:
      return {
        ...state,
        progress: { ...state.progress, [action.payload.assetId]: { ...state.progress[action.payload.assetId], progress: 100, status: 'completed', endTime: new Date() } }
      };
    case PhotosActionTypes.MEDIA_UPLOAD_PROGRESS_UPDATE_BULK:
      return {
        ...state,
        progress: { Upload: action.payload }
      };
    case PhotosActionTypes.MEDIA_UPLOAD_COMPLETED:
      return {
        ...state,
        uploadInProgress: false,
        uploadError: null,
        photos: [],
        progress: {}
      };
    case PhotosActionTypes.CLEAR_UPLOAD_ERROR:
      return {
        ...state,
        photos: [], progress: {},
        uploadError: null,
      };
    default:
      return state;
  }
};

export default photosReducer;
