import { PatientRegistrationActionTypes } from './patient.action.types'


const INITIAL_STATE = 
{
    inProgress: false,
    paymentInProgress: false,
    result: null,
    apiResponseStatus: -1,
    errorMessages: null,
    currentPage: 0,
    subsCodeValidated: false,
    subscCodeValidationInProgress: false,
    paymentRenewalFinishedSuccessfully: false,
}


const patientRegistrationReducer = (currentState = INITIAL_STATE, action) => {

    switch(action.type){
        case PatientRegistrationActionTypes.RESET_STATE:
            return INITIAL_STATE
        case PatientRegistrationActionTypes.POST_PATIENT_START:
            return {
                ...currentState,
                inProgress: true
            }
    
        case PatientRegistrationActionTypes.POST_PATIENT_SUCCESS:
            return {
                ...currentState,
                errorMessages: null,
                result: action.payload,
                inProgress: false
            }

        case PatientRegistrationActionTypes.POST_PATIENT_FAILURE:
         {
            const { message, apiResponseStatus } = action.payload

            return {
                ...currentState,
                result: null,
                errorMessages: message,
                apiResponseStatus,                    
                inProgress: false,
                paymentInProgress: false
            }
        }
        case PatientRegistrationActionTypes.POST_PATIENT_PAGE_CHANGE:
            return {
                ...currentState,
                currentPage: action.payload
            }

        case PatientRegistrationActionTypes.POST_PATIENT_CLEAR_STATE:
            return {
                ...currentState,
                errorMessages: null,
                paymentRenewalFinishedSuccessfully: false,
                result: null,

            }
        case PatientRegistrationActionTypes.POST_PATIENT_RENEW_SUBSCRIPTION_SUCCESS:
            return{
                ...currentState,
                result: action.payload,
                inProgress: false
            }
        case PatientRegistrationActionTypes.POST_PATIENT_RENEW_SUBSCRIPTION_START:
            return{
                ...currentState,
                inProgress: true
            }

        case PatientRegistrationActionTypes.POST_PATIENT_PAYMEMT_START:
            return {
                ...currentState,
                errorMessages: null,
                paymentInProgress: true
            }
            // should be the state once chek payment (verified) with backend
        case PatientRegistrationActionTypes.POST_PATIENT_PAYMEMT_SUCCESS:
                return {
                    ...currentState,
                    errorMessages: null,
                    paymentInProgress: false,
                }
        case PatientRegistrationActionTypes.POST_PATIENT_REGISTRATION_PAYMEMT_COMPLETE:
                return {
                    ...currentState,
                    errorMessages: null,
                    paymentInProgress: false,
                    result: null,
                    currentPage: currentState.currentPage + 1,
                    paymentRenewalFinishedSuccessfully: true
                }
        case PatientRegistrationActionTypes.CANCEL_PATIENT_PAYMEMT:{
                // const {code, description, reason } = action.payload
                return {
                    ...currentState,
                    paymentInProgress: false,
                }
        }
        case PatientRegistrationActionTypes.POST_PATIENT_PAYMEMT_FAILURE:{
                // const {code, description, reason } = action.payload
                const { description } = action.payload
                return {
                    ...currentState,
                    errorMessages: description,
                    paymentInProgress: false,
                    paymentRenewalFinishedSuccessfully: false,
                    result: null
                }
        }
        /* Subscription code state changes ... */
        case PatientRegistrationActionTypes.POST_PATIENT_SUBSCODE_VALIDATION_START:
            return{
                ...currentState,
                subscCodeValidationInProgress: true,
                subsCodeValidated: false
            }
        case PatientRegistrationActionTypes.POST_PATIENT_SUBSCODE_VALIDATION_SUCCESS:
            return{
                ...currentState,
                subscCodeValidationInProgress: false,
                subsCodeValidated: true
            }
        case PatientRegistrationActionTypes.POST_PATIENT_SUBSCODE_VALIDATION_FAILURE:{
            const { message } = action.payload
            return{
                ...currentState,
                subscCodeValidationInProgress: false,
                errorMessages: message
            }
        }
        default:
            return currentState
    }
}

export default patientRegistrationReducer