// Copyright 2017 The Lynx Authors. All rights reserved.
package com.lynx.net.external;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.widget.ImageView;

import com.lynx.net.NetRequest;
import com.lynx.net.NetRequestBuilder;
import com.lynx.net.NetRequestManager;
import com.lynx.net.NetResponse;
import com.lynx.net.RequestInterceptor;
import com.lynx.net.simple.ImageNetResponse;
import com.squareup.picasso.Callback;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;
import com.squareup.picasso.Transformation;

import java.io.IOException;

public class ImageRequestManager extends NetRequestManager {

    private Picasso mPicasso;

    public ImageRequestManager(Context context) {
        mPicasso = Picasso.with(context);
    }

    @Override
    public void cancelAllRequest() {
    }

    @Override
    public void cancelRequest(NetRequest request) {
        ImageView imageView = null;

        Object externalData = request.getNetRequestInfo().getExternalData();

        if (externalData != null
                && externalData instanceof ImageRequestExternalData) {
            ImageRequestExternalData data = ((ImageRequestExternalData) externalData);
            imageView = data.getImageView();
        }

        Object tag = imageView == null ? this : imageView;
        if (imageView != null) {
            mPicasso.cancelRequest(imageView);
        } else {
            mPicasso.cancelTag(tag);
        }
    }

    @Override
    public void start() {

    }

    @Override
    public void stop() {
    }

    @Override
    public void fetch(NetRequest request) {
        NetRequestBuilder info = request.getNetRequestInfo();
        NetResponse response = request.getNetResponse();
        String url = info.getUrl();
        if (TextUtils.isEmpty(url)) {
            return;
        }
        ImageView imageView = null;
        ImageTransformation imageTransformation = null;

        Object externalData = info.getExternalData();

        if (externalData != null
                && externalData instanceof ImageRequestExternalData) {
            ImageRequestExternalData data = ((ImageRequestExternalData) externalData);
            imageView = data.getImageView();
            imageTransformation = data.getTransformation();
        }

        Object tag = imageView == null ? this : imageView;

        RequestCreator creator = mPicasso
                .load(url)
                .tag(tag)
                .noPlaceholder();

        if (imageTransformation != null) {
            creator.transform(new ImageRequestTransformation(imageTransformation));
        }

        LoadedListener loadedListener = new LoadedListener(mPicasso,
                url, (ImageNetResponse) response);

        if (imageView != null) {
            creator.into(imageView, loadedListener);
        } else {
            creator.fetch(loadedListener);
        }
    }

    @Override
    public <T> T  fetchSync(NetRequest request) {
        return null;
    }

    @Override
    public <T> T fetchFromCache(NetRequest request) {
        String url = request.getNetRequestInfo().getUrl();
        if (TextUtils.isEmpty(url)) {
            return null;
        }
        Bitmap result = null;
        try {
            result = mPicasso
                    .load(url)
                    .networkPolicy(NetworkPolicy.OFFLINE)
                    .get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (T) result;
    }

    @Override
    public void addInterceptor(RequestInterceptor interceptor) {
    }

    @Override
    public void removeInterceptor(RequestInterceptor interceptor) {
    }

    static class LoadedListener implements Callback {
        private ImageNetResponse mResponse;
        private Picasso mPicasso;
        private String mUrl;

        public LoadedListener(Picasso picasso, String url, ImageNetResponse response) {
            mResponse = response;
            mPicasso = picasso;
            mUrl = url;
        }

        @Override
        public void onSuccess() {
            if (mResponse != null) {
                if (mResponse.getType() == Bitmap.class) {
                    Bitmap result = null;
                    try {
                        result = mPicasso
                                .load(mUrl)
                                .networkPolicy(NetworkPolicy.OFFLINE)
                                .get();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mResponse.onResponse(result);
                } else {
                    mResponse.onResponse(null);
                }
            }
        }

        @Override
        public void onError() {
            if (mResponse != null) {
                mResponse.onFailure("Bitmap request error occurs internal!");
            }
        }
    }

    public static class ImageRequestTransformation implements Transformation {
        private ImageTransformation mImageTransformation;

        public ImageRequestTransformation(ImageTransformation transformation) {
            mImageTransformation = transformation;
        }

        @Override
        public Bitmap transform(Bitmap source) {
            return mImageTransformation.transform(source);
        }

        @Override
        public String key() {
            return mImageTransformation.key();
        }
    }
}

@@ -146,16 +146,59 @@
 Bitmap;%0A
+import android.graphics.drawable.Drawable;%0A
 import a
@@ -220,16 +220,16 @@
 tUtils;%0A
-
 import a
@@ -640,24 +640,60 @@
 estCreator;%0A
+import com.squareup.picasso.Target;%0A
 import com.s
@@ -3388,267 +3388,157 @@
     
-Bitmap result = null;%0A        try %7B%0A            result = mPicasso%0A                    .load(url)%0A                    .networkPolicy(NetworkPolicy.OFFLINE)%0A                    .get();%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D
+CacheDetector detector = new CacheDetector();%0A        // Async action%0A        mPicasso.load(url).networkPolicy(NetworkPolicy.OFFLINE).into(detector);
 %0A   
@@ -3553,22 +3553,31 @@
 urn (T) 
-result
+detector.bitmap
 ;%0A    %7D%0A
@@ -5451,24 +5451,24 @@
 ing key() %7B%0A
-
             
@@ -5517,11 +5517,432 @@
  %7D%0A    %7D
+%0A%0A    private static class CacheDetector implements Target %7B%0A        public Bitmap bitmap;%0A%0A        @Override%0A        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) %7B%0A            this.bitmap = bitmap;%0A        %7D%0A%0A        @Override%0A        public void onBitmapFailed(Drawable errorDrawable) %7B%0A        %7D%0A%0A        @Override%0A        public void onPrepareLoad(Drawable placeHolderDrawable) %7B%0A        %7D%0A    %7D
 %0A%7D%0A

Fix image fetched from cache bug with Picasso