package com.agilex.healthcare.schedulingmanager.linkbuilder;

import com.agilex.healthcare.mobilehealthplatform.domain.*;
import com.agilex.healthcare.utility.NullChecker;

import java.net.URI;

public class UriDtoValidator {
	public static void validate(PatientDataCollection<?> patientDataCollection) {
		if (patientDataCollection == null)
			throw new IllegalArgumentException("Cannot create URI for null collection object");
	}

	public static void validate(PatientData dto) {
		if (dto == null)
			throw new IllegalArgumentException("Cannot create URI for null object");
		validate(dto.getPatientIdentifier());
		if (dto.getDataIdentifier() != null)
			validate(dto.getDataIdentifier());
	}

	private static void validate(DataIdentifier identifier) {
		if (identifier == null)
			throw new IllegalArgumentException("Cannot create URI for object without identifier");
		if (!isValid(identifier.getUniqueId()))
			throw new IllegalArgumentException(String.format("Cannot create URI for object without identifier. [%s]", identifier.toString()));
	}

	private static void validate(PatientIdentifier identifier) {
		if (identifier == null)
			throw new IllegalArgumentException("Cannot create URI for object without patient identifier");
		if (!isValid(identifier.getUniqueId()) || !isValid(identifier.getAssigningAuthority()))
			throw new IllegalArgumentException(String.format("Cannot create URI for object without patient identifier. [%s]", identifier.toString()));
	}

	private static boolean isValid(String string) {
		return NullChecker.isNotNullish(string);
	}

	public static void validate(URI uri) {
		validate(uri, "");
	}

	public static void validate(URI uri, String failedValidationMessage) {
		if (uri == null || NullChecker.isNullish(uri.toString()))
			throw new IllegalArgumentException("Invalid URI. " + failedValidationMessage);
	}

	public static void validate(Patient patient) {
		if (patient == null)
			throw new IllegalArgumentException("Cannot create URI for null patient");
		validate(patient.getPatientIdentifier());
	}

	public static void validate(Patients patients) {
		if (patients == null) {
			throw new IllegalArgumentException("Cannot create URI for null patient list");
		}
	}

}
