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.SchedulingManagerLinkTitles;
import com.agilex.healthcare.schedulingmanager.domain.WaitListEntries;
import com.agilex.healthcare.schedulingmanager.domain.WaitListEntry;
import com.agilex.healthcare.schedulingmanager.domain.WaitListEntryDetail;

import javax.ws.rs.core.UriBuilder;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;

public class WaitListLinkBuilder {

    private URI baseUri;

    public WaitListLinkBuilder(URI baseUri) {
        this.baseUri = baseUri;
    }

    public WaitListEntries fillLinks(WaitListEntries waitListEntries, URI requestUri, String facilityCode) {
        waitListEntries.setSelfUri(requestUri);
        if(waitListEntries != null && waitListEntries.size() > 0){
            for(WaitListEntry waitListEntry : waitListEntries){
                fillCustomRelatedLinks(waitListEntry, facilityCode);
            }
        }
        return waitListEntries;
    }

    protected void fillCustomRelatedLinks(WaitListEntry waitListEntry, String facilityCode) {
        String encodedWaitListId = encode(waitListEntry.getId());
        waitListEntry.setSelfUri(UriBuilder.fromUri(getBaseUri()).path("scheduling-service").path("site").path(facilityCode).path("waitlist").path(waitListEntry.getStopCode().toLowerCase()).path("id").path(encodedWaitListId).build());

        AtomLink link = new AtomLink();
        link.setRel(rel.related);
        URI relatedUri = UriBuilder.fromUri(getBaseUri()).path("scheduling-service").path("site").path(facilityCode).path("waitlist").path(waitListEntry.getStopCode().toLowerCase()).path("id").path(encodedWaitListId).path("detail").build();
        link.setHref(relatedUri);
        link.setTitle(SchedulingManagerLinkTitles.WaitListDetail);
        waitListEntry.getLink().add(link);

    }

    public WaitListEntry fillLinks(WaitListEntry waitListEntry, URI requestUri, String facilityCode){

        String encodedWaitListId = encode(waitListEntry.getId());
        waitListEntry.setSelfUri(requestUri);

        AtomLink link = new AtomLink();
        link.setRel(rel.related);
        URI relatedUri = UriBuilder.fromUri(getBaseUri()).path("scheduling-service").path("site").path(facilityCode).path("waitlist").path(waitListEntry.getStopCode().toLowerCase()).path("id").path(encodedWaitListId).path("detail").build();
        link.setHref(relatedUri);
        link.setTitle(SchedulingManagerLinkTitles.WaitListDetail);

        waitListEntry.getLink().add(link);

        return waitListEntry;
    }


    public WaitListEntryDetail fillLinks(WaitListEntryDetail waitListEntryDetail, URI requestUri){
        waitListEntryDetail.setSelfUri(requestUri);
        AtomLink link = new AtomLink();
        link.setRel(rel.related);
        URI relatedUri =UriBuilder.fromUri(this.baseUri).path("patient").path(waitListEntryDetail.getPatientIdentifier().getUniqueId()).path(waitListEntryDetail.getPatientIdentifier().getAssigningAuthority()).build();
        link.setHref(relatedUri);
        link.setTitle(SchedulingManagerLinkTitles.Patient);
        waitListEntryDetail.getLink().add(link);

        return waitListEntryDetail;

    }


    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 AppointmentRequests");
        }
        return encoded;
    }
//    private String getEncodedUserId(WaitListEntry dataItem) {
//        return encode(dataItem.getPatientId());
//    }

    protected URI getBaseUri() {
        return this.baseUri;
    }

}
