package io.vehiclehistory;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;

import io.vehiclehistory.activity.VehicleDataActivity;
import io.vehiclehistory.api.exception.VehicleHistoryApiException;
import io.vehiclehistory.api.method.AsyncMethodExecutor;
import io.vehiclehistory.api.method.GetVehicleMethod;
import io.vehiclehistory.api.method.ResponseListener;
import io.vehiclehistory.api.method.SessionHandler;
import io.vehiclehistory.api.model.Auth;
import io.vehiclehistory.api.model.VehicleInput;
import io.vehiclehistory.api.model.VehicleResponse;

public class PerformSearchDelegate {

    private static final String TAG = PerformSearchDelegate.class.getSimpleName();

    private static final int UNAUTHORIZED = 401;

    private final Activity activity;

    private final SessionHandler sessionHandler = new SessionHandler();
    private final AsyncMethodExecutor methodExecutor = new AsyncMethodExecutor();

    public PerformSearchDelegate(Activity activity) {
        this.activity = activity;
    }
    public void performSearch(Search search, OnSearchFinishedListener listener) {
        performSearch(toInput(search), listener);
    }

    public void performSearch(final VehicleInput input, final OnSearchFinishedListener listener) {
        GetVehicleMethod method = new GetVehicleMethod(input, sessionHandler.getSession().getAccessToken(),
                new ResponseListener<VehicleResponse>() {

                    @Override
                    public void onSuccess(VehicleResponse response) {
                        listener.onSearchFinished(response);
                        Intent i = new Intent(activity, VehicleDataActivity.class);
                        i.putExtra(VehicleDataActivity.EXTRA_VEHICLE_RESPONSE_KEY, response);

                        activity.startActivity(i);
                    }

                    @Override
                    public void onError(VehicleHistoryApiException exception) {
                        if (exception != null &&
                            exception.getStatusCode() != null &&
                            exception.getStatusCode() == UNAUTHORIZED) {
                            authorizeAndRetry(input, listener);
                        } else {
                            listener.onSearchError(exception != null ? exception.getUserMessage() : "Błąd podczas szukania " +
                                    "pojazdu");
                        }
                    }
                });

        methodExecutor.execute(method);
    }

    private void authorizeAndRetry(final VehicleInput input, final OnSearchFinishedListener listener) {
        sessionHandler.getNewSession(new ResponseListener<Auth>() {

            @Override
            public void onSuccess(Auth response) {
                if (!sessionHandler.getSession().getAccessToken().isEmpty()) {
                    performSearch(input, listener);
                } else {
                    Log.e(TAG, "Error fetching token");
                    listener.onSearchError("Can't get token.");
                }
            }

            @Override
            public void onError(VehicleHistoryApiException exception) {
                Log.e(TAG, exception.getMessage());
                listener.onSearchError("Can't get vehicle.");
            }
        });
    }

    private VehicleInput toInput(Search search) {
        VehicleInput input = new VehicleInput();

        input.setPlate(search.getPlate());
        input.setVin(search.getVin());
        input.setFirstRegistrationDate(search.getRegistrationDate());

        return input;
    }

    public interface OnSearchFinishedListener {
        void onSearchFinished(VehicleResponse vehicleResponse);
        void onSearchError(String message);
    }
}

@@ -736,19 +736,23 @@
 c final 
-i
+I
 nt
+eger
  UNAUTHO
@@ -2013,122 +2013,28 @@
 if (
-exception != null &&%0A                            exception.getStatusCode() != null &&%0A                            
+UNAUTHORIZED.equals(
 exce
@@ -2058,24 +2058,9 @@
 de()
- == UNAUTHORIZED
+)
 ) %7B%0A
@@ -2220,120 +2220,25 @@
 tion
- != null ? exception.getUserMessage() : %22B%C5%82%C4%85d podczas szukania %22 +%0A                                    %22pojazdu%22
+.getUserMessage()
 );%0A 

Fix NPE in PerformSearchDelegate.