/**
 * Copyright (C) 2013 The Android Open Source Project
 *
 * 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 org.wikimedia.commons;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageContainer;
import com.android.volley.toolbox.ImageLoader.ImageListener;


public class MediaWikiImageView extends ImageView {

    private Media mMedia;

    private ImageLoader mImageLoader;

    private ImageContainer mImageContainer;

    private View loadingView;

    public MediaWikiImageView(Context context) {
        this(context, null);
    }

    public MediaWikiImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MediaWikiImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setMedia(Media media, ImageLoader imageLoader) {
        this.mMedia = media;
        mImageLoader = imageLoader;
        loadImageIfNecessary(false);
    }

    public void setLoadingView(View loadingView) {
        this.loadingView = loadingView;
    }

    public View getLoadingView() {
        return loadingView;
    }

    private void loadImageIfNecessary(final boolean isInLayoutPass) {
        loadImageIfNecessary(isInLayoutPass, false);
    }

    private void loadImageIfNecessary(final boolean isInLayoutPass, final boolean tryOriginal) {
        int width = getWidth();
        int height = getHeight();

        // if the view's bounds aren't known yet, hold off on loading the image.
        if (width == 0 && height == 0) {
            return;
        }

        final String  mUrl;
        if(tryOriginal) {
            mUrl = mMedia.getImageUrl();
        } else {
            // Round it down to the nearest 320
            // Possible a similar size image has already been generated.
            // Reduces Server cache fragmentation, also increases chance of cache hit
            // If width is less than 320, we just use that directly, to avoid a case of the Maths
            mUrl = mMedia.getThumbnailUrl(width <= 320 ? width : (width / 320) * 320);
        }

        // if the URL to be loaded in this view is empty, cancel any old requests and clear the
        // currently loaded image.
        if (TextUtils.isEmpty(mUrl)) {
            if (mImageContainer != null) {
                mImageContainer.cancelRequest();
                mImageContainer = null;
            }
            setImageBitmap(null);
            return;
        }

        // Don't repeat work. Prevents onLayout cascades
        // We ignore it if the image request was for either the current URL of for the full URL
        // Since the full URL is always the second, and
        if (mImageContainer != null && mImageContainer.getRequestUrl() != null) {
            Log.d("Commons", "Older one is " + mImageContainer.getRequestUrl() + " new one is " + mUrl);
            if (mImageContainer.getRequestUrl().equals(mMedia.getImageUrl()) || mImageContainer.getRequestUrl().equals(mUrl)) {
                return;
            } else {
                // if there is a pre-existing request, cancel it if it's fetching a different URL.
                mImageContainer.cancelRequest();
                BitmapDrawable actualDrawable = (BitmapDrawable)getDrawable();
                if(actualDrawable != null && actualDrawable.getBitmap() != null) {
                    setImageBitmap(null);
                    if(loadingView != null) {
                        loadingView.setVisibility(View.VISIBLE);
                    }
                }
            }
        }

        // The pre-existing content of this view didn't match the current URL. Load the new image
        // from the network.
        ImageContainer newContainer = mImageLoader.get(mUrl,
                new ImageListener() {
                    @Override
                    public void onErrorResponse(final VolleyError error) {
                        Log.d("Commons", "Error: or Url " + mUrl + " value is " + tryOriginal);
                        if(!tryOriginal) {
                            post(new Runnable() {
                                public void run() {
                                    loadImageIfNecessary(false, true);
                                }
                            });
                        }

                    }

                    @Override
                    public void onResponse(final ImageContainer response, boolean isImmediate) {
                        // If this was an immediate response that was delivered inside of a layout
                        // pass do not set the image immediately as it will trigger a requestLayout
                        // inside of a layout. Instead, defer setting the image by posting back to
                        // the main thread.
                        if (isImmediate && isInLayoutPass) {
                            post(new Runnable() {
                                @Override
                                public void run() {
                                    onResponse(response, false);
                                }
                            });
                            return;
                        }


                        Log.d("Commons", "No-Error: For Url " + mUrl + " value is " + tryOriginal);

                        if (response.getBitmap() != null) {
                            setImageBitmap(response.getBitmap());
                            if(loadingView != null) {
                                loadingView.setVisibility(View.GONE);
                            }
                        } else {
                            Log.d("Commons", "Whelp, fully can not load an image at all!");
                            // We got nothing back, figure out some sort of a solution?
                        }
                    }
                });

        // update the ImageContainer to be the new bitmap container.
        mImageContainer = newContainer;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.d("Commons", "Called via onLayout");
        loadImageIfNecessary(true);
        // Called via onLayout
    }

    @Override
    protected void onDetachedFromWindow() {
        if (mImageContainer != null) {
            // If the view was bound to an image request, cancel it and clear
            // out the image from the view.
            mImageContainer.cancelRequest();
            setImageBitmap(null);
            // also clear out the container so we can reload the image if necessary.
            mImageContainer = null;
        }
        super.onDetachedFromWindow();
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        invalidate();
    }
}

@@ -3588,113 +3588,8 @@
 ) %7B%0A
-            Log.d(%22Commons%22, %22Older one is %22 + mImageContainer.getRequestUrl() + %22 new one is %22 + mUrl);%0A
     
@@ -4620,104 +4620,8 @@
 ) %7B%0A
-                        Log.d(%22Commons%22, %22Error: or Url %22 + mUrl + %22 value is %22 + tryOriginal);%0A
     
@@ -6263,159 +6263,67 @@
     
-Log.d(%22Commons%22, %22Whelp, fully can not load an image at all!%22);%0A                            // We got nothing back, figure out some sort of a solution?
+// I'm not really sure where this would hit but not onError
 %0A   

Clean out some excess logging