package dev.openshift.tapestry.angular.services;

import dev.openshift.tapestry.angular.data.user.User;

import java.util.Date;
import java.util.HashMap;


public class UserDatabaseImpl implements UserDatabase
{
	public static HashMap<Integer, User> users = new HashMap<Integer, User>();


    public UserDatabaseImpl()
	{
		User userAdmin = new User();
        userAdmin.setId(1);
        userAdmin.setFirstName("user");
        userAdmin.setLastName("admin");
        userAdmin.setUri("/user-management/users/1");
        userAdmin.setLastModified(new Date());
        userAdmin.setEmail("admin@user.com");
        userAdmin.setPassword("password");
        userAdmin.setLogin("admin");
        String[] rolesAdmin = {"ADMIN", "USER"};
        userAdmin.setRoles(rolesAdmin);
		users.put(1, userAdmin);

        User user = new User();
        user.setId(2);
        user.setFirstName("simple");
        user.setLastName("user");
        user.setUri("/user-management/users/2");
        user.setLastModified(new Date());
        user.setEmail("simple.user@user.com");
        user.setPassword("password");
        user.setLogin("user");
        String[] roles =  {"USER"};
        user.setRoles(roles);
        users.put(2, user);


    }
	
	public  User getUserById(Integer id)
	{
		return users.get(id);
	}

    public User getUserByLogin(String login) {
        for (User user : users.values()) {
            if(user.getLogin().equals(login)) return user;
        }
        return null;
    }

    public  void updateUser(Integer id)
	{
		User user = users.get(id);
		user.setLastModified(new Date());
	}
	
	public  Date getLastModifiedById(Integer id)
	{
		return users.get(id).getLastModified();
	}
}

@@ -98,24 +98,53 @@
 er.User;%0D%0A%0D%0A
+import java.util.ArrayList;%0D%0A
 import java.
@@ -155,16 +155,16 @@
 .Date;%0D%0A
-
 import j
@@ -182,16 +182,40 @@
 shMap;%0D%0A
+import java.util.List;%0D%0A
 %0D%0A%0D%0Apubl
@@ -341,26 +341,48 @@
 , User%3E();%0D%0A
+    public int lastId;
 %0D%0A
-
 %0D%0A    public
@@ -777,32 +777,34 @@
 (%22admin%22);%0D%0A
+%0D%0A
         
 String%5B%5D rol
@@ -787,32 +787,36 @@
 %0D%0A%0D%0A        
+List%3C
 String
-%5B%5D
+%3E
  rolesAdmin 
@@ -821,25 +821,98 @@
 n = 
-%7B%22ADMIN%22, 
+new ArrayList%3CString%3E();%0D%0A        rolesAdmin.add(%22ADMIN%22);%0D%0A        rolesAdmin.add(
 %22USER%22
-%7D
+)
 ;%0D%0A 
@@ -1335,16 +1335,20 @@
     
+List%3C
 String
-%5B%5D
+%3E
  rol
@@ -1356,17 +1356,59 @@
 s = 
- %7B
+new ArrayList%3CString%3E();%0D%0A        roles.add(
 %22USER%22
-%7D
+)
 ;%0D%0A 
@@ -1468,18 +1468,37 @@
 er);%0D%0A%0D%0A
+        lastId = 2;
 %0D%0A
-
     %7D%0D%0A%09
@@ -1980,15 +1980,370 @@
 ied();%0D%0A
-
 %09%7D%0D
+%0A%0D%0A    public User add(User user) %7B%0D%0A        lastId++;%0D%0A        user.setId(lastId);%0D%0A        user.setUri(%22/user-management/users/X%22);%0D%0A        user.setLastModified(new Date());%0D%0A        List%3CString%3E roles = new ArrayList%3CString%3E();%0D%0A        roles.add(%22USER%22);%0D%0A        user.setRoles(roles);%0D%0A        users.put(lastId, user);%0D%0A        return  user;%0D%0A    %7D%0D
 %0A%7D%0D%0A

add method and apply change from user