package com.celements.auth;

import java.util.HashMap;
import java.util.Map;

import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.Requirement;
import org.xwiki.context.Execution;

import com.celements.web.plugin.cmd.UserNameForUserDataCommand;
import com.celements.web.service.IWebUtilsService;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.objects.classes.PasswordClass;
import com.xpn.xwiki.user.api.XWikiUser;

@Component
public class AuthenticationService implements IAuthenticationServiceRole {
  
  @Requirement
  private IWebUtilsService webUtilsService;
  
  @Requirement
  private Execution execution;

  private XWikiContext getContext() {
    return (XWikiContext)execution.getContext().getProperty("xwikicontext");
  }
  
  @Override 
  public String getPasswordHash(String encoding, String str) {
    return new PasswordClass().getEquivalentPassword(encoding, str);
  }
  
  @Override 
  public Map<String, String> activateAccount(String activationCode) throws XWikiException{
    Map<String, String> userAccount = new HashMap<String, String>();
    String hashedCode = getPasswordHash("hash:SHA-512:", activationCode);
    String username = new UserNameForUserDataCommand().getUsernameForUserData(hashedCode,
        "validkey", getContext());
    
    if((username != null) && !username.equals("")){
      String password = getContext().getWiki().generateRandomString(24);
      
      XWikiDocument doc = getContext().getWiki().getDocument(
          webUtilsService.resolveDocumentReference(getContext().getUser()), getContext());
      
      BaseObject obj = doc.getXObject(webUtilsService.resolveDocumentReference(
          "XWiki.XWikiUsers"));

//      obj.set("validkey", "", context);
      obj.set("active", "1", getContext());
      obj.set("force_pwd_change", "1", getContext());
      obj.set("password", password, getContext());
      
      getContext().getWiki().saveDocument(doc, getContext());
      
      userAccount.put("username", username);
      userAccount.put("password", password);
    }
    
    return userAccount;
  }
  
  @Override 
  public XWikiUser checkAuth(String logincredential, String password,
      String rememberme, String possibleLogins, Boolean noRedirect) 
          throws XWikiException {
    String loginname = new UserNameForUserDataCommand().getUsernameForUserData(
        logincredential, possibleLogins, getContext());
    if ("".equals(loginname) && possibleLogins.matches("(.*,)?loginname(,.*)?")) {
        loginname = logincredential;
    }
    if (noRedirect != null) {
      getContext().put("ajax", noRedirect);
    }
    return getContext().getWiki().getAuthService().checkAuth(loginname, password, 
        rememberme, getContext());
  }
}

@@ -71,16 +71,73 @@
 l.Map;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -173,24 +173,24 @@
 .Component;%0A
-
 import org.x
@@ -263,16 +263,68 @@
 ecution;
+%0Aimport org.xwiki.model.reference.DocumentReference;
 %0A%0Aimport
@@ -381,16 +381,76 @@
 ommand;%0A
+import com.celements.web.service.CelementsWebScriptService;%0A
 import c
@@ -824,16 +824,117 @@
 ceRole %7B
+%0A%0A  private static Logger _LOGGER  = LoggerFactory.getLogger(%0A      CelementsWebScriptService.class);
 %0A  %0A  @R
@@ -1416,16 +1416,82 @@
 eption%7B%0A
+    _LOGGER.debug(%22activateAccount: for code %22 + activationCode);%0A
     Map%3C
@@ -1746,24 +1746,86 @@
 Context());%0A
+    _LOGGER.debug(%22activateAccount: username = %22 + username);%0A
     %0A    if(
@@ -1946,34 +1946,23 @@
 4);%0A
-      
 %0A      
-XWiki
 Document
  doc
@@ -1961,60 +1961,30 @@
 ment
- doc = getContext().getWiki().getDocument(%0A         
+Reference userDocRef =
  web
@@ -2028,24 +2028,35 @@
 (getContext(
+%0A          
 ).getUser())
@@ -2059,141 +2059,508 @@
 r())
-, getContext());%0A      %0A      BaseObject obj = doc.getXObject(webUtilsService.resolveDocumentReference(%0A          %22XWiki.XWikiUsers%22)
+;%0A      _LOGGER.debug(%22activateAccount: userDocRef = %22 + userDocRef);%0A      XWikiDocument doc = getContext().getWiki().getDocument(userDocRef, getContext());%0A      _LOGGER.debug(%22activateAccount: userDoc = %22 + doc);%0A      DocumentReference userObjRef = webUtilsService.resolveDocumentReference(%0A          %22XWiki.XWikiUsers%22);%0A      _LOGGER.debug(%22activateAccount: userObjRef = %22 + userObjRef);%0A      BaseObject obj = doc.getXObject(userObjRef);%0A      _LOGGER.debug(%22activateAccount: userObj = %22 + obj
 );%0A%0A

add debug logging for accountActivation()