import {
    REMINDER_FETCH,
    REMINDER_CREATE,
    REMINDER_UPDATE,
    REMINDER_DELETE
} from './../actions/types'
import realm from './../Database/'
import moment from 'moment'
import { ToastAndroid } from 'react-native'

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

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

    }
}

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

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