package de.fau.amos.virtualledger.android.api.banking;

import android.util.Log;

import java.util.List;

import de.fau.amos.virtualledger.android.api.Restapi;
import de.fau.amos.virtualledger.android.api.shared.CallWithToken;
import de.fau.amos.virtualledger.android.api.shared.RetrofitCallback;
import de.fau.amos.virtualledger.android.api.shared.TokenCallback;
import de.fau.amos.virtualledger.dtos.BankAccess;
import de.fau.amos.virtualledger.dtos.BankAccessCredential;
import de.fau.amos.virtualledger.dtos.BankAccountSync;
import de.fau.amos.virtualledger.dtos.BankAccountSyncResult;
import io.reactivex.Observable;
import io.reactivex.subjects.PublishSubject;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by sebastian on 14.05.17.
 */

public class HTTPBankingProvider implements BankingProvider {

    private static final String TAG = "HTTPBankingProvider";

    private Retrofit retrofit;
    private CallWithToken callWithToken;

    public HTTPBankingProvider(Retrofit retrofit, final CallWithToken callWithToken) {
        this.retrofit = retrofit;
        this.callWithToken = callWithToken;
    }

    @Override
    public Observable<List<BankAccess>> getBankingOverview() {
        final PublishSubject<List<BankAccess>> observable = PublishSubject.create();

        callWithToken.callWithToken(observable, new TokenCallback() {
            @Override
            public void onReceiveToken(final String token) {
                // got token
                retrofit.create(Restapi.class).getBankAccesses(token).enqueue(new RetrofitCallback<>(observable));
            }
        });

        return observable;
    }

    @Override
    public Observable<BankAccountSyncResult> getBankingTransactions(final List<BankAccountSync> bankAccountSyncList) {

        final PublishSubject<BankAccountSyncResult> observable = PublishSubject.create();

        callWithToken.callWithToken(observable, new TokenCallback() {
            @Override
            public void onReceiveToken(final String token) {
                // got token
                retrofit.create(Restapi.class).getBookings(token, bankAccountSyncList).enqueue(new RetrofitCallback<>(observable));
            }
        });

        return observable;
    }

    @Override
    public Observable<BankAccess> addBankAccess(final BankAccessCredential bankAccessCredential) {
        final PublishSubject<BankAccess> observable = PublishSubject.create();

        callWithToken.callWithToken(observable, new TokenCallback() {
            @Override
            public void onReceiveToken(final String token) {
                // got token
                retrofit.create(Restapi.class).addBankAccess(token, bankAccessCredential).enqueue(new RetrofitCallback<>(observable));
            }
        });


        return observable;
    }

    @Override
    public Observable<Void> deleteBankAccess(final String accessId) {
        final PublishSubject<Void> observable = PublishSubject.create();

        callWithToken.callWithToken(observable, new TokenCallback() {
            @Override
            public void onReceiveToken(final String token) {
                // got token
                final retrofit2.Call<Void> responseMessage = retrofit.create(Restapi.class).deleteBankAccess(token, accessId);

                responseMessage.enqueue(new Callback<Void>() {
                    @Override
                    public void onResponse(retrofit2.Call<Void> call, Response<Void> response) {
                        // got response from server
                        if (response.isSuccessful()) {
                            Log.v(TAG, "Deleting bank accesses was successful " + response.code());
                            observable.onNext(response.body());
                        } else {
                            Log.e(TAG, "Deleting bank accesses was not successful! ERROR " + response.code());
                            observable.onError(new Throwable("Deleting bank accesses was not successful!"));
                        }
                    }


                    @Override
                    public void onFailure(retrofit2.Call<Void> call, Throwable t) {
                        // response from server failed
                        Log.e(TAG, "No connection to server!");
                        observable.onError(new Throwable("No connection to server!"));
                    }
                });
            }
        });


        return observable;
    }

    @Override
    public Observable<Void> deleteBankAccount(final String accessId, final String accountId) {
        final PublishSubject<Void> observable = PublishSubject.create();

        callWithToken.callWithToken(observable, new TokenCallback() {
            @Override
            public void onReceiveToken(final String token) {
                // got token
                final retrofit2.Call<Void> responseMessage = retrofit.create(Restapi.class).deleteBankAccount(token, accessId, accountId);

                responseMessage.enqueue(new Callback<Void>() {
                    @Override
                    public void onResponse(retrofit2.Call<Void> call, Response<Void> response) {
                        // got response from server
                        if (response.isSuccessful()) {
                            Log.v(TAG, "Deleting bank account was successful " + response.code());
                            observable.onNext(response.body());
                        } else {
                            Log.e(TAG, "Deleting bank account was not successful! ERROR " + response.code());
                            observable.onError(new Throwable("Deleting bank account was not successful!"));
                        }
                    }


                    @Override
                    public void onFailure(retrofit2.Call<Void> call, Throwable t) {
                        // response from server failed
                        Log.e(TAG, "No connection to server!");
                        observable.onError(new Throwable("No connection to server!"));
                    }
                });
            }
        });

        return observable;
    }
}

@@ -53,34 +53,8 @@
 g;%0A%0A
-import android.util.Log;%0A%0A
 impo
@@ -638,62 +638,8 @@
 ct;%0A
-import retrofit2.Callback;%0Aimport retrofit2.Response;%0A
 impo
@@ -3130,53 +3130,8 @@
     
- final retrofit2.Call%3CVoid%3E responseMessage =
  ret
@@ -3195,1101 +3195,54 @@
 sId)
-;%0A%0A                responseMessage.enqueue(new Callback%3CVoid%3E() %7B%0A                    @Override%0A                    public void onResponse(r
+.enqueue(new R
 etrofit
-2.
 Call
-%3CVoid%3E call, Response%3CVoid%3E response) %7B%0A                        // got response from server%0A                        if (response.isSuccessful()) %7B%0A                            Log.v(TAG, %22Deleting bank accesses was successful %22 + response.code());%0A                            observable.onNext(response.body());%0A                        %7D else %7B%0A                            Log.e(TAG, %22Deleting bank accesses was not successful! ERROR %22 + response.code());%0A                            observable.onError(new Throwable(%22Deleting bank accesses was not successful!%22));%0A                        %7D%0A                    %7D%0A%0A%0A                    @Override%0A                    public void onFailure(retrofit2.Call%3CVoid%3E call, Throwable t) %7B%0A                        // response from server failed%0A                        Log.e(TAG, %22No connection to server!%22);%0A                        observable.onError(new Throwable(%22No connection to server!%22));%0A      
+back%3C%3E(observable));%0A
     
@@ -3249,35 +3249,26 @@
         
-  %7D%0A       
+%7D%0A
         
  %7D);%0A   
@@ -3263,41 +3263,14 @@
     
- 
 %7D);%0A
-            %7D%0A        %7D);
 %0A%0A
-%0A
     
@@ -3683,53 +3683,8 @@
     
- final retrofit2.Call%3CVoid%3E responseMessage =
  ret
@@ -3760,1131 +3760,51 @@
 tId)
-;%0A%0A                responseMessage.enqueue(new Callback%3CVoid%3E() %7B%0A                    @Override%0A                    public void onResponse(r
+.enqueue(new R
 etrofit
-2.
 Call
-%3CVoid%3E call, Response%3CVoid%3E response) %7B%0A                        // got response from server%0A                        if (response.isSuccessful()) %7B%0A                            Log.v(TAG, %22Deleting bank account was successful %22 + response.code());%0A                            observable.onNext(response.body());%0A                        %7D else %7B%0A                            Log.e(TAG, %22Deleting bank account was not successful! ERROR %22 + response.code());%0A                            observable.onError(new Throwable(%22Deleting bank account was not successful!%22));%0A                        %7D%0A                    %7D%0A%0A%0A                    @Override%0A                    public void onFailure(retrofit2.Call%3CVoid%3E call, Throwable t) %7B%0A                        // response from server failed%0A                        Log.e(TAG, %22No connection to server!%22);%0A                        observable.onError(new Throwable(%22No connection to server!%22));%0A                    %7D%0A                %7D
+back%3C%3E(observable)
 );%0A 

Use RetrofitCallback everywhere in HTTPBankingProvider.java