package com.app.identity;

import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import io.jsonwebtoken.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.app.model.user.Role;
import com.app.model.user.User;

@Service
public class TokenUtil {

    //private static final long VALIDITY_TIME_MS = 10 * 24 * 60 * 60 * 1000;// 10 days Validity
    private static final long VALIDITY_TIME_MS =  2 * 60 * 60 * 1000; // 2 hours  validity
    private static final String AUTH_HEADER_NAME = "Authorization";

    private String secret="mrin";

    public Optional<Authentication> verifyToken(HttpServletRequest request) {
      final String token = request.getHeader(AUTH_HEADER_NAME);

      if (token != null && !token.isEmpty()){
        final TokenUser user = parseUserFromToken(token.replace("Bearer","").trim());
        if (user != null) {
            return  Optional.of(new UserAuthentication(user));
        }
      }
      return Optional.empty();

    }

    //Get User Info from the Token
    public TokenUser parseUserFromToken(String token){

        Claims claims = Jwts.parser()
            .setSigningKey(secret)
            .parseClaimsJws(token)
            .getBody();

        User user = new User();
        user.setUserId( (String)claims.get("userId"));
        //user.setCustomerId((Integer)claims.get("customerId"));
        //user.setRole((String)claims.get("role"));
        user.setRole(Role.valueOf((String)claims.get("role")));
        return new TokenUser(user);
    }

    public String createTokenForUser(TokenUser tokenUser) {
      return createTokenForUser(tokenUser.getUser());
    }

    public String createTokenForUser(User user) {
      return Jwts.builder()
        .setExpiration(new Date(System.currentTimeMillis() + VALIDITY_TIME_MS))
        .setSubject(user.getFullName())
        .claim("userId", user.getUserId())
        .claim("role", user.getRole().toString())
        .signWith(SignatureAlgorithm.HS256, secret)
        .compact();
    }

}

@@ -1361,26 +1361,24 @@
         
-//
 user.set
 Customer
@@ -1373,77 +1373,25 @@
 .set
-CustomerId((Integer)claims.get(%22customerId%22));%0A        //user.setRole
+Role(Role.valueOf
 ((St
@@ -1414,16 +1414,17 @@
 %22role%22))
+)
 ;%0A      
@@ -1429,99 +1429,157 @@
     
+if (
 user.
-setRole(Role.valueOf((String)claims.get(%22role%22)));%0A        return new TokenUser(user);
+getUserId() != null && user.getRole() != null) %7B%0A            return new TokenUser(user);%0A        %7D else %7B%0A            return null;%0A        %7D
 %0A   

test if token contain all key (all claims)