package com.github.ayltai.newspaper.util;

import android.graphics.PointF;
import android.support.annotation.NonNull;
import android.util.Log;

import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.github.ayltai.newspaper.media.DaggerImageComponent;
import com.github.ayltai.newspaper.media.ImageModule;
import com.github.piasy.biv.view.BigImageView;

import io.reactivex.Single;

public final class ImageUtils {
    private ImageUtils() {
    }

    public static void translateToFacesCenter(@NonNull final BigImageView image) {
        image.getSSIV().setOnImageEventListener(new SubsamplingScaleImageView.OnImageEventListener() {
            @Override
            public void onReady() {
                Single.<PointF>create(emitter -> emitter.onSuccess(DaggerImageComponent.builder()
                    .imageModule(new ImageModule(image.getContext()))
                    .build()
                    .faceCenterFinder()
                    .findFaceCenter(image.getCurrentImageFile())))
                    .compose(RxUtils.applySingleBackgroundToMainSchedulers())
                    .subscribe(
                        center -> image.getSSIV().setScaleAndCenter(image.getSSIV().getScale(), new PointF(center.x, center.y)),
                        error -> {
                            if (TestUtils.isLoggable()) Log.e(this.getClass().getSimpleName(), error.getMessage(), error);
                        }
                    );
            }

            @Override
            public void onImageLoaded() {
            }

            @Override
            public void onPreviewLoadError(final Exception e) {
                if (TestUtils.isLoggable()) Log.e(this.getClass().getSimpleName(), e.getMessage(), e);
            }

            @Override
            public void onImageLoadError(final Exception e) {
                if (TestUtils.isLoggable()) Log.e(this.getClass().getSimpleName(), e.getMessage(), e);
            }

            @Override
            public void onTileLoadError(final Exception e) {
                if (TestUtils.isLoggable()) Log.e(this.getClass().getSimpleName(), e.getMessage(), e);
            }

            @Override
            public void onPreviewReleased() {
            }
        });
    }
}

@@ -646,16 +646,23 @@
 ageView.
+Default
 OnImageE
@@ -675,24 +675,24 @@
 stener() %7B%0D%0A
-
             
@@ -1260,38 +1260,14 @@
 (), 
-new PointF(center.x, center.y)
+center
 ),%0D%0A
@@ -1547,315 +1547,34 @@
 d on
-ImageLoaded() %7B%0D%0A            %7D%0D%0A%0D%0A            @Override%0D%0A            public void onPreviewLoadError(final Exception e) %7B%0D%0A                if (TestUtils.isLoggable()) Log.e(this.getClass().getSimpleName(), e.getMessage(), e);%0D%0A            %7D%0D%0A%0D%0A            @Override%0D%0A            public void onImageLoadError(
+PreviewLoadError(@NonNull 
 fina
@@ -1765,11 +1765,12 @@
 d on
-Til
+Imag
 eLoa
@@ -1776,16 +1776,25 @@
 adError(
+@NonNull 
 final Ex
@@ -1981,27 +1981,155 @@
 d on
-PreviewReleased() %7B
+TileLoadError(@NonNull final Exception e) %7B%0D%0A                if (TestUtils.isLoggable()) Log.e(this.getClass().getSimpleName(), e.getMessage(), e);
 %0D%0A  

Simplify code