package com.agilex.healthcare.schedulingmanager.dataservice;

import com.agilex.healthcare.mobilehealthplatform.domain.filter.datefilter.DateFilter;
import com.agilex.healthcare.schedulingmanager.datalayer.SchedulingDataLayer;
import agilex.commons.pagination.PaginationDataLayer;
import com.agilex.healthcare.schedulingmanager.datalayer.SchedulingDataLayer;
import com.agilex.healthcare.schedulingmanager.domain.SMConsults;
import com.agilex.healthcare.schedulingmanager.translator.ConsultMongoTranslator;
import com.agilex.healthcare.schedulingmanager.utils.ListType;
import com.mongodb.DBObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: patelh
 * Date: 12/20/13
 * Time: 9:37 AM
 * To change this template use File | Settings | File Templates.
 */
@Component
public class ConsultDataService {
    private final Log     LOGGER = LogFactory.getLog(ConsultDataService.class);

    private long    LIST_DATA_IS_STALE_AFTER = 180000;    // 3 minutes

    @Resource(name="paginationDataLayer")
    PaginationDataLayer       paginationDataLayer;

    public SMConsults getListForFacility(SchedulingDataLayer schedulingDataLayer, ListType listType, boolean freshStart,
                                         String facilityCode, String                                      serviceId,
                                         HashMap<String, Boolean> sortFields, int  firstRecord, int       lastRecord,
                                         String providerId, DateFilter dateFilter){
        SMConsults                  smConsults = new SMConsults();
        List<DBObject>                smConsultList;
        Iterator                    listIterator;
        ConsultMongoTranslator      translator = new ConsultMongoTranslator();

        LOGGER.debug("Fetch Consult list for Facility : " +facilityCode+" and ServiceID : " + serviceId);

        if(paginationDataLayer.listDataIsStale(listType.getName(), facilityCode, null, null, serviceId)){
            SMConsults      smConsultsFromVista = schedulingDataLayer.getConsultsBySpecialties(serviceId, providerId, dateFilter, facilityCode);
            translator = new ConsultMongoTranslator(smConsultsFromVista);
            paginationDataLayer.saveList(translator.convertToDBObjects(), listType.getName(), facilityCode, null, null, serviceId, false);
        }

        smConsultList = paginationDataLayer.getList(listType.getName(), facilityCode, null, null, serviceId, sortFields, firstRecord, lastRecord, null);
        listIterator = smConsultList.iterator();
        while(listIterator.hasNext()) {
            DBObject    next = (DBObject)listIterator.next();

            smConsults.add(translator.dbObjectToSMConsult(next));
        }

        return smConsults;
    }

}
