package org.knowm.xchange.upbit.service;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import javax.ws.rs.QueryParam;

import si.mazi.rescu.ParamsDigest;
import si.mazi.rescu.RestInvocation;

public class UpbitJWTDigest implements ParamsDigest {
    private String accessKey;
    private String secretKey;

    private UpbitJWTDigest(String accessKey, String secretKey) throws IllegalArgumentException {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    public static UpbitJWTDigest createInstance(String accessKey, String secretKey) {
        return new UpbitJWTDigest(accessKey, secretKey);
    }

    @Override
    public String digestParams(RestInvocation restInvocation) {
        String queryString = "";
        if (restInvocation.getParamsMap().get(QueryParam.class) != null
                && !restInvocation.getParamsMap().get(QueryParam.class).isEmpty()) {
            queryString = String.valueOf(restInvocation.getParamsMap().get(QueryParam.class));
        } else if (restInvocation.getRequestBody() != null
                && !restInvocation.getRequestBody().isEmpty()) {
            ObjectMapper mapper = new ObjectMapper();
            try {
                Map<String, String> map = mapper.readValue(restInvocation.getRequestBody(), Map.class);
                Iterator it = map.keySet().iterator();
                while (it.hasNext()) {
                    String key = (String) it.next();
                    String value = map.get(key);
                    queryString += "&" + key + "=" + value;
                }
                queryString = queryString.substring(1);
            } catch (IOException e) {
            }
        }
        Algorithm algorithm = Algorithm.HMAC256(secretKey);
        JWTCreator.Builder builder = JWT.create();
        if (queryString.length() > 0) {
            builder
                    .withClaim("access_key", accessKey)
                    .withClaim("nonce", String.valueOf(new Date().getTime()))
                    .withClaim("query", queryString);
        } else {
            builder
                    .withClaim("access_key", accessKey)
                    .withClaim("nonce", String.valueOf(new Date().getTime()));
        }
        String jwtToken = builder.sign(algorithm);

        return "Bearer " + jwtToken;
    }
}

@@ -193,37 +193,81 @@
 er;%0A
-%0A
 import 
-java.io.IOExcep
+si.mazi.rescu.ParamsDigest;%0Aimport si.mazi.rescu.RestInvoca
 tion;%0A
+%0A
 impo
@@ -277,45 +277,54 @@
 java
-.util.Date;%0Aimport java.util.Iterator
+x.ws.rs.QueryParam;%0Aimport java.io.IOException
 ;%0Aim
@@ -338,19 +338,20 @@
 va.util.
-Map
+Date
 ;%0Aimport
@@ -359,99 +359,44 @@
 java
-x.ws.rs.QueryParam;%0A%0Aimport si.mazi.rescu.ParamsDigest;%0Aimport si.mazi.rescu.RestInvocation
+.util.Iterator;%0Aimport java.util.Map
 ;%0A%0Ap
@@ -1862,24 +1862,75 @@
 eption e) %7B%0A
+                throw new IllegalStateException();%0A
             

throw IllegalStateException.