import {
    APPOINTMENT_FETCH,
    APPOINTMENT_CREATE,
    APPOINTMENT_UPDATE,
    APPOINTMENT_DELETE
} from './../actions/types'
import realm from './../Database/'
import moment from 'moment'
import { ToastAndroid } from 'react-native'

export const appointmentCreate = ({ id, doctorName, date, place, time }) => {
    return (dispatch) => {
        let appointment = { id, doctorName, date, place, time }
        realm.write(() => {
            realm.create('Appointments', {
                id: id,
                doctorName: doctorName,
                date: date,
                place: place,
                timeStamp: moment().toDate()
            })
        })
        let appointments = realm.objects("Appointments")
        dispatch({ type: APPOINTMENT_CREATE, payload: appointments })
    }
}

export const appointmentUpdate = () => {
    return (dispatch) => {

    }
}

export const appointmentFetch = () => {
    return (dispatch) => {
        let appointments = realm.objects("Appointments")
        dispatch({ type: APPOINTMENT_FETCH, payload: appointments })
    }
}

export const appointmentDelete = (id) => {
    return (dispatch) => {
        let item = realm.objects("Appointments").filtered('id == ' + '"' + id + '"')
        realm.write(() => {
            realm.delete(item)
        })
        let appointments = realm.objects("Appointments")
        dispatch({ type: APPOINTMENT_DELETE, payload: appointments })
    }
}
