package fr.sgr.formation.voteapp.utilisateurs.modele;

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

import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import fr.sgr.formation.voteapp.elections.modele.Election;
import fr.sgr.formation.voteapp.elections.modele.Vote;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.Singular;

@Data
@EqualsAndHashCode(of = { "login" })
@Builder

@AllArgsConstructor
@NoArgsConstructor
@Entity
@NamedQueries({
		@NamedQuery(name = "updateMotdePasse", query = "UPDATE Utilisateur User SET User.motDePasse= :mdpUser WHERE User.login= :loginUser"),
		@NamedQuery(name = "nombreTotalUtilisateur", query = "SELECT COUNT(user) FROM  Utilisateur user") })
public class Utilisateur {
	@Id
	private String login;
	private String nom;
	private String prenom;

	private String motDePasse;

	@Temporal(TemporalType.DATE)
	private Date dateDeNaissance;
	private String email;

	@ElementCollection(targetClass = ProfilsUtilisateur.class)
	@CollectionTable(name = "profilsUtilisateurs", joinColumns = @JoinColumn(name = "loginUtilisateur") )
	@Enumerated(EnumType.STRING)
	@Singular
	private List<ProfilsUtilisateur> profils;

	// Un gerant peut gérer plusieurs élections
	@OneToMany(mappedBy = "gerant")
	private List<Election> gerantElection;

	@OneToMany(mappedBy = "electeur")
	private List<Vote> listeElection = new ArrayList<Vote>();

	@Embedded
	private Adresse adresse;

	public String toHtml() {
		StringBuilder stringBuilder = new StringBuilder();

		/** Construction de la réponse au format demandé... */
		stringBuilder.append("<html>");
		stringBuilder.append("<body>");
		stringBuilder.append("<h1></H1>");
		stringBuilder.append(login);
		stringBuilder.append("<br />");
		stringBuilder.append(nom);
		stringBuilder.append("<br />");
		stringBuilder.append(prenom);
		stringBuilder.append("<br />");
		stringBuilder.append(dateDeNaissance);
		stringBuilder.append("<br />");
		stringBuilder.append(email);
		stringBuilder.append("</body>");
		stringBuilder.append("</html>");

		return stringBuilder.toString();
	}
}
