package it.enea.cruise.model.user;


import java.sql.Date;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import javax.persistence.PreRemove;
import javax.persistence.Table;


import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * 
 * @author Gianluca D'Agosta Questa classe gestisce la tabella
 *         "users_ommunities" del database con i dati di consumo delle Comunita'
 *         energetiche. Essa permette di estrarre il campo "wallet" collegato ad
 *         un utente ed una comunita, che non sarebeb raggiungibile altrimenti.
 *
 */

@Entity(name = "userCommunity")
@Table(name = "users_communities")
public class UserCommunity {

	/*
	 * Properties
	 */

	@EmbeddedId
	@JsonIgnore
	private UserCommunityId id = new UserCommunityId();

	@ManyToOne
	@JoinColumn(name = "userid")
	@MapsId("userid")
	//@JsonBackReference(value = "user-user_communities")
	private User user;

	@ManyToOne
	@JoinColumn(name = "communityid")
	@MapsId("communityid")
	//@JsonBackReference(value = "community-communities")
	//@JsonIgnoreProperties("userCommunities")
	private Community community;

	@Column(name = "wallet")
	private String wallet;

	@Column(name = "subscriptiondate")
	private Date subscriptiondate;
	
	@Column(name = "unsubscriptiondate")
	private Date unsubscriptiondate;
	
	@Column(name = "active")
	private Boolean active;
	
	/*
	 * Generic Methods
	 */

	public UserCommunity() {
	}

	public UserCommunity(User user, Community community) {
		this.user = user;
		this.community = community;
		this.id = new UserCommunityId(user.getUserid(), community.getCommunityid());
	}

	/*
	 * Getters and setters
	 */

	public String getWallet() {
		return wallet;
	}

	public void setWallet(String wallet) {
		this.wallet = wallet;
	}

	public UserCommunityId getId() {
		return id;
	}

	public void setId(UserCommunityId id) {
		this.id = id;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		id.setUserId(user.getUserid());
		this.user = user;
	}

	public Community getCommunity() {
		return community;
	}

	public void setCommunity(Community community) {
		id.setCommunityId(community.getCommunityid());
		this.community = community;
	}

//	@Override
//	public String toString() {
//		String result = "";
//		result = "{\"user\" : " + this.user.getUserid() + ","
//				+ "\"community\" : " + this.community.getCommunityid() + ","
//				+ "\"wallet\" : \"" + this.wallet +"\""
//				+ "}";
//		
//		return result;
//	}

	public void clear() {
		this.user = null;
		this.community = null;
	}

	public void setUser2Null() {
		this.user = null;
	}

	public void dismissUser() {
		this.user = null;
		this.community = null;

	}

	@PreRemove
	public void removeLinks() {
		/*
		 * Devo rimuovere i link agli oggetti user e community e scollegare anche
		 * dall'altro lato della relazione
		 */
		this.user.getUsercommunities().remove(this);
		this.getCommunity().getUserCommunities().remove(this);
		this.community = new Community();
		this.user = new User();

	}

	public Date getSubscriptiondate() {
		return subscriptiondate;
	}

	public void setSubscriptiondate(Date subscriptiondate) {
		this.subscriptiondate = subscriptiondate;
	}

	public Date getUnsubscriptiondate() {
		return unsubscriptiondate;
	}

	public void setUnsubscriptiondate(Date unsubscriptiondate) {
		if (unsubscriptiondate == null) {
			this.unsubscriptiondate = new Date(System.currentTimeMillis());
		}else {
			this.unsubscriptiondate = unsubscriptiondate;
		}
	}

	public Boolean getActive() {
		return active;
	}

	public void setActive(Boolean active) {
		this.active = active;
	}

}
