import { createReducer, on } from '@ngrx/store';
import { initialState } from './state';
import {
  selectedRoom,
  selectedMassDetail,
  massTalkDownloadFailure,
  massTalkDownload,
  massTalkDownloadSuccess,
  clearSelectedRoom,
  selectedRightDrawer,
  clearRightDrawer,
  toggleLeftSideDrawer,
  toggleLeftSideDrawerIndicator
} from './actions';

import * as AuthenticationStore from '@app/store/account/authentication';

export const reducer = createReducer(
  initialState,
  on(selectedRoom, (state, action) => {
    return {
      ...state,
      selectedRoom: action.roomSeq
    };
  }),

  on(clearSelectedRoom, (state, action) => {
    return {
      ...state,
      selectedRoom: null
    };
  }),

  on(selectedMassDetail, (state, action) => {
    return {
      ...state,
      selectedMassDetail: action.massEventSeq
    };
  }),

  on(massTalkDownload, (state, action) => {
    return {
      ...state,
      massDetailProcessing: true
    };
  }),
  on(massTalkDownloadSuccess, (state, action) => {
    return {
      ...state,
      massDetailProcessing: false
    };
  }),
  on(massTalkDownloadFailure, (state, action) => {
    return {
      ...state,
      massDetailProcessing: false
    };
  }),

  on(selectedRightDrawer, (state, action) => {
    return {
      ...state,
      selectedRightDrawer: action.req
    };
  }),
  on(toggleLeftSideDrawer, (state, action) => {
    return {
      ...state,
      leftSideDrawer: !state.leftSideDrawer
    };
  }),
  on(toggleLeftSideDrawerIndicator, (state, action) => {
    return {
      ...state,
      leftSideDrawerIndicator: action.show
    };
  }),
  on(clearRightDrawer, (state, action) => {
    return {
      ...state,
      selectedRightDrawer: null
    };
  }),

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