package task.timer.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.*;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

@Entity
@Table(name="USERS")

public class User extends AbstractEntity {
	 @Id @GeneratedValue(strategy = GenerationType.AUTO)
	 @Column(name = "id")
	 private int id;
	 
	 @Column (name = "login")
	 private String login;
	 
	 @Column (name="firstName")
	 private String firstName;
	 
	 @Column (name="lastName")
	 private String lastName;
	 
	 @Column (name="password")
	 private String password;
	 
	 @Column (name="permissions")
	 private String permissions;
	 
	 @ManyToMany(mappedBy="users")  
	 private Set<Project> projects = new HashSet<Project>();
	 
	 	
	public User(String login, String password, String firstName, String lastName, String permissions) {
		this.login = login;
		this.password = password;
		this.firstName = firstName;
		this.lastName = lastName;
		this.permissions = permissions;
	}
	
	public User(int id, String login, String password, String firstName, String lastName, String permissions) {
		this.id = id;
		this.login = login;
		this.password = password;
		this.firstName = firstName;
		this.lastName = lastName;
		this.permissions = permissions;
	}
	
	public User(){
	
	}
	

	public int getId() {
		return id;
	}
	
	public IntegerProperty getIdProperty(){
		SimpleIntegerProperty idProperty = new SimpleIntegerProperty(id);
		return idProperty;
	}

	public String getLogin() {
		return login;
	}

	public String getFirstName() {
		return firstName;
	}
	
	public StringProperty getFirstNameProperty(){
		SimpleStringProperty firstNameProperty = new SimpleStringProperty(firstName);
		return firstNameProperty;
	}

	public String getLastName() {
		return lastName;
	}
	
	public StringProperty getLastNameProperty(){
		SimpleStringProperty lastNameProperty = new SimpleStringProperty(lastName);
		return lastNameProperty;
	}

	public String getPassword() {
		return password;
	}

	public String getPermissions() {
		return permissions;
	}

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

	public void setLogin(String login) {
		this.login = login;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setPermissions(String permissions) {
		this.permissions = permissions;
	}
	 
	public Set<Project> getProjects() {
	    return projects;
	}
	
	@Override
	public boolean equals(Object other){
		if (other == this) return true;
		if (other == null) return false;
		if (getClass() != other.getClass()) return false;
		User user = (User) other;
		
		return 
				getLogin().equals(user.getLogin()) &&
				getPassword().equals(user.getPassword()) &&
				getFirstName().equals(user.getFirstName()) &&
				getLastName().equals(user.getLastName()) &&
				getPermissions().equals(user.getPermissions());

				
	}
	
	@Override
	public int hashCode(){
		return (int) (id*Math.random()*13*lastName.hashCode());
		
	}
	
}
@@ -2659,24 +2659,27 @@
 ects;%0A%09%7D%0A%09%0A%09
+%0A%09%0A
 @Override%0A%09p
@@ -3127,16 +3127,18 @@
 %09%09%0A%09%7D%0A%09%0A
+%09%0A
 %09@Overri
@@ -3166,70 +3166,531 @@
 de()
+ 
 %7B%0A%09%09
-return (int) (id*Math.random()*13*lastName.hashCode());%0A%09%09
+final int prime = 31;%0A%09%09int result = 1;%0A%09%09result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());%0A%09%09result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());%0A%09%09result = prime * result + ((login == null) ? 0 : login.hashCode());%0A%09%09result = prime * result + ((password == null) ? 0 : password.hashCode());%0A%09%09result = prime * result + ((permissions == null) ? 0 : permissions.hashCode());%0A%09%09return result;%0A%09%7D%0A%0A%0A%09@Override%0A%09public String toString()%7B%0A%09%09return firstName + %22 %22 + lastName;
 %0A%09%7D%0A

change method: "hashCode"