/*
 * Copyright 2012 GitHub Inc.
 *
 * 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 com.github.mobile.util;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static java.lang.Integer.MAX_VALUE;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html.ImageGetter;
import android.text.TextUtils;
import android.widget.TextView;

import com.github.kevinsawicki.http.HttpRequest;
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
import com.github.mobile.R.drawable;
import com.google.inject.Inject;

import java.io.File;
import java.io.IOException;

import roboguice.util.RoboAsyncTask;

/**
 * Getter for an image
 */
public class HttpImageGetter implements ImageGetter {

    private static class LoadingImageGetter implements ImageGetter {

        private final Drawable image;

        private LoadingImageGetter(final Context context, final int size) {
            int imageSize = Math.round(context.getResources().getDisplayMetrics().density * size + 0.5F);
            image = context.getResources().getDrawable(drawable.image_loading_icon);
            image.setBounds(0, 0, imageSize, imageSize);
        }

        public Drawable getDrawable(String source) {
            return image;
        }
    }

    private final LoadingImageGetter loading;

    private final Context context;

    private final File dir;

    private final int width;

    /**
     * Create image getter for context
     *
     * @param context
     */
    @Inject
    public HttpImageGetter(Context context) {
        this.context = context;
        dir = context.getCacheDir();
        width = ServiceUtils.getDisplayWidth(context);
        loading = new LoadingImageGetter(context, 48);
    }

    /**
     * Bind text view to HTML string
     *
     * @param view
     * @param html
     * @param id
     * @return this image getter
     */
    public HttpImageGetter bind(final TextView view, final String html, final Object id) {
        CharSequence encoded = !TextUtils.isEmpty(html) ? HtmlUtils.encode(html, loading) : "";
        view.setVisibility(encoded.length() > 0 ? VISIBLE : GONE);
        view.setText(encoded);

        // Use default encoding if no img tags
        if (html.indexOf("<img") == -1)
            return this;

        view.setTag(id);
        new RoboAsyncTask<CharSequence>(context) {

            @Override
            public CharSequence call() throws Exception {
                return HtmlUtils.encode(html, HttpImageGetter.this);
            }

            @Override
            protected void onSuccess(CharSequence html) throws Exception {
                if (!id.equals(view.getTag()))
                    return;

                view.setVisibility(html.length() > 0 ? VISIBLE : GONE);
                view.setText(html);
                view.setTag(null);
            }
        }.execute();
        return this;
    }

    public Drawable getDrawable(String source) {
        File output = null;
        try {
            output = File.createTempFile("image", ".jpg", dir);
            synchronized (this) {
                HttpRequest request = HttpRequest.get(source);
                if (!request.ok())
                    throw new IOException("Unexpected response code: " + request.code());
                request.receive(output);
            }
            Bitmap bitmap = ImageUtils.getBitmap(output, width, MAX_VALUE);
            if (bitmap == null)
                return loading.getDrawable(source);

            BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap);
            drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
            return drawable;
        } catch (IOException e) {
            return loading.getDrawable(source);
        } catch (HttpRequestException e) {
            return loading.getDrawable(source);
        } finally {
            if (output != null)
                output.delete();
        }
    }

}

@@ -2892,16 +2892,49 @@
 ) == -1)
+ %7B%0A            view.setTag(null);
 %0A       
@@ -2950,16 +2950,26 @@
 rn this;
+%0A        %7D
 %0A%0A      
@@ -3262,16 +3262,22 @@
 Success(
+final 
 CharSequ

Clear tag when HTML contains no image tags