package com.agilex.healthcare.schedulingmanager.datalayer;

import com.agilex.healthcare.mobilehealthplatform.domain.PatientIdentifier;
import com.agilex.healthcare.schedulingmanager.domain.SMConsults;
import com.agilex.healthcare.schedulingmanager.mdws.MdwsEmrDataLayer;
import com.agilex.healthcare.schedulingmanager.mdws.MdwsResponseValidator;
import com.agilex.healthcare.schedulingmanager.mdws.connection.EmrConnection;
import com.agilex.healthcare.schedulingmanager.mdws.connection.MdwsConfiguration;
import com.agilex.healthcare.schedulingmanager.mdws.connection.MdwsConfigurationHelper;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.emrservice.TaggedConsultArrays;
import com.agilex.healthcare.schedulingmanager.mdws.generatedwsdl.emrservice.TaggedText;
import com.agilex.healthcare.schedulingmanager.mdws.translator.PatientConsultTranslator;
import com.agilex.healthcare.schedulingmanager.mdws.translator.SpecialtiesTranslator;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: patelh
 * Date: 12/10/13
 * Time: 4:50 PM
 * To change this template use File | Settings | File Templates.
 */
public class EmrDataLayerMdws extends MdwsEmrDataLayer implements EmrDataLayer{

    //private final static org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory.getLog(EmrDataLayerMdws.class);

    public EmrDataLayerMdws(MdwsConfiguration mdwsConfiguration) {
        super(mdwsConfiguration);
    }

    /**
	 * Calls the MDWS service SchedulingSvc.getConsultsForPatient for the given patient
	 */
	@Override
    public SMConsults getPatientConsults(PatientIdentifier patientIdentifier) {
        EmrConnection authenticatedConnection = super.getAuthenticatedConnectionWithPatientSelect(new MdwsConfigurationHelper().extractSiteCodeFromAssigningAuthority(patientIdentifier.getAssigningAuthority()), patientIdentifier.getUniqueId());
        TaggedConsultArrays taggedConsultArrays = authenticatedConnection.getPort().getConsultsForPatient();
        MdwsResponseValidator.validateMdwsObject(taggedConsultArrays);
        PatientConsultTranslator patientConsultTranslator = new PatientConsultTranslator();
        SMConsults smConsults = patientConsultTranslator.translate(taggedConsultArrays);
        return smConsults;
    }

    @Override
    public Map<String, String> getSpecialties(final String siteCode) {
        EmrConnection authenticatedConnection = super.getAuthenticatedConnection(siteCode);
        TaggedText taggedText = authenticatedConnection.getPort().getSpecialties();
        MdwsResponseValidator.validateMdwsObject(taggedText);
        SpecialtiesTranslator specialtiesTranslator = new SpecialtiesTranslator();
        Map<String, String> specialtiesMap= specialtiesTranslator.translate(taggedText);
        return specialtiesMap;
    }
}
