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.SMConsult;
import com.agilex.healthcare.schedulingmanager.domain.SMConsultDetail;
import com.agilex.healthcare.schedulingmanager.domain.SMConsults;
import com.agilex.healthcare.schedulingmanager.domain.SchedulingManagerLinkTitles;

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

/**
 * Created with IntelliJ IDEA.
 * User: patelh
 * Date: 12/5/13
 * Time: 12:10 PM
 * To change this template use File | Settings | File Templates.
 */
public class ConsultLinkBuilder {

    private URI baseUri;

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

    public SMConsults fillLinks(SMConsults smConsults, URI requestUri, String siteCode){
        smConsults.setSelfUri(requestUri);
        if(smConsults != null && smConsults.size() > 0){
            for (SMConsult smConsult : smConsults){
                fillCustomRelatedLinks(smConsult, siteCode);
            }
        }
        return smConsults;
    }

    protected void fillCustomRelatedLinks(SMConsult smConsult, String siteCode){
        AtomLink link = new AtomLink();

        link.setRel(rel.related);
        URI consultDetailUri = UriBuilder.fromUri(getBaseUri()).path("scheduling-service").path("site").path(siteCode).path("consult").path(smConsult.getConsultIen()).path("detail").build();
        link.setHref(consultDetailUri);
        link.setTitle(SchedulingManagerLinkTitles.ConsultDetail);
        smConsult.getLink().add(link);

        link = new AtomLink();
        link.setRel(rel.related);
        URI printConsultUri = UriBuilder.fromUri(getBaseUri()).path("scheduling-service").path("site").path(siteCode).path("patient").path("dfn-"+siteCode).path(smConsult.getPatientDfn()).path("consult").path(smConsult.getConsultIen()).path(smConsult.getServiceIen()).path("pdf").build();
        link.setHref(printConsultUri);
        link.setTitle(SchedulingManagerLinkTitles.PrintConsult);
        smConsult.getLink().add(link);

    }

    public SMConsultDetail fillLinks(SMConsultDetail smConsultDetail, URI requestUri, String siteCode){
        AtomLink link = new AtomLink();
        link.setRel(rel.self);
        URI consultDetailUri = UriBuilder.fromUri(getBaseUri()).path("scheduling-service").path("site").path(siteCode).path("consult").path(smConsultDetail.getConsultIen()).path("detail").build();
        link.setHref(consultDetailUri);
        link.setTitle(SchedulingManagerLinkTitles.ConsultDetail);
        smConsultDetail.getLink().add(link);
        return smConsultDetail;
    }

    public SMConsults fillLinks(SMConsults smConsults, URI requestUri, String patientId, String siteCode){
        smConsults.setSelfUri(requestUri);
        if(smConsults !=null && smConsults.size() > 0){
            for (SMConsult smConsult : smConsults){
                fillCustomRelatedLinks(smConsult, siteCode, patientId);
            }
        }
        return smConsults;
    }

    protected void fillCustomRelatedLinks(SMConsult smConsult, String siteCode, String patientId){
        AtomLink link = new AtomLink();

        link = new AtomLink();
        link.setRel(rel.related);
        URI printConsultUri = UriBuilder.fromUri(getBaseUri()).path("scheduling-service").path("site").path(siteCode).path("patient").path("dfn-"+siteCode).path(patientId).path("consult").path(smConsult.getConsultIen()).path("pdf").build();
        link.setHref(printConsultUri);
        link.setTitle(SchedulingManagerLinkTitles.PrintPatientConsult);
        smConsult.getLink().add(link);
    }

    public URI getBaseUri() {
        return baseUri;
    }

    public void setBaseUri(URI baseUri) {
        this.baseUri = baseUri;
    }
}
