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, medName, reminderTime, meal, period, dose }) => {
    return (dispatch) => {
        realm.write(() => {
            realm.create('Reminder', {
                id: id,
                medName: medName,
                reminderTime: reminderTime,
                meal: meal,
                period: period,
                dose: dose,
                timeStamp: moment().toDate()
            })
        })
        let reminders = realm.objects("Reminder")
        dispatch({ type: REMINDER_CREATE, payload: reminders })
    }
}

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

    }
}

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

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