/*
 * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.wso2.carbon.identity.samples.microprofile.jwt;

import org.eclipse.microprofile.jwt.JsonWebToken;

import javax.annotation.security.DeclareRoles;
import javax.annotation.security.RolesAllowed;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@RequestScoped
@DeclareRoles({"admin","ViewBalance", "Debtor", "Creditor"})
@Path("/")
public class SecureWalletEndpoint {
    private double currentBalance = 13492838.45;
    private String currency = "USD";

    @Inject
    private JsonWebToken jwt;

    @GET
    @Path("/balance")
    @Produces(MediaType.APPLICATION_JSON)
    @RolesAllowed({"admin", "ViewBalance", "Debtor"})
    public JsonObject getBalance() {
        return generateBalanceInfo();
    }

    @GET
    @Path("/debit")
    @Produces(MediaType.APPLICATION_JSON)
    @RolesAllowed({"admin","Debtor"})
    public Response debit(@QueryParam("amount") String amount) {
        Double debitAmount = Double.valueOf(amount);
        if (currentBalance > debitAmount) {
            currentBalance -= debitAmount;
        } else {
            return Response.status(Response.Status.BAD_REQUEST)
                    .entity("Not enough balance to debit: " + currency + " " + currentBalance).build();
        }
        return Response.ok(generateBalanceInfo()).build();
    }

    @GET
    @Path("/credit")
    @Produces(MediaType.APPLICATION_JSON)
    @RolesAllowed({"admin","Creditor"})
    public JsonObject credit(@QueryParam("amount") String amount) {
        currentBalance += Double.valueOf(amount);
        return generateBalanceInfo();
    }

    private JsonObject generateBalanceInfo() {
        JsonObjectBuilder result = Json.createObjectBuilder().add(currency, currentBalance);
        double warningLimit = 10000.00;

        if (warningLimit > currentBalance) {
            String warningMsg = String.format("balance is below warning limit: %s", warningLimit);
            result.add("warning", warningMsg);
        }

        return result.build();
    }
}

@@ -780,16 +780,45 @@
 Token;%0A%0A
+import java.math.BigDecimal;%0A
 import j
@@ -852,24 +852,24 @@
 clareRoles;%0A
-
 import javax
@@ -1383,22 +1383,26 @@
 private 
-double
+BigDecimal
  current
@@ -1411,16 +1411,32 @@
 lance = 
+new BigDecimal(%22
 13492838
@@ -1438,16 +1438,18 @@
 92838.45
+%22)
 ;%0A    pr
@@ -1985,16 +1985,30 @@
 tBalance
+.doubleValue()
  %3E debit
@@ -2048,22 +2048,57 @@
 nce 
--
 = 
-debitA
+currentBalance.subtract(new BigDecimal(a
 mount
+))
 ;%0A  
@@ -2479,26 +2479,24 @@
  public 
-JsonObject
+Response
  credit(
@@ -2561,33 +2561,51 @@
 nce 
-+
 = 
-Double.valueOf
+currentBalance.add(new BigDecimal
 (amount)
 ;%0A  
@@ -2592,32 +2592,33 @@
 gDecimal(amount)
+)
 ;%0A        return
@@ -2610,32 +2610,44 @@
 %0A        return 
+Response.ok(
 generateBalanceI
@@ -2643,32 +2643,41 @@
 ateBalanceInfo()
+).build()
 ;%0A    %7D%0A%0A    pri
@@ -2850,16 +2850,16 @@
 00.00;%0A%0A
-
         
@@ -2891,16 +2891,30 @@
 tBalance
+.doubleValue()
 ) %7B%0A    

Change balance variable type to BigDecimal