package com.agilex.healthcare.schedulingmanager.linkbuilder;

import com.agilex.healthcare.mobilehealthplatform.domain.AtomLink;
import com.agilex.healthcare.mobilehealthplatform.domain.atom.rel;
import com.agilex.healthcare.schedulingmanager.domain.SMAppointmentRequest;
import com.agilex.healthcare.schedulingmanager.domain.SMAppointmentRequests;
import com.agilex.healthcare.schedulingmanager.domain.SchedulingManagerLinkTitles;

import javax.ws.rs.Path;
import javax.ws.rs.core.UriBuilder;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;

public class AppointmentRequestLinkBuilder extends PatientDataLinkBuilder<SMAppointmentRequests, SMAppointmentRequest> {

	public AppointmentRequestLinkBuilder(URI baseUri) {
		super(baseUri);
	}
	
	@Override
	public URI getCannonicalSelfUri(SMAppointmentRequest dataItem) {
		String encodedUserId = getEncodedUserId(dataItem);
		String encodedAppointmentRequestId = encode(dataItem.getAppointmentRequestId());
		
		URI selfUri = UriBuilder.fromUri(getBaseUri()).path("appointment-service").path("patient").path(dataItem.getPatientIdentifier().getAssigningAuthority())
				.path(encodedUserId).path("appointments").path("system").path(dataItem.getDataIdentifier().getSystemId()).path("id").path(encodedAppointmentRequestId).build();
		
		return selfUri;
	}

    private String getEncodedUserId(SMAppointmentRequest dataItem) {
        return encode(dataItem.getPatientId());
    }

    @Override
	protected void fillCustomRelatedLinks(SMAppointmentRequest dataItem) {
		AtomLink link = new AtomLink();
		link.setRel(rel.related);
        String encodedUserId = getEncodedUserId(dataItem);
        String encodedAppointmentRequestId = encode(dataItem.getAppointmentRequestId());
        URI relatedUri = UriBuilder.fromUri(getBaseUri()).path("appointment-service").path("patient").path(dataItem.getPatientIdentifier().getAssigningAuthority())
                .path(encodedUserId).path("appointment-requests").path("system").path(dataItem.getDataIdentifier().getSystemId()).path("id").path(encodedAppointmentRequestId).path("messages").build();

		link.setHref(relatedUri);
		link.setTitle(SchedulingManagerLinkTitles.AppointmentRequestMessages);
		dataItem.getLink().add(link);

        link = new AtomLink();
        link.setRel(rel.related);
        URI newMessageFlagUri = UriBuilder.fromUri(getBaseUri()).path("appointment-service").path("patient").path(dataItem.getPatientIdentifier().getAssigningAuthority())
                .path(encodedUserId).path("appointment-requests").path("system").path(dataItem.getDataIdentifier().getSystemId()).path("id").path(encodedAppointmentRequestId).path("messages").path("read").build();
        link.setHref(newMessageFlagUri);
        link.setTitle(SchedulingManagerLinkTitles.AppointmentRequestNewMessageFlag);
        dataItem.getLink().add(link);

        link = new AtomLink();
        link.setRel(rel.related);
        URI newProviderSeenFlagUri = UriBuilder.fromUri(getBaseUri()).path("appointment-service").path("patient").path(dataItem.getPatientIdentifier().getAssigningAuthority())
                .path(encodedUserId).path("appointment-requests").path("system").path(dataItem.getDataIdentifier().getSystemId()).path("id").path(encodedAppointmentRequestId).path("appointment").path("provider-read").build();
        link.setHref(newProviderSeenFlagUri);
        link.setTitle(SchedulingManagerLinkTitles.AppointmentRequestProviderSeenFlag);
        dataItem.getLink().add(link);

        link = new AtomLink();
        link.setRel(rel.related);
        URI patientUri = UriBuilder.fromUri(getBaseUri()).path("appointment-service").path("patient").path(dataItem.getPatientIdentifier().getAssigningAuthority()).path(dataItem.getPatientIdentifier().getUniqueId()).build();
        link.setHref(patientUri);
        link.setTitle(SchedulingManagerLinkTitles.ConvertPatientIdentifier);
        dataItem.getLink().add(link);
        
        link = new AtomLink();
        link.setRel(rel.related);
        URI patientPrefUri = UriBuilder.fromUri(getBaseUri()).path("patient").path(dataItem.getPatientIdentifier().getAssigningAuthority()).path(dataItem.getPatientIdentifier().getUniqueId()).path("preference").build();
        link.setHref(patientPrefUri);
        link.setTitle(SchedulingManagerLinkTitles.patientPrefUri);
        dataItem.getLink().add(link);
    }
	
	@Override
	protected void fillCustomRelatedLinks(SMAppointmentRequests dataList) {
	}
	
	private String encode(String id) {
		String encoded;
		try {
			encoded = URLEncoder.encode(id, "UTF-8");
		} catch (UnsupportedEncodingException failedToEncode) {
			throw new RuntimeException("Could not encode a URL parameter for SMAppointmentRequests");
		}
		return encoded;
	}

	@Override
	public URI getCannonicalUriForCollection(URI patientUri) {
		return null;
	}

}