/*
 *
 * Gallery reducer
 *
 */

import { fromJS } from 'immutable';
import * as types from './constants';

const initialState = fromJS({
  assetList: [],
  assetUploading: false,
  fetchingAllAssets: false,
});

function galleryReducer(state = initialState, action) {
  switch (action.type) {
    case types.GET_ALL_ASSETS_REQUEST:
      return state
        .set('fetchingAllAssets', true)
        .set('fetchAllAssetSuccess', false);
    case types.GET_ALL_ASSETS_SUCCESS:
      if (action.isReset) {
        return state
          .set('assetList', fromJS(action.data.assets))
          .set('fetchingAllAssets', false)
          .set('fetchAllAssetSuccess', true);
      }
      return state
        .set('assetList', fromJS(state.get('assetList').toJS().concat(action.data.assets)))
        .set('fetchingAllAssets', false)
        .set('fetchAllAssetSuccess', true);
    case types.GET_ALL_ASSETS_FAILURE:
      return state
      .set('fetchingAllAssets', false)
      .set('fetchAllAssetSuccess', false);
    case types.UPLOAD_ASSET_REQUEST:
      return state
        .set('uploadAssetSuccess', false)
        .set('assetUploading', true);
    case types.UPLOAD_ASSET_SUCCESS:
      return state
      .set('uploadAssetSuccess', (action.statusCode !== undefined && action.statusCode !== '' && action.statusCode < 300))
      .set('assetUploading', false)
      .set('uploadedAssetData', action.data);
    case types.UPLOAD_ASSET_FAILURE:
      return state
        .set('uploadAssetSuccess', false)
        .set('assetUploading', false);
    case types.DELETE_ASSET_REQUEST:
      return state
        .set('deletingAsset', true)
        .set('assetDeleteError', false).set("itemDeleted", false);
    case types.DELETE_ASSET_SUCCESS:
      return state
        .set('deletingAsset', false).set("itemDeleted", true);
    case types.DELETE_ASSET_FAILURE:
      return state
        .set('deletingAsset', false)
        .set('assetDeleteError', true).set("itemDeleted", false);
    case types.GET_USER_LIST_REQUEST:
      return state
        .set('fetchingUserList', true)
        .set('userList', fromJS({}))
        .set('userListFetchError', false);
    case types.GET_USER_LIST_SUCCESS:
      return state
        .set('fetchingUserList', false)
        .set('userList', fromJS(action.data))
        .set('userListFetchError', false);
    case types.GET_USER_LIST_FAILURE:
      return state
        .set('fetchingUserList', false)
        .set('userList', fromJS({}))
        .set('userListFetchError', true);
    default:
      return state;
  }
}

export default galleryReducer;
