package com.google.sps.data;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Entity;
import java.util.ArrayList;
import java.util.List;
import com.google.sps.service.DatabaseService;

public class Room {
    public static final String ROOM_ENTITY_NAME = "Room";
    public static final String TITLE_PROPERTY_KEY = "title";
    public static final String DESCRIPTION_PROPERTY_KEY = "description";
    public static final String HOST_PROPERTY_KEY = "host";
    public static final String FOLLOWERS_PROPERTY_KEY = "followers";
    public static final String FORMS_PROPERTY_KEY = "forms";
    

    private Entity entity;

    public Room(Entity entity) {
        this.entity = entity;
    }

    public Room(User host, String title, String description) { 
        this.entity = new Entity(Room.ROOM_ENTITY_NAME);
        this.entity.setProperty(Room.TITLE_PROPERTY_KEY, title);
        this.entity.setProperty(Room.DESCRIPTION_PROPERTY_KEY, description);
        this.entity.setProperty(Room.HOST_PROPERTY_KEY, host.getUserKey());
        this.entity.setProperty(Room.FOLLOWERS_PROPERTY_KEY, null);
        this.entity.setProperty(Room.FORMS_PROPERTY_KEY, null);
    }

    public Entity getRoomEntity() {
        return this.entity;
    }

    public Key getHost() {
        return (Key) this.entity.getProperty(Room.HOST_PROPERTY_KEY);
    }

    public void setHost(User host) { 
        this.entity.setProperty(Room.HOST_PROPERTY_KEY, host.getUserKey());
    }

    public String getTitle() {
        return (String) this.entity.getProperty(Room.TITLE_PROPERTY_KEY);
    }

    public void setTitle(String title){
        this.entity.setProperty(Room.TITLE_PROPERTY_KEY, title);
    }

    @SuppressWarnings("unchecked")
    public List<Key> getAllFollowers() {
        return (ArrayList<Key>) this.entity.getProperty(Room.FOLLOWERS_PROPERTY_KEY);
    }

    @SuppressWarnings("unchecked")
    public List<Key> getAllForms() { 
        return (ArrayList<Key>) this.entity.getProperty(Room.FORMS_PROPERTY_KEY); 
    }
    
    @SuppressWarnings("unchecked")
    public void addFollower(User follower) {
        List<Key> followers = (ArrayList<Key>) this.entity.getProperty(Room.FOLLOWERS_PROPERTY_KEY);
        if(followers == null){
            followers = new ArrayList<>();
        }
        followers.add(follower.getUserKey());
        this.entity.setProperty(Room.FOLLOWERS_PROPERTY_KEY, followers);
    }

    @SuppressWarnings("unchecked")
    public void removeFollower(User follower) {
        List<Key> followers = (ArrayList<Key>) this.entity.getProperty(Room.FOLLOWERS_PROPERTY_KEY);
        if(followers == null){
            return;
        } else {
            followers.remove(follower.getUserKey());
            this.entity.setProperty(Room.FOLLOWERS_PROPERTY_KEY, followers);
        }
    }

    @SuppressWarnings("unchecked")
    public void addForm(Form form) {
        List<Key> forms = (ArrayList<Key>) this.entity.getProperty(Room.FORMS_PROPERTY_KEY);
        if(forms == null){
            forms = new ArrayList<>();
        }
        forms.add(form.getFormKey());
        this.entity.setProperty(Room.FORMS_PROPERTY_KEY, forms);
    }

    @SuppressWarnings("unchecked")
    public void removeForm(Form form) {
        List<Key> forms = (ArrayList<Key>) this.entity.getProperty(Room.FORMS_PROPERTY_KEY);
        if(forms == null){
            return;
        } else {
            forms.remove(form.getFormKey());
            this.entity.setProperty(Room.FORMS_PROPERTY_KEY, forms);
        }
    }

    @SuppressWarnings("unchecked")
    public boolean isFollowerInRoom(User follower) {
        List<Key> followers = (ArrayList<Key>) this.entity.getProperty(Room.FOLLOWERS_PROPERTY_KEY);
        return (!(followers.lastIndexOf(follower.getUserKey()) == -1));
    }
}
@@ -562,74 +562,8 @@
 rs%22;
-%0A    public static final String FORMS_PROPERTY_KEY = %22forms%22;%0A    
 %0A%0A  
@@ -1001,140 +1001,8 @@
 ));%0A
-        this.entity.setProperty(Room.FOLLOWERS_PROPERTY_KEY, null);%0A        this.entity.setProperty(Room.FORMS_PROPERTY_KEY, null);%0A
     
@@ -1695,171 +1695,8 @@
    %7D
-%0A%0A    @SuppressWarnings(%22unchecked%22)%0A    public List%3CKey%3E getAllForms() %7B %0A        return (ArrayList%3CKey%3E) this.entity.getProperty(Room.FORMS_PROPERTY_KEY); %0A    %7D
 %0A   
@@ -1789,47 +1789,12 @@
     
-List%3CKey%3E followers = (ArrayList%3CKey%3E) 
+if (
 this
@@ -1841,38 +1841,16 @@
 RTY_KEY)
-;%0A        if(followers
  == null
@@ -1842,32 +1842,33 @@
 TY_KEY) == null)
+ 
 %7B%0A            fo
@@ -1869,257 +1869,93 @@
     
-followers = new ArrayList%3C%3E();%0A        %7D%0A        followers.add(follower.getUserKey());%0A        this.entity.setProperty(Room.FOLLOWERS_PROPERTY_KEY, followers);%0A    %7D%0A%0A    @SuppressWarnings(%22unchecked%22)%0A    public void removeFollower(User follower) %7B
+this.entity.setProperty(Room.FOLLOWERS_PROPERTY_KEY, new ArrayList%3CKey%3E());%0A        %7D
 %0A   
@@ -1951,32 +1951,37 @@
       %7D%0A        
+Array
 List%3CKey%3E follow
@@ -2061,80 +2061,8 @@
 Y);%0A
-        if(followers == null)%7B%0A            return;%0A        %7D else %7B%0A    
     
@@ -2075,22 +2075,19 @@
 llowers.
-remove
+add
 (followe
@@ -2111,213 +2111,106 @@
     
-        this.entity.setProperty(Room.FOLLOWERS_PROPERTY_KEY, followers);%0A        %7D%0A    %7D%0A%0A    @SuppressWarnings(%22unchecked%22)%0A    public void addForm(Form form) %7B%0A        List%3CKey%3E forms = (ArrayList%3CKey%3E) 
+%7D%0A%0A    @SuppressWarnings(%22unchecked%22)%0A    public void removeFollower(User follower) %7B%0A        if (
 this
@@ -2228,34 +2228,38 @@
 Property(Room.FO
+LLOWE
 R
-M
 S_PROPERTY_KEY);
@@ -2261,124 +2261,24 @@
 KEY)
-;%0A        if(forms == null)%7B%0A            forms = new ArrayList%3C%3E();%0A        %7D%0A        forms.add(form.getFormKey());%0A
+ == null) %7B%0A    
     
@@ -2304,34 +2304,38 @@
 Property(Room.FO
+LLOWE
 R
-M
 S_PROPERTY_KEY, 
@@ -2338,97 +2338,40 @@
 EY, 
-forms);%0A    %7D%0A%0A    @SuppressWarnings(%22unchecked%22)%0A    public void removeForm(Form form) %7B
+new ArrayList%3CKey%3E());%0A        %7D
 %0A   
@@ -2367,32 +2367,37 @@
       %7D%0A        
+Array
 List%3CKey%3E forms 
@@ -2392,18 +2392,22 @@
 %3CKey%3E fo
+llowe
 r
-m
 s = (Arr
@@ -2442,34 +2442,38 @@
 Property(Room.FO
+LLOWE
 R
-M
 S_PROPERTY_KEY);
@@ -2485,187 +2485,48 @@
     
-if(forms == null)%7B%0A            return;%0A        %7D else %7B%0A            forms.remove(form.getFormKey());%0A            this.entity.setProperty(Room.FORMS_PROPERTY_KEY, forms);%0A        %7D
+followers.remove(follower.getUserKey());
 %0A   

Change add/remove followers to properly reference arrayLists