package org.datacite.mds.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;
import org.datacite.mds.domain.AllocatorOrDatacentre;
import org.datacite.mds.service.MagicAuthStringService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class MagicAuthStringServiceImpl implements MagicAuthStringService {
    Logger log4j = Logger.getLogger(MagicAuthStringServiceImpl.class);

    @Value("${salt.magicAuthString}")
    String salt;

    private String getBaseAuthString(AllocatorOrDatacentre user) {
        if (user == null)
            return null;
        
        String baseAuthString = user.getBaseAuthString();
        log4j.debug("base auth string for " + user.getSymbol() + ": " + baseAuthString);
        return baseAuthString;
    }

    private String saltAndHash(String baseAuth, Date date) {
        if (StringUtils.isEmpty(baseAuth)) {
            return null;
        }
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        // DateFormat df = new SimpleDateFormat("yyyyMMddhhmm");
        String rawAuth = baseAuth + ":" + df.format(date) + ":" + this.salt;
        String hashedAuth = DigestUtils.sha256Hex(rawAuth);
        log4j.debug("saltAndHash: " + hashedAuth + " <- " + rawAuth);
        return hashedAuth;
    }

    public Collection<String> getValidAuthStrings(AllocatorOrDatacentre user) {
        List<String> list = new ArrayList<String>();
        if (user == null)
            return list;
        
        Date curDate = new Date();
        Date prevDate = DateUtils.addDays(curDate, -1);
        // Date prevDate = DateUtils.addMinutes(curDate, -1);
        String baseAuthString = getBaseAuthString(user);
        if (baseAuthString != null) {
            list.add(saltAndHash(baseAuthString, curDate));
            list.add(saltAndHash(baseAuthString, prevDate));
        }
        log4j.debug("valid auth strings for " + user.getSymbol() + ": " + list);
        return list;
    }

    public String getCurrentAuthString(AllocatorOrDatacentre user) {
        return saltAndHash(getBaseAuthString(user), new Date());
    }

    public boolean isValidAuthString(AllocatorOrDatacentre user, String auth) {
        return user != null && !StringUtils.isEmpty(auth) && // 
                getValidAuthStrings(user).contains(auth);
    }
}

@@ -810,320 +810,8 @@
 t;%0A%0A
-    private String getBaseAuthString(AllocatorOrDatacentre user) %7B%0A        if (user == null)%0A            return null;%0A        %0A        String baseAuthString = user.getBaseAuthString();%0A        log4j.debug(%22base auth string for %22 + user.getSymbol() + %22: %22 + baseAuthString);%0A        return baseAuthString;%0A    %7D%0A%0A
     
@@ -863,24 +863,24 @@
 ate date) %7B%0A
+
         if (
@@ -1009,73 +1009,8 @@
 %22);%0A
-        // DateFormat df = new SimpleDateFormat(%22yyyyMMddhhmm%22);%0A
     
@@ -1434,17 +1434,9 @@
 st;%0A
-        
 %0A
+
     
@@ -1526,69 +1526,8 @@
 1);%0A
-        // Date prevDate = DateUtils.addMinutes(curDate, -1);
 %0A   
@@ -1555,16 +1555,21 @@
 tring = 
+user.
 getBaseA
@@ -1582,57 +1582,11 @@
 ing(
-user
 );%0A
-        if (baseAuthString != null) %7B%0A    
     
@@ -1633,28 +1633,24 @@
  curDate));%0A
-    
         list
@@ -1694,33 +1694,32 @@
 Date));%0A        
-%7D
 %0A        log4j.d
@@ -1873,32 +1873,100 @@
 acentre user) %7B%0A
+        if (user == null)%0A            return null;%0A        else%0A    
         return s
@@ -1976,16 +1976,21 @@
 AndHash(
+user.
 getBaseA
@@ -1999,20 +1999,16 @@
 hString(
-user
 ), new D
@@ -2098,24 +2098,24 @@
 ing auth) %7B%0A
+
         retu
@@ -2121,74 +2121,8 @@
 urn 
-user != null && !StringUtils.isEmpty(auth) && // %0A                
 getV

simplify code