import { createReducer, on } from '@ngrx/store';
import {
  initialState,
  adapterReceiveList,
  adapterSendList,
  adapterReservationList,
  adapterSearchList
} from './state';
import * as AuthenticationStore from '@app/store/account/authentication';
import {
  retrieveMessageSuccess,
  retrieveMessage,
  searchMessage,
  searchMessageSuccess,
  retrieveUnreadCountSuccess,
  deleteMessageSuccess,
  cancelReservationMessageSuccess,
  detailMessageSuccess,
  detailMessageClear,
  detailMessage,
  clearSearchMessage,
  clearNewFlagReceiveMessage
} from './actions';
import { MessageType, MessageList } from '@ucap-webmessenger/api-message';

export const reducer = createReducer(
  initialState,

  on(retrieveUnreadCountSuccess, (state, action) => {
    return {
      ...state,
      unReadMessageCount: action.count
    };
  }),

  on(retrieveMessage, (state, action) => {
    switch (action.messageType) {
      case MessageType.Receive: {
        return {
          ...state,
          receiveListProcessing: true
        };
      }
      case MessageType.Send: {
        return {
          ...state,
          sendListProcessing: true
        };
      }
      case MessageType.Reservation: {
        return {
          ...state,
          reservationListProcessing: true
        };
      }
    }
  }),
  on(retrieveMessageSuccess, (state, action) => {
    switch (action.messageType) {
      case MessageType.Receive: {
        return {
          ...state,
          receiveList: adapterReceiveList.upsertMany(action.res.messageList, {
            ...state.receiveList
          }),
          receiveListProcessing: false
        };
      }
      case MessageType.Send: {
        return {
          ...state,
          sendList: adapterSendList.upsertMany(action.res.messageList, {
            ...state.sendList
          }),
          sendListProcessing: false
        };
      }
      case MessageType.Reservation: {
        return {
          ...state,
          reservationList: adapterReservationList.upsertMany(
            action.res.messageList,
            {
              ...state.reservationList
            }
          ),
          reservationListProcessing: false
        };
      }
    }
  }),

  on(searchMessage, (state, action) => {
    return {
      ...state,
      searchList: adapterSearchList.removeAll(state.searchList),
      searchListProcessing: true
    };
  }),
  on(searchMessageSuccess, (state, action) => {
    return {
      ...state,
      searchList: adapterSearchList.upsertMany(action.res.messageList, {
        ...state.searchList
      }),
      searchListProcessing: false
    };
  }),

  on(deleteMessageSuccess, (state, action) => {
    if (action.messageType === MessageType.Receive) {
      return {
        ...state,
        receiveList: adapterReceiveList.removeMany(
          action.msgList.map(item => item.msgId),
          {
            ...state.receiveList
          }
        ),
        searchList: adapterSearchList.removeMany(
          action.msgList.map(item => item.msgId),
          {
            ...state.searchList
          }
        )
      };
    } else if (action.messageType === MessageType.Send) {
      return {
        ...state,
        sendList: adapterSendList.removeMany(
          action.msgList.map(item => item.msgId),
          {
            ...state.sendList
          }
        ),
        searchList: adapterSearchList.removeMany(
          action.msgList.map(item => item.msgId),
          {
            ...state.searchList
          }
        )
      };
    }
  }),

  on(clearSearchMessage, state => {
    return {
      ...state,
      searchList: adapterSearchList.removeAll(state.searchList)
    };
  }),

  on(clearNewFlagReceiveMessage, (state, action) => {
    const msgId = action.msgId;
    const msgList: MessageList = {
      ...state.receiveList.entities[msgId],
      readYn: true
    };

    return {
      ...state,
      receiveList: adapterReceiveList.updateOne(
        { id: msgId, changes: msgList },
        { ...state.receiveList }
      )
    };
  }),

  on(cancelReservationMessageSuccess, (state, action) => {
    return {
      ...state,
      reservationList: adapterReservationList.removeOne(action.msgId, {
        ...state.reservationList
      }),
      searchList: adapterSearchList.removeOne(action.msgId, {
        ...state.searchList
      })
    };
  }),

  on(detailMessageSuccess, (state, action) => {
    return {
      ...state,
      detailMessageInfo: action.res
    };
  }),

  on(detailMessage, (state, action) => {
    return {
      ...state,
      detailMessageInfo: null
    };
  }),
  on(detailMessageClear, (state, action) => {
    return {
      ...state,
      detailMessageInfo: null
    };
  }),

  on(AuthenticationStore.logoutInitialize, (state, action) => {
    return {
      ...initialState
    };
  })
);
