package models;

import com.feth.play.module.pa.user.AuthUser;
import com.feth.play.module.pa.user.EmailIdentity;
import com.feth.play.module.pa.user.FirstLastNameIdentity;
import com.feth.play.module.pa.user.PicturedIdentity;
import org.hibernate.validator.constraints.Email;
import services.UserService;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
public class User {

    @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "user")
    private List<Membership> memberships = new ArrayList<>();
    @Id
    private String id;
    private String firstName;
    private String lastName;
    @Email
    private String email;
    private String pictureUrl;
    @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "user")
    private List<Participation> participations = new ArrayList<>();
    @Transient
    private String gravatarUrl;

    public User(String id, String firstName, String lastName, Gender gender, String email, String picureUrl) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.pictureUrl = picureUrl;
    }

    public User(final AuthUser authUser) {
        this.id = authUser.getId();
        if (authUser instanceof EmailIdentity) {
            EmailIdentity identity = (EmailIdentity) authUser;
            this.email = identity.getEmail();
        }
        if(authUser instanceof FirstLastNameIdentity) {
            FirstLastNameIdentity identity = (FirstLastNameIdentity) authUser;
            this.firstName = identity.getFirstName();
            this.lastName = identity.getLastName();
        }
        if(authUser instanceof PicturedIdentity) {
            PicturedIdentity identity = (PicturedIdentity) authUser;
            this.pictureUrl = identity.getPicture();
        }
    }

    public static List<String> genderAsList() {
        ArrayList<String> list = new ArrayList<>();

        for (Gender gender : Gender.values()) {
            list.add(gender.toString());
        }
        return list;
    }

    @PostLoad
    public void onPostLoad() {
        UserService.setupGravatar(this, this.pictureUrl);
    }

    public List<Membership> getMemberships() {
        return memberships;
    }

    public void setMemberships(List<Membership> memberships) {
        this.memberships = memberships;
    }

    public String getId() {
        return id;
    }

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

    public String getFirstName() {
        return firstName;
    }

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

    public String getLastName() {
        return lastName;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPictureUrl() {
        if (gravatarUrl == null) {
            return this.pictureUrl;
        }
        return this.getGravatarUrl();
    }

    public void setPictureUrl(String pictureUrl) {
        this.pictureUrl = pictureUrl;
    }

    public List<Participation> getParticipations() {
        return participations;
    }

    public void setParticipations(List<Participation> participations) {
        this.participations = participations;
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;

        final User user = (User) o;

        return id.equalsIgnoreCase(user.id);

    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + id.hashCode();
        return result;
    }

    private String getGravatarUrl() {
        return gravatarUrl;
    }

    public void setGravatarUrl(String gravatarUrl) {
        this.gravatarUrl = gravatarUrl;
    }

    public enum Gender {
        MALE, FEMALE, UNSPECIFIED;

        @Override
        public String toString() {
            switch (this) {
                case MALE:
                    return "Mann";
                case FEMALE:
                    return "Kvinne";
                case UNSPECIFIED:
                    return "Ukjent/Uspesifisert";
                default:
                    return "Ukjent/Uspesifisert";
            }
        }
    }
}

@@ -938,23 +938,8 @@
 ame,
- Gender gender,
  Str
@@ -961,32 +961,32 @@
 ng picureUrl) %7B%0A
+
         this.id 
@@ -1382,32 +1382,33 @@
     %7D%0A        if
+ 
 (authUser instan
@@ -1642,16 +1642,17 @@
       if
+ 
 (authUse
@@ -1827,236 +1827,8 @@
  %7D%0A%0A
-    public static List%3CString%3E genderAsList() %7B%0A        ArrayList%3CString%3E list = new ArrayList%3C%3E();%0A%0A        for (Gender gender : Gender.values()) %7B%0A            list.add(gender.toString());%0A        %7D%0A        return list;%0A    %7D%0A%0A
     
@@ -3247,16 +3247,30 @@
 is == o)
+ %7B%0A           
  return 
@@ -3275,16 +3275,26 @@
 n true;%0A
+        %7D%0A
         
@@ -3342,51 +3342,21 @@
 ()) 
-return false;%0A        if (!super.equals(o))
+%7B%0A           
  ret
@@ -3365,16 +3365,26 @@
 n false;
+%0A        %7D
 %0A%0A      
@@ -3746,24 +3746,24 @@
 vatarUrl) %7B%0A
+
         this
@@ -3799,467 +3799,7 @@
    %7D
-%0A%0A    public enum Gender %7B%0A        MALE, FEMALE, UNSPECIFIED;%0A%0A        @Override%0A        public String toString() %7B%0A            switch (this) %7B%0A                case MALE:%0A                    return %22Mann%22;%0A                case FEMALE:%0A                    return %22Kvinne%22;%0A                case UNSPECIFIED:%0A                    return %22Ukjent/Uspesifisert%22;%0A                default:%0A                    return %22Ukjent/Uspesifisert%22;%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

fix failing equals method