package com.agilex.healthcare.schedulingmanager.translator;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import com.agilex.healthcare.schedulingmanager.domain.WaitListEntries;
import com.agilex.healthcare.schedulingmanager.domain.WaitListEntry;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

public class WaitListTranslator {
    public ArrayList<DBObject>  convertToDBObjects(WaitListEntries waitListEntries) {
        Iterator<?>                  listEntryIterator;
        ArrayList<DBObject>       returnArray = new ArrayList<DBObject>();

        listEntryIterator = waitListEntries.iterator();
        while(listEntryIterator.hasNext()) {
            WaitListEntry     entry;
            DBObject    dbObject;

            entry = (WaitListEntry)listEntryIterator.next();
            dbObject = new BasicDBObject();

            dbObject.put(WaitListEntry.ID_KEY, entry.getId());
            dbObject.put(WaitListEntry.DFN_KEY, entry.getDfn());
            dbObject.put(WaitListEntry.STOP_CODE_KEY, entry.getStopCode());
            dbObject.put(WaitListEntry.DATE_LAST_UPDATED_KEY, entry.getDateLastUpdated());
            dbObject.put(WaitListEntry.DESIRED_DATE_KEY, entry.getDesiredDate());
            dbObject.put(WaitListEntry.DAYS_ON_LIST_KEY, entry.getDaysOnList());
            dbObject.put(WaitListEntry.LAST_NAME_KEY, entry.getLastName());
            dbObject.put(WaitListEntry.SSN_KEY, entry.getSsn());
            dbObject.put(WaitListEntry.SERVICE_CONNECTED_KEY, entry.getServiceConnected());
            dbObject.put(WaitListEntry.CLINIC_NAME_KEY, entry.getClinicName());
            dbObject.put(WaitListEntry.ORIGINATING_DATE_KEY, entry.getOriginatingDate());
            dbObject.put(WaitListEntry.PATIENT_ZIP_KEY, entry.getPatientZip());
            dbObject.put(WaitListEntry.ENROLLEE_STATUS_KEY, entry.getEnrolleeStatus());
            dbObject.put(WaitListEntry.PRIORITY_KEY, entry.getPriority());
            dbObject.put(WaitListEntry.WAIT_LIST_TYPE_KEY, entry.getWaitListType());
            dbObject.put(WaitListEntry.HOME_PHONE_KEY, entry.getHomePhone());
            dbObject.put(WaitListEntry.CELL_PHONE_KEY, entry.getCellPhone());
            dbObject.put(WaitListEntry.FACILITY_ID_KEY, entry.getFacilityId());
            dbObject.put(WaitListEntry.MESSAGE_KEY, entry.getMessage());
            dbObject.put(WaitListEntry.ELIGIBILITY_CODE_INTERNAL_KEY, entry.getEligibilityCodeInternal());
            dbObject.put(WaitListEntry.ELIGIBILITY_CODE_EXTERNAL_KEY, entry.getEligibilityCodeExternal());
            dbObject.put(WaitListEntry.DATE_REMOVED_FROM_EWL_KEY, entry.getDateRemovedFromEwl());
            dbObject.put(WaitListEntry.CLINIC_ID_KEY, entry.getClinicId());
            dbObject.put(WaitListEntry.ACCESSIONNO_KEY, entry.getAccessionno());
            dbObject.put(WaitListEntry.PROVIDER_KEY, entry.getProvider());
            dbObject.put(WaitListEntry.COMMENT_KEY, entry.getComment());
            dbObject.put(WaitListEntry.RECALL_DATE_KEY, entry.getRecallDate());
            dbObject.put(WaitListEntry.FAST_NON_FASTING_KEY, entry.getFastNonFasting());
            dbObject.put(WaitListEntry.DATE_REMINDER_SENT_KEY, entry.getDateReminderSent());
            dbObject.put(WaitListEntry.APPOINTMENT_LENGTH_KEY, entry.getAppointmentLength());
            dbObject.put(WaitListEntry.USER_KEY, entry.getUser());
            dbObject.put(WaitListEntry.RECALL_DATE_1_KEY, entry.getRecallDate());
            dbObject.put(WaitListEntry.SECOND_PRINT_DATE_KEY, entry.getSecondPrintDate());
            dbObject.put(WaitListEntry.PATIENT_NAME_KEY, entry.getPatientName());
            dbObject.put(WaitListEntry.APPOINTMENT_REQUEST_DATE_KEY, entry.getAppointmentRequestDate());
            dbObject.put(WaitListEntry.EWL_IEN_KEY, entry.getEwlIen());

            returnArray.add(dbObject);
        }

        return returnArray;
    }

    public WaitListEntries  convertFromDBObjects(BasicDBList  listEntries) {
        Iterator<?>                  listEntryIterator;
        WaitListEntries       waitListToReturn = new WaitListEntries();

        listEntryIterator = listEntries.iterator();
        while(listEntryIterator.hasNext()) {
            WaitListEntry     entry;
            DBObject    dbObject;

            dbObject = (DBObject)listEntryIterator.next();
            entry = new WaitListEntry();

            entry.setDfn((String)dbObject.get(WaitListEntry.DFN_KEY));
            entry.setStopCode((String)dbObject.get(WaitListEntry.STOP_CODE_KEY));
            entry.setDateLastUpdated((Date)dbObject.get(WaitListEntry.DATE_LAST_UPDATED_KEY));
            entry.setDesiredDate((Date)dbObject.get(WaitListEntry.DESIRED_DATE_KEY));
            entry.setDaysOnList((String)dbObject.get(WaitListEntry.DAYS_ON_LIST_KEY));
            entry.setLastName((String)dbObject.get(WaitListEntry.LAST_NAME_KEY));
            entry.setSsn((String)dbObject.get(WaitListEntry.SSN_KEY));
            entry.setServiceConnected((String)dbObject.get(WaitListEntry.SERVICE_CONNECTED_KEY));
            entry.setClinicName((String)dbObject.get(WaitListEntry.CLINIC_NAME_KEY));
            entry.setOriginatingDate((Date)dbObject.get(WaitListEntry.ORIGINATING_DATE_KEY));
            entry.setPatientZip((String)dbObject.get(WaitListEntry.PATIENT_ZIP_KEY));
            entry.setEnrolleeStatus((String)dbObject.get(WaitListEntry.ENROLLEE_STATUS_KEY));
            entry.setPriority((String)dbObject.get(WaitListEntry.PRIORITY_KEY));
            entry.setWaitListType((String)dbObject.get(WaitListEntry.WAIT_LIST_TYPE_KEY));
            entry.setHomePhone((String)dbObject.get(WaitListEntry.HOME_PHONE_KEY));
            entry.setCellPhone((String)dbObject.get(WaitListEntry.CELL_PHONE_KEY));
            entry.setFacilityId((String)dbObject.get(WaitListEntry.FACILITY_ID_KEY));
            entry.setMessage((String)dbObject.get(WaitListEntry.MESSAGE_KEY));
            entry.setEligibilityCodeInternal((String)dbObject.get(WaitListEntry.ELIGIBILITY_CODE_INTERNAL_KEY));
            entry.setEligibilityCodeExternal((String)dbObject.get(WaitListEntry.ELIGIBILITY_CODE_EXTERNAL_KEY));
            entry.setDateRemovedFromEwl((Date)dbObject.get(WaitListEntry.DATE_REMOVED_FROM_EWL_KEY));
            entry.setClinicId((String)dbObject.get(WaitListEntry.CLINIC_ID_KEY));
            entry.setAccessionno((String)dbObject.get(WaitListEntry.ACCESSIONNO_KEY));
            entry.setProvider((String)dbObject.get(WaitListEntry.PROVIDER_KEY));
            entry.setComment((String)dbObject.get(WaitListEntry.COMMENT_KEY));
            entry.setRecallDate((Date)dbObject.get(WaitListEntry.RECALL_DATE_KEY));
            entry.setFastNonFasting((String)dbObject.get(WaitListEntry.FAST_NON_FASTING_KEY));
            entry.setDateReminderSent((Date)dbObject.get(WaitListEntry.DATE_REMINDER_SENT_KEY));
            entry.setAppointmentLength((String)dbObject.get(WaitListEntry.APPOINTMENT_LENGTH_KEY));
            entry.setUser((String)dbObject.get(WaitListEntry.USER_KEY));
            entry.setRecallDate((Date)dbObject.get(WaitListEntry.RECALL_DATE_1_KEY));
            entry.setSecondPrintDate((Date)dbObject.get(WaitListEntry.SECOND_PRINT_DATE_KEY));
            entry.setPatientName((String)dbObject.get(WaitListEntry.PATIENT_NAME_KEY));
            entry.setAppointmentRequestDate((Date)dbObject.get(WaitListEntry.APPOINTMENT_REQUEST_DATE_KEY));
            entry.setEwlIen((String)dbObject.get(WaitListEntry.EWL_IEN_KEY));

            waitListToReturn.add(entry);
        }

        return waitListToReturn;
    }


    public WaitListEntry convertFromDbObject(DBObject dbObject) {

            WaitListEntry     entry;

            entry = new WaitListEntry();

            entry.setDfn((String)dbObject.get(WaitListEntry.DFN_KEY));
            entry.setStopCode((String)dbObject.get(WaitListEntry.STOP_CODE_KEY));
            entry.setDateLastUpdated((Date)dbObject.get(WaitListEntry.DATE_LAST_UPDATED_KEY));
            entry.setDesiredDate((Date)dbObject.get(WaitListEntry.DESIRED_DATE_KEY));
            entry.setDaysOnList((String)dbObject.get(WaitListEntry.DAYS_ON_LIST_KEY));
            entry.setLastName((String)dbObject.get(WaitListEntry.LAST_NAME_KEY));
            entry.setSsn((String)dbObject.get(WaitListEntry.SSN_KEY));
            entry.setServiceConnected((String)dbObject.get(WaitListEntry.SERVICE_CONNECTED_KEY));
            entry.setClinicName((String)dbObject.get(WaitListEntry.CLINIC_NAME_KEY));
            entry.setOriginatingDate((Date)dbObject.get(WaitListEntry.ORIGINATING_DATE_KEY));
            entry.setPatientZip((String)dbObject.get(WaitListEntry.PATIENT_ZIP_KEY));
            entry.setEnrolleeStatus((String)dbObject.get(WaitListEntry.ENROLLEE_STATUS_KEY));
            entry.setPriority((String)dbObject.get(WaitListEntry.PRIORITY_KEY));
            entry.setWaitListType((String)dbObject.get(WaitListEntry.WAIT_LIST_TYPE_KEY));
            entry.setHomePhone((String)dbObject.get(WaitListEntry.HOME_PHONE_KEY));
            entry.setCellPhone((String)dbObject.get(WaitListEntry.CELL_PHONE_KEY));
            entry.setFacilityId((String)dbObject.get(WaitListEntry.FACILITY_ID_KEY));
            entry.setMessage((String)dbObject.get(WaitListEntry.MESSAGE_KEY));
            entry.setEligibilityCodeInternal((String)dbObject.get(WaitListEntry.ELIGIBILITY_CODE_INTERNAL_KEY));
            entry.setEligibilityCodeExternal((String)dbObject.get(WaitListEntry.ELIGIBILITY_CODE_EXTERNAL_KEY));
            entry.setDateRemovedFromEwl((Date)dbObject.get(WaitListEntry.DATE_REMOVED_FROM_EWL_KEY));
            entry.setClinicId((String)dbObject.get(WaitListEntry.CLINIC_ID_KEY));
            entry.setAccessionno((String)dbObject.get(WaitListEntry.ACCESSIONNO_KEY));
            entry.setProvider((String)dbObject.get(WaitListEntry.PROVIDER_KEY));
            entry.setComment((String)dbObject.get(WaitListEntry.COMMENT_KEY));
            entry.setRecallDate((Date)dbObject.get(WaitListEntry.RECALL_DATE_KEY));
            entry.setFastNonFasting((String)dbObject.get(WaitListEntry.FAST_NON_FASTING_KEY));
            entry.setDateReminderSent((Date)dbObject.get(WaitListEntry.DATE_REMINDER_SENT_KEY));
            entry.setAppointmentLength((String)dbObject.get(WaitListEntry.APPOINTMENT_LENGTH_KEY));
            entry.setUser((String)dbObject.get(WaitListEntry.USER_KEY));
            entry.setRecallDate((Date)dbObject.get(WaitListEntry.RECALL_DATE_1_KEY));
            entry.setSecondPrintDate((Date)dbObject.get(WaitListEntry.SECOND_PRINT_DATE_KEY));
            entry.setPatientName((String)dbObject.get(WaitListEntry.PATIENT_NAME_KEY));
            entry.setAppointmentRequestDate((Date)dbObject.get(WaitListEntry.APPOINTMENT_REQUEST_DATE_KEY));
            entry.setEwlIen((String)dbObject.get(WaitListEntry.EWL_IEN_KEY));

        return entry;
    }

}
