import { createReducer, on } from '@ngrx/store';
import {
  initialState,
  adapterInfoList,
  adapterFileInfoList,
  adapterFileInfoCheckList
} from './state';
import {
  infoSuccess,
  appendInfoList,
  info,
  infoFailure,
  recallInfoList,
  delInfoList,
  infoMoreSuccess,
  fileInfoSuccess,
  infoForSearch,
  infoForSearchEnd,
  infoAll
} from './actions';
import * as AuthenticationStore from '@app/store/account/authentication';
import * as ChatStore from '@app/store/messenger/chat';
import { Info, EventType, EventJson } from '@ucap-webmessenger/protocol-event';

export const reducer = createReducer(
  initialState,
  on(info, (state, action) => {
    return {
      ...state,
      infoListProcessing: true
    };
  }),
  on(infoForSearch, (state, action) => {
    return {
      ...state,
      infoListProcessing: true,
      infoSearchListProcessing: true
    };
  }),
  on(infoForSearchEnd, (state, action) => {
    return {
      ...state,
      infoSearchListProcessing: false
    };
  }),
  on(infoAll, (state, action) => {
    return {
      ...state,
      infoListProcessing: true,
      infoSearchListProcessing: true
    };
  }),

  on(infoSuccess, (state, action) => {
    return {
      ...state,
      infoList: adapterInfoList.addAll(action.infoList, {
        ...state.infoList
      }),
      infoStatus: action.res,
      infoListProcessing: false,
      remainInfo: action.remainInfo
    };
  }),
  on(infoMoreSuccess, (state, action) => {
    return {
      ...state,
      infoList: adapterInfoList.upsertMany(action.infoList, {
        ...state.infoList
      }),
      infoStatus: action.res,
      infoListProcessing: false,
      remainInfo: action.remainInfo
    };
  }),
  on(infoFailure, (state, action) => {
    return {
      ...state,
      infoListProcessing: false
    };
  }),

  on(fileInfoSuccess, (state, action) => {
    return {
      ...state,
      fileInfoList: adapterFileInfoList.addAll(action.fileInfoList, {
        ...state.fileInfoList
      }),
      fileInfoCheckList: adapterFileInfoCheckList.addAll(
        action.fileInfoCheckList,
        {
          ...state.fileInfoCheckList
        }
      ),
      fileInfoListProcessing: false,
      fileInfoSyncDate: new Date().toString()
    };
  }),

  on(appendInfoList, (state, action) => {
    const eventinfo = action.info;

    const statusEventInfo: Info<EventJson> = {
      ...state.infoList.entities[eventinfo.seq],
      type: eventinfo.type,
      senderSeq: eventinfo.senderSeq,
      sendDate: eventinfo.sendDate,
      sentMessage: eventinfo.sentMessage,
      receiverCount: eventinfo.receiverCount
    };

    return {
      ...state,
      infoList: adapterInfoList.upsertOne(eventinfo, {
        ...state.infoList
      })
    };
  }),

  on(recallInfoList, (state, action) => {
    const eventSeq = action.eventSeq;

    const statusEventInfo: Info<EventJson> = {
      ...state.infoList.entities[eventSeq],
      type: EventType.RecalledMessage,
      sentMessage: '회수된 메시지'
    };

    return {
      ...state,
      infoList: adapterInfoList.updateOne(
        { id: eventSeq, changes: statusEventInfo },
        { ...state.infoList }
      )
    };
  }),

  on(delInfoList, (state, action) => {
    return {
      ...state,
      infoList: adapterInfoList.removeMany(action.eventSeqs, {
        ...state.infoList
      })
    };
  }),

  on(ChatStore.selectedRoom, (state, action) => {
    return {
      ...initialState
    };
  }),
  on(ChatStore.clearSelectedRoom, (state, action) => {
    return {
      ...initialState
    };
  }),

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