import { withStore } from '../../../services/AxiosInstance'
import buildQuery from 'odata-query'
import {METADATA_END_POINTS} from '../../helpers/action-urls'
import {MettaSvr_Common} from "../../helpers/action-enums"
import { DashboardActionTypes, ResultTypes } from "./dashboard-detail.action.types"

const getDashboardContentStart = () => ({
    type: DashboardActionTypes.GET_DASHBOARD_START
})


const getDashboardCriticalityTrendContentSuccess = response => ({
    type: DashboardActionTypes.GET_DASHBOARD_CRITICALITY_TREND_SUCCESS,
    payload: response
})
const getDashboardContentSuccess = response => ({
    type: DashboardActionTypes.GET_DASHBOARD_SUCCESS,
    payload: response
})

const getDashboardContentFailure = err => ({
    type: DashboardActionTypes.GET_DASHBOARD_FAILURE,
    payload: err
})

export const clearGetDashboardContentState = () => ({
    type: DashboardActionTypes.DASHBOARD_RESET_STATE
})

export const clearDashboardContentErrorState = () => ({
    type: DashboardActionTypes.DASHBOARD_STATUS_CLEAR_ERROR_STATE
})

const fetchDashboardMandatoryContentAsync = () => {
    return async  (dispatch, getState) => {
        const { DASHBOARD_DISEASE_WISE_PATIENTS, DASHBOARD_METRICS } = METADATA_END_POINTS.common


        const {user} = getState()
        const odataQuery = {
            
        }
        
        const query = buildQuery( odataQuery )
        const { ValueKey } = MettaSvr_Common.ODATA_Keys
        const disease_count_response = await withStore(user.currentUser.accessToken, dispatch).getService.get(`${DASHBOARD_DISEASE_WISE_PATIENTS}${query}`)              
        const dashboard_metrics_response = await withStore(user.currentUser.accessToken, dispatch).getService.get(`${DASHBOARD_METRICS}${query}`)              
        
        return {
            [ResultTypes.DiseaseWisePatientCount]: disease_count_response.data[ValueKey], 
            [ResultTypes.DashboardMertrics]: dashboard_metrics_response.data[ValueKey][0]    
        }
    }
}

export const fetchDashboardContentForEducatorAsync = () => {
    return async  (dispatch) => {
        dispatch(getDashboardContentStart())
        try{
            const { ValueKey } = MettaSvr_Common.ODATA_Keys
            const result = await dispatch(fetchDashboardMandatoryContentAsync())
            
            dispatch(getDashboardContentSuccess(result))

        }catch(ex){            
            console.log("Error is: ", !!ex.response ? ex.response.data.errors : 'none')
            dispatch(getDashboardContentFailure({message: ex.response.data.message ?? ex.response.data.title, apiResponseStatus: -1}))

        }
    }
}

export const fetchDashboardContentForDoctorOfPatientTrendAsync = ({PatientId,FromDate,ToDate}) => {
    return async  (dispatch, getState) => {
        dispatch(getDashboardContentStart())
        try {
            const {user} = getState()
            const { ValueKey } = MettaSvr_Common.ODATA_Keys

            const {DASHBOARD_PATIENT_CRITICALITY_TREND} = METADATA_END_POINTS.common
            const pat_trend_response = await withStore(user.currentUser.accessToken, dispatch).getService.get(DASHBOARD_PATIENT_CRITICALITY_TREND(PatientId, FromDate, ToDate))              
            
            dispatch(getDashboardCriticalityTrendContentSuccess({                
                [ResultTypes.PatientCrticalityTrend]: pat_trend_response.data[ValueKey],                    
            })
        )
        } catch (error) {
            
        }
    }
}

export const fetchDashboardContentForDoctorAsync = () => {
    return async  (dispatch, getState) => {
        dispatch(getDashboardContentStart())
        try{
            const {  DASHBOARD_EDUCATOR_WISE_PATIENTS } = METADATA_END_POINTS.common


            const {user} = getState()
            const odataQuery = {
                
            }
            
            const query = buildQuery( odataQuery )
            const { ValueKey } = MettaSvr_Common.ODATA_Keys
            const result = await dispatch(fetchDashboardMandatoryContentAsync())
            const edu_count_response = await withStore(user.currentUser.accessToken, dispatch).getService.get(`${DASHBOARD_EDUCATOR_WISE_PATIENTS}${query}`)              
            
            dispatch(getDashboardContentSuccess({
                    ...result,
                    [ResultTypes.EducatorPatientCount]: edu_count_response.data[ValueKey],                    
                })
            )
            
        }catch(ex){            
            console.log("Error is: ", !!ex.response ? ex.response.data.errors : 'none')
            dispatch(getDashboardContentFailure({message: ex.response.data.message ?? ex.response.data.title, apiResponseStatus: -1}))

        }
    }
}