package org.fedorahosted.flies.util;

import java.util.List;

import javax.persistence.EntityManager;

import org.fedorahosted.flies.core.dao.AccountDAO;
import org.fedorahosted.flies.core.dao.IdentityDAO;
import org.fedorahosted.flies.core.model.HAccount;
import org.fedorahosted.flies.core.model.HPerson;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Observer;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.log.Log;

/**
 * Ensures that flies has at least one admin user
 *
 * @author Sean Flanigan
 */
@Name("essentialDataCreator")
@Scope(ScopeType.APPLICATION)
@Install(false)
public class EssentialDataCreator {

    @Logger
    private static Log log;
    
    @In
    private EntityManager entityManager;
   
    @In
    private AccountDAO accountDAO;
    
    @In("identityDAO")
    private IdentityDAO identityManager;
    
    private boolean prepared;
    
//    @In
//    private IdentityManager identityManager;
    
    public String username;
    public String password;
    public String email;
    public String name;

    // Do it when the application starts (but after everything else has been loaded)
    @Observer("org.jboss.seam.postInitialization")
    @Transactional
    public void prepare() {
        if (!prepared) {
        	boolean adminExists;
        	if (identityManager.roleExists("admin")) {
        		List<?> adminUsers = identityManager.listMembers("admin");
        		adminExists = !adminUsers.isEmpty();
        	} else {
        		log.info("Creating 'admin' role");
        		if (!identityManager.createRole("admin")) {
        			throw new RuntimeException("Couldn't create 'admin' role");
        		}
        		adminExists = false;
        	}
        	if (!identityManager.roleExists("user")) {
        		log.info("Creating 'user' role");
        		if (!identityManager.createRole("user")) {
        			throw new RuntimeException("Couldn't create 'user' role");
        		}
        	}
        	if (!adminExists) {
        		log.info("No admin users found: creating default user 'admin'");
        		
        		if (!identityManager.createUser(username, password)) {
        			throw new RuntimeException("Couldn't create 'admin' user");
        		}
        		identityManager.grantRole(username, "admin");            
                identityManager.grantRole(username, "user");            
            	HAccount account = accountDAO.getByUsername(username);
            	HPerson person = new HPerson();
            	
            	person.setAccount(account);
            	person.setEmail(email);
            	person.setName(name);
            	entityManager.persist(person);
        	}
        	
	        prepared = true;
        } 
//        Events.instance().raiseEvent(IMPORT_COMPLETE_EVENT);
    }

/*
 * Create the following data:
    create admin user:
	<HAccount id="1"
          	  creationDate="2009-01-14 11:39:00"
                  lastChanged="2009-01-14 11:39:00"
                  apiKey="12345678901234567890123456789012"
                  enabled="TRUE"
                  passwordHash="Eyox7xbNQ09MkIfRyH+rjg=="
                  username="admin"
        />
        
        create admin role:
        <HAccountRole id="1"
                      conditional="FALSE"
                      name="admin"
        />
        create user role:
        <HAccountRole id="2"
                      conditional="FALSE" 
                      name="user"
        />

		admin user is in role admin:

        <HAccountMembership accountId="1" 
                            memberOf="1"
        />
         
        admin role includes user role:
	
        <HAccountRoleGroup roleId="1" 
                           memberOf="2"
        />

		create admin person, related to admin account
        <HPerson id="1"
                 creationDate="2009-01-14 11:39:00"
                 lastChanged="2009-01-14 11:39:00"
                 email="asgeirf@localhost"
                 name="Administrator"
                 accountId="1"
        />
    
 */
    
    
}

@@ -1517,16 +1517,258 @@
 Exists;%0A
+        %09if (!identityManager.roleExists(%22user%22)) %7B%0A        %09%09log.info(%22Creating 'user' role%22);%0A        %09%09if (!identityManager.createRole(%22user%22)) %7B%0A        %09%09%09throw new RuntimeException(%22Couldn't create 'user' role%22);%0A        %09%09%7D%0A        %09%7D%0A
         
@@ -1803,32 +1803,32 @@
 sts(%22admin%22)) %7B%0A
-
         %09%09List%3C?
@@ -2031,32 +2031,40 @@
 eateRole(%22admin%22
+, %22user%22
 )) %7B%0A        %09%09%09
@@ -2127,32 +2127,32 @@
 %22);%0A        %09%09%7D%0A
+
         %09%09adminE
@@ -2181,250 +2181,8 @@
  %09%7D%0A
-        %09if (!identityManager.roleExists(%22user%22)) %7B%0A        %09%09log.info(%22Creating 'user' role%22);%0A        %09%09if (!identityManager.createRole(%22user%22)) %7B%0A        %09%09%09throw new RuntimeException(%22Couldn't create 'user' role%22);%0A        %09%09%7D%0A        %09%7D%0A
     

Make role 'admin' a member of role 'user'