package com.agilex.healthcare.schedulingmanager.dataservice;


import com.agilex.healthcare.mobilehealthplatform.domain.PatientIdentifier;
import com.agilex.healthcare.mobilehealthplatform.domain.ValidationResult;
import com.agilex.healthcare.schedulingmanager.datalayer.preference.PatientPreferenceDataLayer;
import com.agilex.healthcare.schedulingmanager.domain.SMPatientPreference;
import com.agilex.healthcare.schedulingmanager.exception.InvalidPreferenceDetailsException;
import com.agilex.healthcare.schedulingmanager.validator.PreferenceValidator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/***
 * This covers calendar events created by the veteran. These are items to show
 * up on their calendar that are not "appointments"
 * 
 * @author ???
 * 
 */
@Service
public class PatientPreferenceDataService {

    @Resource
	PatientPreferenceDataLayer patientPreferenceDataLayer;

	public PatientPreferenceDataService() {
	}

	public SMPatientPreference fetchPatientPreference(PatientIdentifier patientIdentifier) {
		return patientPreferenceDataLayer.fetchPatientPreference(patientIdentifier);
	}

	public SMPatientPreference updatePatientPreference(SMPatientPreference SMPatientPreference){
		validatePatientPreference(SMPatientPreference);
		return patientPreferenceDataLayer.updatePatientPreference(SMPatientPreference);
	}
	
	private void validatePatientPreference(SMPatientPreference patPref) {
			PreferenceValidator validator = new PreferenceValidator();
				ValidationResult<SMPatientPreference> validationResult = validator.validate(patPref);
				if (!validationResult.isValid()) {
					throw new InvalidPreferenceDetailsException(validationResult.getErrors());
				}
	}

	public void deletePatientPreference(SMPatientPreference SMPatientPreference){
		patientPreferenceDataLayer.deletePatientPreference(SMPatientPreference);
	}
}
