package uk.ac.ebi.biosamples.mongo.model;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.LastModifiedDate;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import uk.ac.ebi.biosamples.models.Attribute;
import uk.ac.ebi.biosamples.models.CustomLocalDateTimeDeserializer;
import uk.ac.ebi.biosamples.models.CustomLocalDateTimeSerializer;
import uk.ac.ebi.biosamples.models.Relationship;

public class MongoSample {
	
	@Id
	public String accession;

	protected String name; 
	
	@JsonSerialize(using = CustomLocalDateTimeSerializer.class)
	@JsonDeserialize(using = CustomLocalDateTimeDeserializer.class)
	protected LocalDateTime release; 
	@JsonSerialize(using = CustomLocalDateTimeSerializer.class)
	@JsonDeserialize(using = CustomLocalDateTimeDeserializer.class)
	@LastModifiedDate
	protected LocalDateTime update;

	protected SortedSet<Attribute> attributes;
	protected SortedSet<Relationship> relationships;

	private MongoSample() {
		
	}

	public String getAccession() {
		return accession;
	}

	public String getName() {
		return name;
	}

	public LocalDateTime getRelease() {
		return release;
	}

	public LocalDateTime getUpdate() {
		return update;
	}

	public SortedSet<Attribute> getAttributes() {
		return attributes;
	}
	
	public void addAttribute(Attribute attribute) {
		if (attributes == null) {
			attributes = new TreeSet<>();
		}
		attributes.add(attribute);
	}

	public SortedSet<Relationship> getRelationships() {
		return relationships;
	}
	
	public void addRelationship(Relationship relationship) {
		if (relationships == null) {
			relationships = new TreeSet<>();
		}
		relationships.add(relationship);
	}

	@Override
    public boolean equals(Object o) {

        if (o == this) return true;
        if (!(o instanceof MongoSample)) {
            return false;
        }
        MongoSample other = (MongoSample) o;
        return Objects.equals(this.name, other.name) 
        		&& Objects.equals(this.accession, other.accession)
        		&& Objects.equals(this.release, other.release)
        		&& Objects.equals(this.update, other.update)
        		&& Objects.equals(this.attributes, other.attributes)
        		&& Objects.equals(this.relationships, other.relationships);
    }
    
    @Override
    public int hashCode() {
    	return Objects.hash(name, accession, release, update, attributes, relationships);
    }
	
	static public MongoSample build(String name, String accession, LocalDateTime release, LocalDateTime update, Set<Attribute> attributes, Set<Relationship> relationships){
		MongoSample sample = new MongoSample();
		sample.accession = accession;
		sample.name = name;
		sample.release = release;
		sample.update = update;
		
		if (attributes == null || attributes.size() == 0) {
			sample.attributes = null;
		} else {
			sample.attributes = new TreeSet<>();
			sample.attributes.addAll(attributes);
		}

		if (relationships == null || relationships.size() == 0) {
			sample.relationships = null;
		} else {
			sample.relationships = new TreeSet<>();
			sample.relationships.addAll(relationships);
		}
		return sample;
	}

}

@@ -2941,26 +2941,26 @@
 ributes 
-=
+!
 = null 
-%7C%7C
+&&
  attribu
@@ -2962,34 +2962,33 @@
 tributes.size() 
-==
+%3E
  0) %7B%0A%09%09%09sample.
@@ -2984,85 +2984,50 @@
 %0A%09%09%09
-sample.attributes = null;%0A%09%09%7D else %7B%0A%09%09%09sample.attributes = new TreeSet%3C%3E();%0A
+for (Attribute attribute : attributes) %7B%0A%09
 %09%09%09s
@@ -3033,24 +3033,27 @@
 sample.a
+ddA
 ttribute
 s.addAll
@@ -3044,24 +3044,16 @@
 ttribute
-s.addAll
 (attribu
@@ -3054,20 +3054,24 @@
 ttribute
-s
 );%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%09i
@@ -3091,18 +3091,18 @@
 ips 
-=
+!
 = null 
-%7C%7C
+&&
  rel
@@ -3119,18 +3119,17 @@
 .size() 
-==
+%3E
  0) %7B%0A%09%09
@@ -3125,32 +3125,30 @@
 ) %3E 0) %7B%0A%09%09%09
-sample.r
+for (R
 elationships
@@ -3150,72 +3150,42 @@
 ship
-s = null;%0A%09%09%7D else %7B%0A%09%09%09sample.relationships = new TreeSet%3C%3E();%0A
+ relationship : relationships) %7B%0A%09
 %09%09%09s
@@ -3190,17 +3190,20 @@
 %09sample.
-r
+addR
 elations
@@ -3209,16 +3209,8 @@
 ship
-s.addAll
 (rel
@@ -3218,20 +3218,24 @@
 tionship
-s
 );%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%09re

use add methods