/*
 * Copyright (C) 2014 Simon Vig Therkildsen
 *
 * Licensed 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 net.simonvt.cathode.module;

import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager;
import com.squareup.otto.Bus;
import dagger.Module;
import dagger.Provides;
import javax.inject.Singleton;
import net.simonvt.cathode.BuildConfig;
import net.simonvt.cathode.CathodeApp;
import net.simonvt.cathode.R;
import net.simonvt.cathode.api.ApiKey;
import net.simonvt.cathode.api.TraktModule;
import net.simonvt.cathode.api.UserToken;
import net.simonvt.cathode.event.AuthFailedEvent;
import net.simonvt.cathode.event.RequestFailedEvent;
import net.simonvt.cathode.settings.Settings;
import retrofit.ErrorHandler;
import retrofit.RetrofitError;
import retrofit.client.Response;
import timber.log.Timber;

@Module(
    complete = false,
    library = true,

    includes = {
        TraktModule.class
    })
public class ApiModule {

  private static final Handler MAIN_HANDLER = new Handler(Looper.getMainLooper());

  @Provides @ApiKey String provideClientId() {
    return BuildConfig.TRAKT_CLIENT_ID;
  }

  @Provides @Singleton ErrorHandler provideErrorHandler(final Bus bus) {
    return new ErrorHandler() {
      @Override public Throwable handleError(RetrofitError error) {
        switch (error.getKind()) {
          case HTTP:
            Timber.d(error, "Request failed");

            Response response = error.getResponse();
            if (response != null) {
              final int statusCode = response.getStatus();
              if (statusCode == 401) {
                MAIN_HANDLER.post(new Runnable() {
                  @Override public void run() {
                    bus.post(new AuthFailedEvent());
                  }
                });
              } else if (statusCode >= 500 && statusCode < 600) {
                MAIN_HANDLER.post(new Runnable() {
                  @Override public void run() {
                    bus.post(new RequestFailedEvent(R.string.error_5xx_retrying));
                  }
                });
              } else {
                MAIN_HANDLER.post(new Runnable() {
                  @Override public void run() {
                    bus.post(new RequestFailedEvent(R.string.error_unknown_retrying));
                  }
                });
              }
            }
            break;

          case CONVERSION:
          case UNEXPECTED:
            Timber.e(error, "Request failed");
            break;

          case NETWORK:
            Timber.e(error, "Request failed");

            MAIN_HANDLER.post(new Runnable() {
              @Override public void run() {
                bus.post(new RequestFailedEvent(R.string.error_network_retrying));
              }
            });
            break;
        }

        return error;
      }
    };
  }

  @Provides @Singleton UserToken provideUserToken(CathodeApp app) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(app);

    String token = settings.getString(Settings.TRAKT_TOKEN, null);

    return new UserToken(token);
  }
}

@@ -1927,56 +1927,8 @@
 TP:%0A
-            Timber.d(error, %22Request failed%22);%0A%0A
     
@@ -2067,24 +2067,79 @@
 etStatus();%0A
+              Timber.i(%22Status code: %22 + statusCode);%0A%0A
             
@@ -2668,16 +2668,63 @@
  else %7B%0A
+                Timber.e(error, %22Kind: HTTP%22);%0A
         
@@ -2948,32 +2948,144 @@
 %0A              %7D
+%0A%0A              Timber.d(error, %22Kind: HTTP%22);%0A            %7D else %7B%0A              Timber.e(error, %22Kind: HTTP%22);
 %0A            %7D%0A 
@@ -3130,16 +3130,85 @@
 ERSION:%0A
+            Timber.e(error, %22Kind: CONVERSION%22);%0A            break;%0A%0A
         
@@ -3251,38 +3251,40 @@
 r.e(error, %22
-Request failed
+Kind: UNEXPECTED
 %22);%0A        
@@ -3352,22 +3352,21 @@
 r, %22
-Request failed
+Kind: NETWORK
 %22);%0A

Add more detailed RetrofitError logging.