package com.agilex.healthcare.schedulingmanager.translator;

import com.agilex.healthcare.mobilehealthplatform.utils.StringUtil;
import com.agilex.healthcare.schedulingmanager.domain.SchedulingClinic;
import com.agilex.healthcare.schedulingmanager.domain.SchedulingClinics;
import com.agilex.healthcare.schedulingmanager.domain.SchedulingSpecialty;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

import java.util.Iterator;

public class SpecialtyTranslator {

    private final String NAME_KEY = "name";
    private final String CLINICS_KEY = "clinics";
    private final String CLINIC_ID_KEY = "id";
    private final String FACILITY_CODE_KEY = "facility";

    public SchedulingSpecialty translate(DBObject po) {
        SchedulingSpecialty schedulingSpecialty = new SchedulingSpecialty();

        if (po != null) {

            schedulingSpecialty.setName(StringUtil.objectToString(po.get(NAME_KEY)));
            BasicDBList clinicList = (BasicDBList) po.get(CLINICS_KEY);

            SchedulingClinics specialtyClinics = new SchedulingClinics();
            Iterator<?> it = clinicList.iterator();
            while (it.hasNext()) {
                BasicDBObject clinicPo = (BasicDBObject) it.next();
                if (clinicPo.get(CLINIC_ID_KEY) != null) {
                    SchedulingClinic clinic = new SchedulingClinic();
                    clinic.setName(StringUtil.objectToString(clinicPo.get(NAME_KEY)));
                    clinic.setId(StringUtil.objectToString(clinicPo.get(CLINIC_ID_KEY)));
                    clinic.setFacilityCode(StringUtil.objectToString(po.get(FACILITY_CODE_KEY)));

                    specialtyClinics.add(clinic);
                }
            }

            schedulingSpecialty.setSpecialtyClinics(specialtyClinics);
        }
        return schedulingSpecialty;
    }

}
