package org.commcare.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.commcare.dalvik.R;
import org.commcare.utils.MediaUtil;
import org.commcare.utils.StringUtils;
import org.javarosa.core.reference.InvalidReferenceException;
import org.javarosa.core.reference.ReferenceManager;

import java.io.File;

public class ImageAdapter extends BaseAdapter {
    private final String[] choices;
    private final ImageView[] imageViews;
    private final Context context;

    public ImageAdapter(Context context, String[] choices, ImageView[] imageViews) {
        this.choices = choices;
        this.context = context;
        this.imageViews = imageViews;
    }

    @Override
    public int getCount() {
        return choices.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        String imageURI = choices[position];

        // It is possible that an imageview already exists and has been updated
        // by updateViewAfterAnswer
        ImageView mImageView = null;
        if (imageViews[position] != null) {
            mImageView = imageViews[position];
        }
        TextView mMissingImage = null;

        String errorMsg = null;
        if (imageURI != null) {
            try {
                String imageFilename =
                        ReferenceManager.instance().DeriveReference(imageURI).getLocalURI();
                final File imageFile = new File(imageFilename);
                if (imageFile.exists()) {
                    Display display =
                            ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE))
                                    .getDefaultDisplay();
                    int screenWidth = display.getWidth();
                    int screenHeight = display.getHeight();
                    Bitmap b = MediaUtil.getBitmapScaledToContainer(imageFile, screenHeight, screenWidth);
                    if (b != null) {

                        if (mImageView == null) {
                            mImageView = new ImageView(context);
                            mImageView.setBackgroundColor(Color.WHITE);
                        }

                        mImageView.setPadding(3, 3, 3, 3);
                        mImageView.setImageBitmap(b);

                        imageViews[position] = mImageView;
                    } else {
                        // Loading the image failed, so it's likely a bad file.
                        errorMsg = StringUtils.getStringRobust(context, R.string.file_invalid, imageFile.toString());
                    }
                } else {
                    // We should have an image, but the file doesn't exist.
                    errorMsg = StringUtils.getStringRobust(context, R.string.file_missing, imageFile.toString());
                }

                if (errorMsg != null) {
                    // errorMsg is only set when an error has occurred
                    Log.e("GridWidget", errorMsg);
                    mMissingImage = new TextView(context);
                    mMissingImage.setText(errorMsg);
                    mMissingImage.setPadding(10, 10, 10, 10);
                }
            } catch (InvalidReferenceException e) {
                Log.e("GridWidget", "image invalid reference exception");
                e.printStackTrace();
            }
        }

        if (mImageView != null) {
            mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return mImageView;
        } else {
            return mMissingImage;
        }
    }
}

@@ -149,37 +149,8 @@
 og;%0A
-import android.view.Display;%0A
 impo
@@ -206,43 +206,8 @@
 up;%0A
-import android.view.WindowManager;%0A
 impo
@@ -412,145 +412,8 @@
 ils;
-%0Aimport org.javarosa.core.reference.InvalidReferenceException;%0Aimport org.javarosa.core.reference.ReferenceManager;%0A%0Aimport java.io.File;
 %0A%0Apu
@@ -1515,40 +1515,8 @@
 l;%0A%0A
-        String errorMsg = null;%0A
     
@@ -1559,675 +1559,69 @@
     
-try %7B%0A                String imageFilename =%0A                        ReferenceManager.instance().DeriveReference(imageURI).getLocalURI();%0A                final File imageFile = new File(imageFilename);%0A                if (imageFile.exists()) %7B%0A                    Display display =%0A                            ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE))%0A                                    .getDefaultDisplay();%0A                    int screenWidth = display.getWidth();%0A                    int screenHeight = display.getHeight();%0A                    Bitmap b = MediaUtil.getBitmapScaledToContainer(imageFile, screenHeight, screenWidth);%0A        
+Bitmap b = MediaUtil.inflateDisplayImage(context, imageURI);%0A
     
@@ -1645,25 +1645,16 @@
 null) %7B%0A
-%0A        
         
@@ -1703,32 +1703,24 @@
             
-  
-      
 mImageView =
@@ -1740,32 +1740,24 @@
 w(context);%0A
-        
             
@@ -1824,35 +1824,18 @@
         
-       
- 
 %7D%0A
-%0A        
         
@@ -1889,32 +1889,24 @@
             
-        
 mImageView.s
@@ -1923,20 +1923,16 @@
 map(b);%0A
-%0A   
         
@@ -1939,21 +1939,16 @@
         
-     
 imageVie
@@ -1982,32 +1982,24 @@
             
-        
 %7D else %7B%0A   
@@ -2015,561 +2015,97 @@
     
-        // Loading the image failed, so it's likely a bad file.%0A                        errorMsg = StringUtils.getStringRobust(context, R.string.file_invalid, imageFile.toString());%0A                    %7D%0A                %7D else %7B%0A                    // We should have an image, but the file doesn't exist.%0A                    errorMsg = StringUtils.getStringRobust(context, R.string.file_missing, imageFile.toString());%0A                %7D%0A%0A                if (errorMsg != null) %7B%0A                    // errorMsg is only set when an error has occurred%0A    
+String errorMsg = StringUtils.getStringRobust(context, R.string.file_invalid, imageURI);%0A
     
@@ -2139,36 +2139,32 @@
 et%22, errorMsg);%0A
-    
                 
@@ -2194,36 +2194,32 @@
 tView(context);%0A
-    
                 
@@ -2259,36 +2259,32 @@
                 
-    
 mMissingImage.se
@@ -2313,189 +2313,8 @@
 0);%0A
-                %7D%0A            %7D catch (InvalidReferenceException e) %7B%0A                Log.e(%22GridWidget%22, %22image invalid reference exception%22);%0A                e.printStackTrace();%0A
     

make ImageAdapter inflate images the proper way