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.SchedulingClinic;
import com.agilex.healthcare.schedulingmanager.domain.SchedulingClinics;
import com.agilex.healthcare.schedulingmanager.domain.SchedulingManagerLinkTitles;

import javax.ws.rs.core.UriBuilder;
import java.net.URI;

public class ClinicLinkBuilder {

    private URI baseUri;
    private String facilityCode;

    public ClinicLinkBuilder(URI baseUri, String facilityCode) {
        this.baseUri = baseUri;
        this.facilityCode = facilityCode;
    }

    public SchedulingClinics fillLinks(SchedulingClinics clinics){

        for(SchedulingClinic clinic : clinics){
            fillCustomRelatedLinks(clinic);
        }

        return clinics;
    }


    protected void fillCustomRelatedLinks(SchedulingClinic clinic) {

        AtomLink link = new AtomLink();
        link.setRel(rel.related);
        URI relatedUri = UriBuilder.fromUri(getBaseUri())
                                   .path("scheduling-service")
                                   .path("site").path(facilityCode)
                                   .path("available-appointment-slots")
                                   .path("clinic").path(clinic.getId()).build();
        link.setHref(relatedUri);
        link.setTitle(SchedulingManagerLinkTitles.ClinicAppointmentSlots);
        clinic.getLink().add(link);
    }

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

}
