import { StoreAction } from 'store/utils/actions';
import produce from 'immer';
import _ from 'lodash';
import { handleRequest, handleResponse, handleAddResponse } from 'store/utils/reducers';
import auth from 'store/auth';
import actions from './actions';

interface MessageState {
  rooms: {} | null;
  detail: {} | null;
  messages: {} | null;
}

export const initialState: MessageState = {
  rooms: null,
  detail: null,
  messages: null,
};

const reducer = (state: MessageState = initialState, action: StoreAction): MessageState =>
  produce(
    state,
    (draft): MessageState => {
      let index;
      switch (action.type) {
        case actions.getRooms.request.constant:
          action.payload.limit = 100;
          handleRequest(draft, action, 'rooms');
          break;
        case actions.getRooms.result.constant:
          handleResponse(draft, action, 'rooms');
          break;
        case actions.getRoomsMore.result.constant:
          handleAddResponse(draft, action, 'rooms');
          break;
        case actions.getRoomInfo.request.constant:
          handleRequest(draft, action, 'detail');
          break;
        case actions.getRoomInfo.result.constant:
          handleResponse(draft, action, 'detail');
          break;
        case actions.getRoomMessage.request.constant:
          handleRequest(draft, action, 'messages');
          break;
        case actions.getRoomMessage.result.constant:
          if (state.messages.data) {
            draft.messages.data = state.messages.data.concat(action.payload);
          } else {
            handleResponse(draft, action, 'messages');
          }
          draft.messages.hasMore = action.payload.length === 50;
          break;
        case actions.resetMessage.constant:
          draft.messages = null;
          break;
        case actions.createMessage.result.constant:
          if (action.payload && action.payload.message) {
            draft.messages.data.unshift(action.payload);
          }
          break;
        case actions.addPeopleRoom.result.constant:
          handleResponse(draft, action, 'detail');
          break;
        case actions.removePeopleRoom.result.constant:
          handleResponse(draft, action, 'detail');
          break;
        case actions.roomSeen.result.constant:
          if (action.payload) {
            if (state.rooms.data) {
              index = _.findIndex(state.rooms.data, e => e._id === action.payload._id);
              if (index !== -1) {
                draft.rooms.data[index] = { ...state.rooms.data[index], ...action.payload };
              }
            }
            if (state.detail.data) {
              draft.detail.data = { ...state.detail.data, ...action.payload };
            }
          }
          break;
        case actions.createRoom.result.constant:
          if (action.payload) draft.rooms.data.unshift(action.payload);
          break;
        case auth.actions.logout.result.constant:
          draft.rooms = null;
          draft.detail = null;
          break;
        default:
      }
    },
  );

export default reducer;
