package org.pdxfinder.graph.dao;

import org.neo4j.ogm.annotation.GraphId;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;

import java.util.ArrayList;
import java.util.List;

/*
 * Created by csaba on 23/10/2017.
 */


@NodeEntity
public class TreatmentProtocol {

    /**
     * @param components                The treatment components that were used in this protocol
     * @param armSize                   The number of animals used in the study arm
     * @param response                  A recist classification of the response to the treatment
     * @param responseCalculationMethod The method used to determine the response classification
     * @param passages                  The list of passages at which this treatment was applied
     */

    @GraphId
    private Long id;

    @Relationship(type = "TREATMENT_COMPONENT")
    private List<TreatmentComponent> components;

    @Relationship(type = "RESPONSE")
    private Response response;

    @Relationship(type = "CURRENT_TREATMENT")
    private CurrentTreatment currentTreatment;

    private String armSize;
    private String responseCalculationMethod;
    private String passages;

    private String treatmentDate;

    public TreatmentProtocol() {
        components = new ArrayList<>();
    }


    public List<TreatmentComponent> getComponents() {
        return components;
    }

    public void setComponents(List<TreatmentComponent> components) {
        this.components = components;
    }

    public Response getResponse() {
        return response;
    }

    public void setResponse(Response response) {
        this.response = response;
    }

    public String getArmSize() {
        return armSize;
    }

    public void setArmSize(String armSize) {
        this.armSize = armSize;
    }

    public String getResponseCalculationMethod() {
        return responseCalculationMethod;
    }

    public void setResponseCalculationMethod(String responseCalculationMethod) {
        this.responseCalculationMethod = responseCalculationMethod;
    }

    public String getPassages() {
        return passages;
    }

    public void setPassages(String passages) {
        this.passages = passages;
    }

    public CurrentTreatment getCurrentTreatment() {
        return currentTreatment;
    }

    public void setCurrentTreatment(CurrentTreatment currentTreatment) {
        this.currentTreatment = currentTreatment;
    }

    public String getTreatmentDate() {
        return treatmentDate;
    }

    public void setTreatmentDate(String treatmentDate) {
        this.treatmentDate = treatmentDate;
    }

    public String getTreatmentString(boolean includeControlDrugs){

        String ret = "";

        for(TreatmentComponent comp:components){

            String drugName;

            if(
                comp.getTreatment() != null && 
                comp.getTreatment().getTreatmentToOntologyRelationship() != null && 
                comp.getTreatment().getTreatmentToOntologyRelationship().getOntologyTerm() != null
            ){

                drugName = comp.getTreatment().getTreatmentToOntologyRelationship().getOntologyTerm().getLabel();
            }
            else{
                continue;
            }

            if(includeControlDrugs){
                if(!ret.isEmpty()){
                    ret+=" and ";
                }
                ret+=drugName;
            }
            //include only Drugs but no Controls
            else{

                if(comp.getType().equals("Drug")){

                    if(!ret.isEmpty()){
                        ret+=" and ";
                    }
                    ret+=drugName;
                }

            }

        }

        return ret;
    }





    public String getDurationString(boolean includeControlDrugs){

        String durString = "";

        for(TreatmentComponent comp:components){

            String dur = comp.getDuration();

            if(includeControlDrugs){
                if(!durString.isEmpty()){
                    durString+=" / ";
                }
                durString+=dur;
            }
            //include only Drugs but no Controls
            else{

                if(comp.getType().equals("Drug")){

                    if(!durString.isEmpty()){
                        durString+=" / ";
                    }
                    durString+=dur;
                }

            }

        }

        return durString;
    }

    public String getDoseString(boolean includeControlDrugs){

        String doseString = "";

        for(TreatmentComponent comp:components){

            String dose = comp.getDose();

            if(includeControlDrugs){
                if(!doseString.isEmpty()){
                    doseString+=" / ";
                }
                doseString+=dose;
            }
            //include only Drugs but no Controls
            else{

                if(comp.getType().equals("Drug")){

                    if(!doseString.isEmpty()){
                        doseString+=" / ";
                    }
                    doseString+=dose;
                }

            }

        }

        return doseString;
    }


    public void addTreatmentComponent(TreatmentComponent tc){

        if(components == null){
            components = new ArrayList<>();
        }

        components.add(tc);
    }


    public void addDurationForAllComponents(String duration){

        if(components != null){

            for(TreatmentComponent tc : components){

                tc.setDuration(duration);
            }
        }
    }

}

@@ -187,16 +187,46 @@
 ayList;%0A
+import java.util.Collections;%0A
 import j
@@ -2720,32 +2720,82 @@
 ControlDrugs)%7B%0A%0A
+        List%3CString%3E result = new ArrayList%3C%3E();%0A%0A
         String r
@@ -3392,109 +3392,27 @@
     
-if(!ret.isEmpty())%7B%0A                    ret+=%22 and %22;%0A                %7D%0A                ret+=
+result.add(
 drugName
 ;%0A  
@@ -3399,32 +3399,33 @@
 ult.add(drugName
+)
 ;%0A            %7D%0A
@@ -3568,122 +3568,48 @@
     
-if(!ret.isEmpty())%7B%0A                        ret+=%22 and %22;%0A                    %7D%0A                    ret+=drugName;
+result.add(drugName);%0A                %7D%0A
 %0A   
@@ -3613,35 +3613,27 @@
             
-    %7D%0A%0A    
+%7D%0A%0A
         %7D%0A%0A 
@@ -3643,29 +3643,78 @@
     
-%7D%0A%0A        return ret
+Collections.sort(result);%0A%0A        return String.join(%22 and %22, result)
 ;%0A  

Order treatment string components alphabetically