package de.fau.amos.virtualledger.server.banking.adorsys.api.bankAccountEndpoint;

import de.fau.amos.virtualledger.server.banking.adorsys.api.BankingApiUrlProvider;
import de.fau.amos.virtualledger.server.banking.adorsys.api.json.BankAccountJSONBankingModel;
import de.fau.amos.virtualledger.server.banking.adorsys.api.json.BankAccountSyncJSONBankingModel;
import de.fau.amos.virtualledger.server.banking.model.BankAccountBankingModel;
import de.fau.amos.virtualledger.server.banking.model.BankingException;
import de.fau.amos.virtualledger.server.banking.model.BookingModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.List;

@Component

@Qualifier("default")
public class HttpBankAccountEndpoint implements BankAccountEndpoint {
    private static final int HTTP_OK = 200;

    private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

    private final BankingApiUrlProvider urlProvider;

    @Autowired
    public HttpBankAccountEndpoint(BankingApiUrlProvider urlProvider) {
        this.urlProvider = urlProvider;
    }

    @Override
    public List<BankAccountBankingModel> getBankAccounts(String userId, String bankingAccessId)
            throws BankingException {

        // Create Jersey client
        Client client = ClientBuilder.newClient();

        String url = urlProvider.getBankAccountEndpointUrl(userId, bankingAccessId);
        WebTarget webTarget = client.target(url);
        Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON_TYPE);
        Response response = invocationBuilder.get();

        if (response.getStatus() != HTTP_OK) {
            LOGGER.warn("No connection to Adorsys Server!");
            throw new BankingException("No connection to Adorsys Server!");
        }
        BankAccountJSONBankingModel reponseModel = response.readEntity(BankAccountJSONBankingModel.class);
        if (reponseModel == null || reponseModel.getEmbedded() == null) {
            LOGGER.info("No accounts found");
            return new ArrayList<>();
        }
        return reponseModel.getEmbedded().getBankAccountEntityList();
    }

    @Override
    public List<BookingModel> syncBankAccount(String userId, String bankAccessId, String bankAccountId, String pin)
            throws BankingException {

        // Create Jersey client
        Client client = ClientBuilder.newClient();

        String url = urlProvider.getBankAccountSyncEndpointUrl(userId, bankAccessId, bankAccountId);
        WebTarget webTarget = client.target(url);
        Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON_TYPE);
        Response response = invocationBuilder.put(Entity.entity(pin, MediaType.TEXT_PLAIN_TYPE));

        if (response.getStatus() != HTTP_OK) {
            LOGGER.warn("No connection to Adorsys Server!");
            throw new BankingException("No connection to Adorsys Server!");
        }
        BankAccountSyncJSONBankingModel responseModel = response.readEntity(BankAccountSyncJSONBankingModel.class);
        if (responseModel == null || responseModel.getEmbedded() == null) {
            LOGGER.warn("No bookings found");
            return new ArrayList<>();
        }
        return responseModel.getEmbedded().getBookingEntityList();

    }

}

@@ -1505,16 +1505,22 @@
 ndpoint(
+final 
 BankingA
@@ -1666,16 +1666,22 @@
 ccounts(
+final 
 String u
@@ -1678,32 +1678,38 @@
 l String userId,
+ final
  String bankingA
@@ -1788,32 +1788,38 @@
  client%0A        
+final 
 Client client = 
@@ -1846,32 +1846,38 @@
 ent();%0A%0A        
+final 
 String url = url
@@ -1937,32 +1937,38 @@
 essId);%0A        
+final 
 WebTarget webTar
@@ -1993,32 +1993,38 @@
 t(url);%0A        
+final 
 Invocation.Build
@@ -2098,32 +2098,38 @@
 _TYPE);%0A        
+final 
 Response respons
@@ -2352,32 +2352,38 @@
       %7D%0A        
+final 
 BankAccountJSONB
@@ -2396,16 +2396,17 @@
 Model re
+s
 ponseMod
@@ -2480,16 +2480,17 @@
   if (re
+s
 ponseMod
@@ -2505,16 +2505,17 @@
 ll %7C%7C re
+s
 ponseMod
@@ -2653,16 +2653,17 @@
 eturn re
+s
 ponseMod
@@ -2774,16 +2774,22 @@
 Account(
+final 
 String u
@@ -2794,16 +2794,22 @@
  userId,
+ final
  String 
@@ -2821,16 +2821,22 @@
 ccessId,
+ final
  String 
@@ -2849,16 +2849,22 @@
 countId,
+ final
  String 
@@ -2947,16 +2947,22 @@
         
+final 
 Client c
@@ -3005,16 +3005,22 @@
         
+final 
 String u
@@ -3112,16 +3112,22 @@
         
+final 
 WebTarge
@@ -3168,16 +3168,22 @@
         
+final 
 Invocati
@@ -3273,16 +3273,22 @@
         
+final 
 Response
@@ -3572,16 +3572,22 @@
         
+final 
 BankAcco

Clean up HttpBankAccountEndpoint.java