
import { NotificationDetailLogTypes } from './notification-detail.action.type'

const INITIAL_STATE = 
{
    inProgress: false,
    result: [],
    errorMessages: null,
    submissionInProgress: false,
    submissionResult: false,
    updateIndex: -1,
    deletionRequest: null,
    recordCount: 0  
}

const remoteNotificationDetailReducer = (currentState = INITIAL_STATE, action) => {
    switch(action.type){
        case NotificationDetailLogTypes.NOTIFICATION_DETAIL_GET_MESSAGE_LIST_START:
            return {
                ...currentState,
                inProgress: true
            }
        case NotificationDetailLogTypes.NOTIFICATION_DETAIL_GET_MESSAGE_LIST_SUCCESS:
            {
                const {count, records} = action.payload
                return {
                    ...currentState,
                    inProgress: false,
                    result:  records,
                    recordCount: count
                }
            }
            case NotificationDetailLogTypes.NOTIFICATION_DETAIL_GET_MESSAGE_LIST_FAILURE:
                const {message} = action.payload
                return {
                    ...currentState,
                    inProgress: false,
                    errorMessages: message,
                    result: []
                }
            
            case NotificationDetailLogTypes.NOTIFICATION_DETAIL_POST_MESSAGE_START:{
                const {indexOfRecord, deletionRequest} = action.payload
                return {
                    ...currentState,
                    submissionInProgress: true,
                    submissionResult: false,
                    updateIndex: indexOfRecord,
                    deletionRequest: deletionRequest,
                    errorMessages: null
                }
            }
            case NotificationDetailLogTypes.NOTIFICATION_DETAIL_POST_MESSAGE_SUCCESS:{
                const updatedResult = [...currentState.result]
                var newRecordCount = currentState.recordCount
                if(currentState.deletionRequest){
                    updatedResult.splice(currentState.updateIndex,1)
                    newRecordCount = newRecordCount - 1

                }else{
                    const {IsRead } = updatedResult[currentState.updateIndex]
                    updatedResult[currentState.updateIndex].IsRead = !IsRead

                }

                return {
                    ...currentState,
                    submissionInProgress: false,
                    submissionResult: action.payload,
                    updateIndex: -1,
                    deletionRequest: null,
                    result: updatedResult,
                    errorMessages: null,
                    recordCount: newRecordCount
                }
            }
            case NotificationDetailLogTypes.NOTIFICATION_DETAIL_POST_MESSAGE_FAILURE:
                return {
                    ...currentState,
                    submissionInProgress: false,
                    errorMessages: action.payload,
                    submissionResult: false,
                    updateIndex: -1,
                    deletionRequest: null
            
                }
            case NotificationDetailLogTypes.NOTIFICATION_DETAIL_GET_MESSAGE_LIST_RESET_STATE:
                return INITIAL_STATE
            
        default: 
            return currentState
    }
}

export default remoteNotificationDetailReducer