package models;

import helpers.UserService;
import org.hibernate.validator.constraints.Email;
import play.db.ebean.Model;
import utils.MembershipLevel;

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

@Entity
public class User extends Model {

    @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 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;
    }

    public boolean isAdmin() {
        if (SuperUser.find.all().isEmpty()) {
            return false;
        }
        SuperUser su = SuperUser.find.byId(new SuperUser(this).key);
        if (su == null) {
            return false;
        }
        if (!su.user.equals(this)) {
            for (Membership mem : this.memberships) {
                if (mem.level == MembershipLevel.COUNCIL) {
                    return true;
                }
            }
        } else {
            return true;
        }
        return false;
    }

    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";
            }
        }
    }
}

@@ -3077,32 +3077,81 @@
     
-if (SuperUser.find.all()
+List%3CSuperUser%3E superUsers = SuperUser.find.all();%0A        if (superUsers
 .isE
@@ -3187,32 +3187,39 @@
 false;%0A        %7D
+ else %7B
 %0A        SuperUs
@@ -3211,16 +3211,25 @@
         
+    for (
 SuperUse
@@ -3236,65 +3236,46 @@
 r su
- = Su
 perUser
-.find.byId(new SuperUser(this).key);%0A
+ : superUsers) %7B%0A      
         
 if (
@@ -3274,26 +3274,53 @@
     
+  
 if (su
- == null) %7B%0A
+perUser.user.equals(this)) %7B%0A        
     
@@ -3326,39 +3326,46 @@
          return 
-false;%0A
+true;%0A        
         %7D%0A      
@@ -3370,41 +3370,24 @@
     
-if (!su.user.equals(this)) %7B%0A
+    %7D%0A   
     
+ %7D%0A
     
@@ -3440,28 +3440,24 @@
             
-  
-  
 if (mem.leve
@@ -3496,36 +3496,32 @@
                 
-    
 return true;%0A   
@@ -3533,69 +3533,9 @@
     
-    %7D%0A            %7D%0A        %7D else %7B%0A            return true;
+%7D
 %0A   

Fix pseudo super users not being labelled as super users