import {DailyLogActionTypes } from './daily-logs.action.types'

const INITIAL_STATE = 
{
    inProgress: false,
    result: [],
    saveResult: null,
    apiResponseStatus: -1,
    errorMessages: null,
    submissionInProgress: false,
    recordCount: 0,
    reportFetchInProgress: false,
    reportResult: undefined
}

const dailyLogReducer = (currentState = INITIAL_STATE, action) => {
    switch(action.type){
        case DailyLogActionTypes.GET_USER_DAILY_LOGS_START:
            return{
                ...currentState,
                inProgress: true,
                saveResult: null

            }
        case DailyLogActionTypes.GET_USER_DAILY_LOGS_SUCCESS:
        {    
            const { count, records } = action.payload
            return{
                ...currentState,
                result: records,
                errorMessages: null,
                inProgress: false,
                recordCount: count
            }
        }
        case DailyLogActionTypes.GET_USER_DAILY_LOGS_FAILURE:{
            const { message } = action.payload
            return{
                ...currentState,
                errorMessages: message,
                result:[],
                inProgress: false
            }
        }
        
        case DailyLogActionTypes.POST_USER_DAILY_LOGS_START:
            return{
                ...currentState,
                submissionInProgress: true
            }
        
        case DailyLogActionTypes.POST_USER_DAILY_LOGS_SUCCESS:
            return{
                ...currentState,
                errorMessages: null,
                saveResult: action.payload,
                submissionInProgress: false
            }
        
        case DailyLogActionTypes.POST_USER_DAILY_LOGS_FAILURE:{
            const { message } = action.payload
            return{
                ...currentState,
                errorMessages: message,
                saveResult: null,
                submissionInProgress: false
            }
        }
        
        case DailyLogActionTypes.POST_USER_DAILY_LOGS_CLEAR_ERROR:
            return {
                ...currentState,
                submissionInProgress: false,
                errorMessages: null
            }
        case DailyLogActionTypes.GET_DAILY_LOG_REPORT_START:{
            return {
                ...currentState,
                reportFetchInProgress: true,
                errorMessages: null
            }
        }
        case DailyLogActionTypes.GET_DAILY_LOG_REPORT_SUCCESS:{
            const { /*count,*/ records } = action.payload
            return {
                ...currentState,
                reportFetchInProgress: false,
                errorMessages: null,
                reportResult: records
            }
        }
        case DailyLogActionTypes.GET_DAILY_LOG_REPORT_FAILURE:{
            return {
                ...currentState,
                reportFetchInProgress: false,
                reportResult: undefined,
                errorMessages: action.payload.message
            }
        }

        case DailyLogActionTypes.DAILY_LOG_RESET_STATE:
            return INITIAL_STATE

        default: {
            return currentState
        }
    }
}

export default dailyLogReducer